C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# Expression bodied getters and settersC# expression body is a single line expression statement. It is used to provide single life definition to the method, constructor or property. We can use it to provide definition for the getter and setter. In the following example, we are providing expression body for the getter and setters. C# Expression bodied getters and setters Exampleusing System; namespace CSharpFeatures { class Student { private string SName; public Student(string name) => SName = name; public string Name { get => SName; // Expression body for getter set => SName = value; // Expression body for setter } } class ExpressionExample { public static void Main() { Student student = new Student("Peter"); Console.WriteLine(student.Name); } } } Output: Peter
Next TopicC# Async Main
|