---
title: "Difference between & 'Fields' and & 'Properties' in C#"  
description: "in this article, we define difference between Fields and Property in C#."  
author: "Anupam Mishra"  
published: 2015-12-18  
updated: 2017-12-14  
canonical: https://www.mindstick.com/articles/11957/difference-between-fields-and-properties-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# 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#](https://www.mindstick.com/forum/161271/what-is-the-difference-between-class-and-struct-in-c-sharp). A class or struct may have instance fields or static fields or used both. Often, you should use fields only for [variables](https://www.mindstick.com/articles/715/php-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](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule) and indexers. By using these constructs for indirect access to internal fields, you can guard against [invalid input](https://www.mindstick.com/forum/159539/how-to-handle-an-invalid-input-exception-like-entering-a-character-instead-of-an-integer) values. It means you can also provide a [validation](https://www.mindstick.com/articles/43/validation-controls-in-asp-dot-net) 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](https://www.mindstick.com/interview/2390/can-you-achieve-runtime-polymorphism-by-data-members-in-java), 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](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) or [verification](https://www.mindstick.com/forum/34736/what-is-difference-between-validation-and-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#](https://www.mindstick.com/forum/34141/how-to-set-cookie-value-in-c-sharp).

##### The syntax of the property are as follows:

```
public <return type> PropertyName{     get{         //Here you are returning a value        return value;        }     set{      <VaribleName>=value;     //you also check validation or other calculation        }}
```

For setting its property by [creating an instance](https://www.mindstick.com/forum/156698/can-you-call-the-base-class-method-without-creating-an-instance) of object of its class as :

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

// For setting the property we are using this

```
  Obj.PropertyName=<value>;
```

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;            }            else            {                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");            else            {                Console.WriteLine("You are eligible for voting");            }             Console.ReadKey();        }    }}
```

\

##### Output:

\
Your Name is Anupam Mishra\
You are not eligible for voting

---

Original Source: https://www.mindstick.com/articles/11957/difference-between-fields-and-properties-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
