C# Properties (GET, SET)

Write short notes on C# Properties (GET, SET)

In c#, Property is an extension of the class variable and it provides a mechanism to read, write or change the value of the class variable without affecting the external way of accessing it in our applications.

 In c#, properties can contain one or two code blocks called accessors and those are called a get accessor and set accessor. By using get and set accessors, we can change the internal implementation of class variables and expose it without effecting the external way of accessing it based on our requirements.

Generally, in object-oriented programming languages like c# you need to define fields as private, and then use properties to access their values in a public way with get and set accessors.

Following is the syntax of defining a property with get and set accessor in c# programming language.

class User

{

    private string name;

    public string Name

    {

        get { return name; }

        set { name = value; }

    }

}

 

Leave Comment

Important Topics

Title
Object Oriented Programming (OOP)
OOP vs POP
Class with Example
Objects
Access Modifiers
Encapsulation with example
C# Properties (GET, SET)
Method and Function
Abstraction
Polymorphism
Operator Overloading
Inheritance
Constructor and its Types
Exception Handling
Throwing an Exception