Home > DeveloperSection > Beginner > Difference between 'Fields' and 'Properties' in C#

Difference between 'Fields' and 'Properties' in C#

C# C# 
0 Comment(s)
 1028  View(s)
Rate this:

Difference between Fields and Properties in C#


If you making the class field as public and exposing its value that is risky.you will not have  any control of what value get assigned and what will be returned.

A field is a variable i.e. declared directly in a class or struct in C#. A class or struct may have instance fields or static fields or used both. Often, you should use fields only for variables that have using access specifier private or protected accessibility. You know about the Data that your class exposes to client code should be provided through methods, properties and indexers. By using these constructs for indirect access to internal fields, you can guard against invalid input values. It means you can also provide a validation using in property.

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties are used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods. Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code. A get property accessor is used to return the property value, and a set accessor is used to assign a new value in C#.

The syntax of the property are as follows:

public <return type> PropertyName
        //Here you are returning a value
        return value;
     //you also check validation or other calculation

For setting its property by creating an instance of object of its class as :

  <ClassName> Obj= new <ClassName>();

 // For setting the property we are using this


For example,we are creating a class PropertyEx and defined two property SetName and SetAge.In SetName we are assigning a value its name and age we checking its age are above 18 or not for voting.

using System;
namespace PropertyDemo
    class Program
    {    //defining private member
         private string Name;
         private int Age =0;
         private static int num;
        //defining public method
        public String SetName
            get { return Name; }
            set { Name = value; } 
        public int SetAge
            get { return Age; }
            set { Age = value;
            if (Age >= 18)
                Age = value;
                Age = 0;
        static void Main(string[] args)
            //Creating instance using namespace.className
            PropertyDemo.Program pd = new PropertyDemo.Program();
             //For setting Name
pd.SetName="Anupam Mishra";
            //Initially we set age
pd.SetAge= 15;
            Console.WriteLine(“Your Name is ”+pd.SetName);
            if (pd.SetAge == 0)
               Console.WriteLine("You are not eligible for voting");
                Console.WriteLine("You are eligible for voting");

Your Name is Anupam Mishra
You are not eligible for voting

Don't want to miss updates? Please click the below button!

Follow MindStick