My application uses a list like this:
List<MyClass> list = new List<MyClass>();
Using the Add method, another instance of MyClass is added to the list.
MyClass provides, among others, the following methods:
public void SetId(String Id);
public String GetId();
How can I find a specific instance of MyClass by means of using the GetId method? I know there is the Find method, but I don’t know if this would work here?!
Use a lambda expression
Note: C# has a built-in syntax for properties. Instead of writing getter and setter as ordinary methods (as you might be used to from Java), write
valueis an implicit parameter to the set accessor (but this may be about to change according to this LDM). It contains the value assigned to the property.Since this pattern is often used, C# provides auto-implemented properties. They are a short version of the code above; however, the backing variable is hidden and not accessible (it is accessible from within the class in VB, however).
You can simply use properties as if you were accessing a field:
Using properties, you would search for items in the list like this
You can also use auto-implemented properties if you need a read-only property:
This enables you to set the
Idwithin the class but not from outside. If you need to set it in derived classes as well you can also protect the setterAnd finally, you can declare properties as
virtualand override them in deriving classes, allowing you to provide different implementations for getters and setters; just as for ordinary virtual methods. In the deriving class you can override only the getter or only the setter. The other accessor is inherited without changes.Since C# 6.0 (Visual Studio 2015, Roslyn) you can write getter-only auto-properties with an inline initializer
You can also initialize getter-only properties within the constructor instead. Getter-only auto-properties are true read-only properties, unlike auto-implemented properties with a private setter.
This works also with read-write auto-properties:
Beginning with C# 6.0 you can also write properties as expression-bodied members
See: .NET Compiler Platform ("Roslyn")
The history of C# (version history)
Starting with C# 7.0, both, getter and setter, can be written with expression bodies:
Note that in this case the setter must be an expression. It cannot be a statement. The example above works, because in C# an assignment can be used as an expression or as a statement. The value of an assignment expression is the assigned value where the assignment itself is a side effect. This allows you to assign a value to more than one variable at once:
x = y = z = 0is equivalent tox = (y = (z = 0))and has the same effect as the statementsz = 0; y = 0; x = 0;.Since C# 9.0 you can use read-only (or better initialize-once) properties that you can initialize in an object initializer. This is currently not possible with getter-only properties.
Beginning with C# 11, you can have a
requiredproperty to force client code to initialize it.The
fieldkeyword is planned for a future version of C# (it did not make it into C# 11 and C# 12) and allows the access to the automatically created backing field in a semi-auto-implemented property.