×

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# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes 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.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 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# 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# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 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# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

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.

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.

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.

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.

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# 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# 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# 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# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.