Are C# properties actually Methods?

C#.NetOop

C# Problem Overview


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

enter image description here

and this was an "Eye Opener" to me. I was expecting one property stringProp and one method stringProp but instead I got this.

Why this happened? can someone explain please.

C# Solutions


Solution 1 - C#

Yes, the compiler generates a pair of get and set methods for a property, plus a private backing field for an auto-implemented property.

public int Age {get; set;}

becomes the equivalent of:

private int <Age>k__BackingField;

public int get_Age()
{
     return <Age>k__BackingField;
}

public void set_Age(int age)
{
    <Age>k__BackingField = age;
}

Code that accesses your property will be compiled to call one of these two methods. This is exactly one of the reasons why changing a public field to be a public property is a breaking change.

See Jon Skeet's Why Properties Matter.

Solution 2 - C#

Strictly speaking, properties are not methods, although they are indeed supported by getter and setter methods (also called accessors). When you write code like this (provided you modify the code to remove the compile error mentioned below)

myFoo.stringProp = "bar";

The compiler actually generates IL code like this:

ldstr       "bar"
callvirt    foo.set_stringProp

Where set_stringProp is the setter method for that property. In fact, if you so desired, you can invoke these methods directly via reflection.

However, the code sample you posted may look okay in Visual Studio's intellisense, but it will not compile. Try building the project and you will see an error like:

> The type 'foo' already contains a definition for 'stringProp'

Solution 3 - C#

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)

Solution 4 - C#

Yes. Properties are mutator methods.

In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (also known as an accessor), which returns the value of the private member variable.

The mutator method is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.

Mutator methods may also be used in non-object-oriented environments. In this case, a reference to the variable to be modified is passed to the mutator, along with the new value. In this scenario, the compiler cannot restrict code from bypassing the mutator method and changing the variable directly. The onus falls to the developers to ensure the variable is only modified through the mutator method and not modified directly.

In programming languages that support them, properties offer a convenient alternative without giving up the utility of encapsulation.

Reference: http://en.wikipedia.org/wiki/Mutator_method

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyogiView Question on Stackoverflow
Solution 1 - C#dcastroView Answer on Stackoverflow
Solution 2 - C#p.s.w.gView Answer on Stackoverflow
Solution 3 - C#TigranView Answer on Stackoverflow
Solution 4 - C#Amir SaniyanView Answer on Stackoverflow