×

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# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute 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# 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# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

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.

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# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 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# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes 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# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.