Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public accessor .net

Tags:

c#

I think it was in .net 2.0, microsoft introduced an accessor that was abbreviated to something like

public string Name { get; set; }

But is there any real difference between the above code, and simply:

public string Name;
like image 843
maxp Avatar asked Dec 24 '09 10:12

maxp


People also ask

Why do we use accessors in C#?

A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. In C# 9 and later, an init property accessor is used to assign a new value only during object construction. These accessors can have different access levels.

What is public string in C#?

Public accessors allow any object to read and write these properties. It's sometimes desirable, however, to exclude one of the accessors. You can omit the set accessor to make the property read-only: C# Copy. public string Name { get { return _name; } private set { _name = value; } }

What is public/private and protected in C#?

public - can be access by anyone anywhere. private - can only be accessed from with in the class it is a part of. protected - can only be accessed from with in the class or any object that inherits off of the class.

What is accessor and mutators in C#?

An Accessors just returns value of one of the member variables . It is a member function that accesses the contents of the object but does not modify the objects. A Mutator just assign a new value to one of the member variable. It is a member function that can modify an object.


2 Answers

The main difference is that if you later need to add logic into your getter or setter, and other DLLs have already been compiled against yours, you can easily change

public string Name { get; set; }

into

public string Name { get{/*special code*/} set{/*special code*/} }

and it won't be a breaking change to publish your new DLL and have other DLLs not be recompiled.


Whereas if you changed

public string Name;

into

public string Name { get{/*special code*/} set{/*special code*/} } 

then you'll need to make sure any DLLs that use yours are recompiled, since they change from accessing a field into accessing a property.

This is obviously a bigger problem when you're shipping DLLs to other programmers (as an open source project or as a component vendor for example) than if you're just building an app for your own self / employer

like image 64
Rob Fonseca-Ensor Avatar answered Oct 20 '22 22:10

Rob Fonseca-Ensor


The difference is between a Field and a Property. A field is just a member variable on the class instance. By contrast, a property is shorthand for two separate actions - get and set:

public string Name
{
    get
    {
        return _name;
    }
    set
    {
        _name = value;
    }
}

private string _name;

This is an over-simplified example, as the property simply "wraps" the private field by returning it in the getter and setting it in the setter. However, properties become very useful when they become "gateways" to the underlying value. If program flow requires something happen every time the value of a field is set (say, an event is fired), it can be fired from the setter of the property:

set
{
    this.InvokePropertyChangedEvent();
    _name = value;
}

The exact syntax you're asking about is called Auto-Implemented Properties, which is just shorthand for the simple example I provided above. The compiler creates a private member that is got and set by the property.

like image 25
Rex M Avatar answered Oct 20 '22 20:10

Rex M