Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use a lambda expression</p> <pre><code>MyClass result = list.Find(x =&gt; x.GetId() == "xy"); </code></pre> <hr> <p>Note: C# has a built-in syntax for properties. Instead of writing Set- and Get-methods (how you could be used to it from Java), write</p> <pre><code>private string _id; public string Id { get { return _id; } set { _id = value; } } </code></pre> <p><code>value</code> is a contextual keyword known only in the set accessor. It represents the value assigned to the property.</p> <p>Since this pattern is often used, C# provides automatically 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).</p> <pre><code>public string Id { get; set; } </code></pre> <p>You can simply use properties as if you were accessing a field:</p> <pre><code>var obj = new MyClass(); obj.Id = "xy"; // Calls the setter with "xy" assigned to the value parameter. string id = obj.Id; // Calls the getter. </code></pre> <hr> <p>Using properties, you would search for items in the list like this</p> <pre><code>MyClass result = list.Find(x =&gt; x.Id == "xy"); </code></pre> <hr> <p>You can also use automatically implemented properties if you need a read-only property:</p> <pre><code>public string Id { get; private set; } </code></pre> <p>This enables you to set the <code>Id</code> within the class but not from outside. If you need to set it in derived classes as well you can also protect the setter</p> <pre><code>public string Id { get; protected set; } </code></pre> <hr> <p>And finally, you can declare properties as <code>virtual</code> and override them in deriving classes, allowing you to provide different implementations for getters and setters; just as for ordinary virtual methods.</p> <hr> <p>Since C# 6.0 (Visual Studio 2015, Roslyn) you can write getter-only auto-properties with an inline initializer</p> <pre><code>public string Id { get; } = "A07"; // Evaluated once when object is initialized. </code></pre> <p>You can also initialize getter-only properties within the constructor instead. Getter-only auto-properties are <strong>true</strong> read-only properties, unlike automatically implemented properties with a private setter.</p> <p>This works also with read-write auto-properties:</p> <pre><code>public string Id { get; set; } = "A07"; </code></pre> <p>Beginning with C# 6.0 you can also write properties as expression-bodied members</p> <pre><code>public DateTime Yesterday =&gt; DateTime.Date.AddDays(-1); // Evaluated at each call. // Instead of public DateTime Yesterday { get { return DateTime.Date.AddDays(-1); } } </code></pre> <p>See: <a href="https://roslyn.codeplex.com/" rel="noreferrer">.NET Compiler Platform ("Roslyn")</a><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6" rel="noreferrer">New Language Features in C# 6</a></p> <p>Starting with <a href="https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/" rel="noreferrer">C# 7.0</a>, both, getter and setter, can be written with expression bodies:</p> <pre><code>public string Name { get =&gt; _name; // getter set =&gt; _name = value; // setter } </code></pre> <p>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: <code>x = y = z = 0</code> is equivalent to <code>x = (y = (z = 0))</code> and has the same effect as the statements <code>x = 0; y = 0; z = 0;</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload