Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » .Net

Properties (set and get) in C#



In this article, we will learn about the C# properties, types of properties (set and get) with example.
Submitted by IncludeHelp, on August 06, 2018

C# properties

Properties are the members of a class, interface, and structure in C#. As we know that, members or methods of class and structure are known as "Fields". Properties are an extended version of fields with almost same syntax.

Using properties, we can read and write or manipulate private fields. For example, we have a class name EMPLOYEE that contains private fields EMP_ID, EMP_NAME, EMP_SALARY. Normally we cannot access those fields out of the class. But using Properties we can access them.

Types of C# properties

There are two types of properties are using to access private fields:

  1. set - To write value in private fields
  2. get - To read value of private fields

Declaration:

public int ID
{
	get
	{
		return EMP_ID;
	}

	set 
	{
		EMP_ID = value;
	}
}

public string NAME
{
	get
	{
		return EMP_NAME;
	}

	set 
	{
		EMP_NAME = value;
	}
}

public int SALARY
{
	get
	{
		return EMP_SALARY;
	}
	set 
	{
		EMP_SALARY = value;
	}
}


Example: With the help of program, we can understand about properties easily...

using System;
using System.Collections;
namespace ConsoleApplication1
{
    class EMPLOYEE
    {
        private int     EMP_ID      ;
        private string  EMP_NAME    ;
        private int     EMP_SALARY  ;

        public int ID
        {
            get
            {
                return EMP_ID;
            }

            set
            {
                EMP_ID = value;
            }
        }

        public string NAME
        {
            get
            {
                return EMP_NAME;
            }

            set
            {
                EMP_NAME = value;
            }
        }

        public int SALARY
        {
            get
            {
                return EMP_SALARY;
            }
            set
            {
                EMP_SALARY = value;
            }
        }

    }

    
         
    class Program
    {
        static void Main()
        {
            EMPLOYEE E = new EMPLOYEE();

            E.ID   = 101;
            E.NAME = "Duggu Pandit";
            E.SALARY = 1000000;

            Console.WriteLine("ID     : " + E.ID);
            Console.WriteLine("NAME   : " + E.NAME);
            Console.WriteLine("SALARY : " + E.SALARY);

        }
    }
}

Output

    ID		: 101
    NAME	: Duggu Pandit
    SALARY	: 1000000





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.