In this article, we are going to talk about the use of properties in C# with examples.
What are the Properties in C#
A Property in C# is an extension of a field that provides a public way for the classes to read, write, and expose the private fields while hiding the implementation details.
Properties in C# are actually a special kind of getter and setter methods wrapped around a private variable. A get property accessor is used to retrieve the value and the set accessor is used to assign the new value.
In this article, we will understand the var keyword in C#.
var keyword in C#
The var keyword was introduced in C# 3.0 to declare an implicitly typed of local variable to hold any type of data. In implicitly typed variables, the type of the variable declared is decided by the compiler at the compile-time based on the type of the value that the variable is initialized with. The variable declared as var is mandatory to initialize at the time of declaration.
The following are the implicitly and explicitly typed variables declaration in C#.
Anonymous method : In C#, An anonymous method is a method without name and return type.
It can be created using the delegate keyword with the optional parameters and can be assigned to a variable of the delegate type. An anonymous method is an inline statement or expression which makes the code easy and more readable.
An anonymous method is useful when we want to hook an action directly to an event without separately creating an event handler.
Syntax to declare an anonymous method
The following is the syntax to declare an anonymous method in C#.