Are C# properties actually Methods?

前端 未结 4 1222
花落未央
花落未央 2020-12-04 20:54

Till now, I was under the impression that Properties & Methods are two different things in C#. But then I did something like below.

4条回答
  •  广开言路
    2020-12-04 21:35

    This is visual studio intelicence issue, that picks by name. By the way your code will not compile even, due the name collision in the same type.

    But you are right, that properties are methods at the end:

    public class A {
    
       public string Name  {get;set;}  
    }
    

    here Name property is converted into 2 methods: get_Name() and set_Name().

    In fact, if you define class like this:

    public class A {
    
       public string Name  {get;set;}  
    
       public string get_Name() {
           return "aaa"; 
       }
    }
    

    you will get compilation error, as there is already defined get_Name (property)

提交回复
热议问题