none
How to identify a method in class in C# RRS feed

  • Question

  • Hello,

    supposing that I have a class

    public partial class A

    {

    ...

      public void Method()

      {

      }

    ...

    }

    and my source code is somewhat "longer" and contains more classes. Is it possible to recognize which class the method appertain to ? Similarly like in C++  is: public void A::Method(). Is there (in C#) something like:

    public partial class A

    {

    ...

      public void A.Method()   <<<<<<<<<< HERE the "A" the calss identifier.

      {

      }

    ...

    }

    Wednesday, October 3, 2018 3:05 PM

Answers

  • Hi,

    I don't understan what you mean. Do you mean the prototype pattern in c++? Why do you need this? The method is nested in the class, so it's not necessary. In prototype pattern the method is outside of the class body.

    Greetings, Chris

    • Marked as answer by Jerry.Mouse Wednesday, October 3, 2018 5:12 PM
    Wednesday, October 3, 2018 3:18 PM
  • As DerChris88 mentions, it sounds like you are getting confused with the C++ syntax.

    In C++ it is common (or possible) to declare the prototype of the class and methods first (in a .h file) and then define the actual methods separately (in a .cpp file). Because the methods are defined separately outside of the class definition, you do need the class identifier in front of the method names.

    However, C# syntax is different. Methods are always declared and defined entirely within the class itself, so it is entirely unnecessary (and would be superfluous) to repeat the class name in front of every method.

    If you are getting confused somehow, do you possibly have too many classes defined in a single file? (its better practice to stick to one class per file).

    Its also worth noting that Visual Studio displays the current class name and property/method name at the top of the editor based on where your text cursor is.

    There is also the Visual Studio Solution Explorer.


    • Edited by RJP1973 Wednesday, October 3, 2018 3:35 PM
    • Marked as answer by Jerry.Mouse Wednesday, October 3, 2018 5:12 PM
    Wednesday, October 3, 2018 3:34 PM
  • You have asked again, so I shall answer again, but hopefully more succinctly.

    No, it is not possible, in C#, to use write public void A.Method() similar to C++. Because C# is not C++. In C# methods are defined fully within their class so such a syntax would be unnecessary and superfluous.

    • Marked as answer by Jerry.Mouse Wednesday, October 3, 2018 5:09 PM
    Wednesday, October 3, 2018 3:50 PM
  • OK, folks, I am disappointed and frustrated, but I am accepting your rules :(

    ...and ...I found the way ! Viva La Revolucia :)

    public void /*A::*/ Method() 

    :)



    • Marked as answer by Jerry.Mouse Wednesday, October 3, 2018 5:12 PM
    • Edited by Jerry.Mouse Wednesday, October 3, 2018 5:13 PM update
    Wednesday, October 3, 2018 5:11 PM

All replies

  • Hi,

    I don't understan what you mean. Do you mean the prototype pattern in c++? Why do you need this? The method is nested in the class, so it's not necessary. In prototype pattern the method is outside of the class body.

    Greetings, Chris

    • Marked as answer by Jerry.Mouse Wednesday, October 3, 2018 5:12 PM
    Wednesday, October 3, 2018 3:18 PM
  • As DerChris88 mentions, it sounds like you are getting confused with the C++ syntax.

    In C++ it is common (or possible) to declare the prototype of the class and methods first (in a .h file) and then define the actual methods separately (in a .cpp file). Because the methods are defined separately outside of the class definition, you do need the class identifier in front of the method names.

    However, C# syntax is different. Methods are always declared and defined entirely within the class itself, so it is entirely unnecessary (and would be superfluous) to repeat the class name in front of every method.

    If you are getting confused somehow, do you possibly have too many classes defined in a single file? (its better practice to stick to one class per file).

    Its also worth noting that Visual Studio displays the current class name and property/method name at the top of the editor based on where your text cursor is.

    There is also the Visual Studio Solution Explorer.


    • Edited by RJP1973 Wednesday, October 3, 2018 3:35 PM
    • Marked as answer by Jerry.Mouse Wednesday, October 3, 2018 5:12 PM
    Wednesday, October 3, 2018 3:34 PM
  • yes, a method is outside, but it is not possible to move a method outside in C#, so

    is it possible to write public void A.Method()  similar to C++ ?

    Wednesday, October 3, 2018 3:42 PM
  • to stick one class in one file ? wooooooooou it is terrible :)

    • Edited by Jerry.Mouse Wednesday, October 3, 2018 3:43 PM update
    Wednesday, October 3, 2018 3:43 PM
  • You have asked again, so I shall answer again, but hopefully more succinctly.

    No, it is not possible, in C#, to use write public void A.Method() similar to C++. Because C# is not C++. In C# methods are defined fully within their class so such a syntax would be unnecessary and superfluous.

    • Marked as answer by Jerry.Mouse Wednesday, October 3, 2018 5:09 PM
    Wednesday, October 3, 2018 3:50 PM
  • Hi,

    you can use partial to split the class in different files.

    public partial class Test
    {
        //Part 1
    }
    
    public partial class Test
    {
        //Part 2
    }
    
    public partial class Test
    {
        //Part 3
    }

    Greetings, Chris

    Wednesday, October 3, 2018 3:55 PM
  • You can use the nesting for a single method if you want to.

    Here is an example:

    public partial class Test
    {
        private int field1;
        private int field2;
        private int field3;
    
        public Test(int i1, int i2, int i3)
        {
            field1 = i1;
            field2 = i2;
            field3 = i3;
        }
    }
    
    public partial class Test // << here is the identifier of the method
    {
        public int Method1()
        {
            return field1 + field2;
        }
    }
    
    public partial class Test
    {
        public void Method2()
        {
    
        }
    }
    
    public partial class Test
    {
        public void Method3()
        {
    
        }
    }

    Greetings, Chris

    Wednesday, October 3, 2018 4:47 PM
  • And you could use a different coding format:

    public partial class Test { public void Method2()
    {
    
    }}

    Now it looks similar to c++.

    Greetings, Chris


    • Edited by DerChris88 Wednesday, October 3, 2018 5:02 PM
    Wednesday, October 3, 2018 4:52 PM
  • OK, folks, I am disappointed and frustrated, but I am accepting your rules :(

    ...and ...I found the way ! Viva La Revolucia :)

    public void /*A::*/ Method() 

    :)



    • Marked as answer by Jerry.Mouse Wednesday, October 3, 2018 5:12 PM
    • Edited by Jerry.Mouse Wednesday, October 3, 2018 5:13 PM update
    Wednesday, October 3, 2018 5:11 PM