×

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all the fields of the class private and use only properties for getting and setting values. Prior to this, we were using method for this purpose. We can implement the logic also while setting values. Properties in C#, are read only or write only. They don’t have storage location. In C#, Properties are either read only or write only. They have accessors which are used to set and get values. Let's see an example where we are using Set and Get for getting and setting values.
using System;
public class Employee
{
            private int id;
            private string name;
            static int batch=2017;
            static string course="B. Tech.";
            public int ID
            {
            get
            {
                        return id;
            }
            set
            {
                        id=value;
            }
            }
            public string Name
            {
            get
            {
                        return name;
            }
            set
            {
                        name=value;
            }
            }
                        public static void Main (string[] args)
                        {
                                    Employee e=new Employee();
                                    e.ID=101;
                                    e.Name="Ayush Sharma";
                                    Console.WriteLine(e.ID+" "+e.Name);
                        }
}
Output
101 Ayush Sharma
C# properties Example: using logic while setting value                                        
using System;
public class Employee
{
            private int id;
            private string name;
            static int batch=2017;
            static string course="B. Tech.";
            public int ID
            {
            get
            {
                        return id;
            }
            set
            {
                        id=value;
            }
            }
            public string Name
            {
            get
            {
                        return name;
            }
            set
            {
                        Console.WriteLine("Enter Designation:");
                        string designation=Console.ReadLine();
                        name=value+" " +designation;                       
            }
            }
                        public static void Main (string[] args)
                        {
                                    Employee e=new Employee();
                                    e.ID=101;
                                    e.Name="Ayush Sharma";
                                    Console.WriteLine(e.ID+" "+e.Name);
                        }
}
Output
Enter Designation:
software developer
101 Ayush Sharma software developer

Related Topics

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.