Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Automatically implemented properties must define both get and set accessors

When we define a property like

    public string Name {get; set;} 

dot net can make our properties code. but when we use

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

we face with

'Hajloo.SomeThing.PropertyName.set' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors. 

Actually why the compiler can't determine the property and make code automatically? What's the problem?

like image 411
Nasser Hadjloo Avatar asked Apr 08 '10 06:04

Nasser Hadjloo


People also ask

What is an auto implemented property and what does it automatically create?

Auto-implemented properties declare a private instance backing field, and interfaces may not declare instance fields. Declaring a property in an interface without defining a body declares a property with accessors that must be implemented by each type that implements that interface.

Why do we use get and set properties 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 an auto implemented property?

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.

Why should auto implemented properties be avoided?

Auto-implemented properties are not guaranteed to keep the same backing field name between builds. Therefore, it is theoretically possible that serializing an object in one version of an assembly, and then re-serializing that same object in another assembly could cause breaking changes.


2 Answers

Because the auto-implemented properties generate their own backing store for the property values. You have no access to the internal store.

Implementing a property with

  • just get : means you can only retrieve the values. You can't ever set the property value (even in the containing class)
  • just set : means you can only set the values. You can't retrieve the property value.

for a normal property

private int _data; public int Data{  get { return _data } }; 

Here the parent class can do the following somewhere else in the class ( which it can't with auto props)

_data = 100;

Note: You can define an auto-prop like this (which is how I use it the most).

public int Data { get; private set;} 

This means that the property can't be set by external clients of the class. However the containing class itself can set the property multiple times via this.Data = x; within the class definition.

like image 59
Gishu Avatar answered Oct 23 '22 22:10

Gishu


If there is no setter, the property can never have anything other than the default value, so doesn't serve any purpose.

like image 44
Joe Avatar answered Oct 23 '22 22:10

Joe