Showing posts with label properties. Show all posts
Showing posts with label properties. Show all posts

Wednesday, March 31, 2021

C# Properties (Getter & Setters)

Encapsulation can be achieved using C# properties. Encapsulation means hiding a complex object from the outer world, in other words not exposing sensitive information to the client. 

For which we will require Private members and as that is not accessible to the outer world we initialize the values using Properties. 

Basically, Property contains two methods Get and Set. 

Example,

class Car
{
        private string model;                //Field member

        public string Model                  //Property
        {
              get
                {
                        return model;        //Get method
                }
                set

                {
                        model = value;        //Set method
                }
        }
}

Achieving encapsulation,

  • Provides control for the class members
  • Making fields read-only (Only get method) and write-only (Only set method)
  • Provides data security

C# Interview Prep: 100 Common Questions for 0-3 Years Experience

Common C# Interview Questions  Basics of C# Language: What is C#? C# (pronounced C sharp) is a modern, object-oriented programming language...