1

I have a class testClass

public class testClass
{
    public int firstInt;
    public int SecondInt { get; set; }
}

On an event in my page _Default, I am creating an object and trying to set the properties.

    protected void Button1_Click(object sender, EventArgs e)
    {
        testClass objtestClass = new testClass();
        objtestClass.firstInt = 1;
        objtestClass.SecondInt = 2;
    }

Value got set in both correctly. But firstInt and SecondInt behaves different. See the image.

enter image description here

Why both appears in different color?

Community
  • 1
  • 1
Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70
  • 2
    The second is a property. The first is only a public visible variable. – juergen d Sep 21 '13 at 06:52
  • 1
    Q: Why both appears in different color? A: Because they're different: one is a public int, the other is a public property (that happens to read and write an int). – paulsm4 Sep 21 '13 at 07:00

4 Answers4

3

Here

public class testClass
{
public int firstInt; // a class variable/Field
public int SecondInt { get; set; } // property
}

Variables/Field: A variable corresponds directly to a memory location. You define a variable with a single declaration statement. A variable can be a local variable, defined inside a procedure and available only within that procedure, or it can be a member variable, defined in a module, class, or structure but not inside any procedure. A member variable is also called a field

Properties: A property is a data element defined on a module, class, or structure. You define a property with a code block between the Property and End Property statements. The code block contains a Get procedure, a Set procedure, or both. These procedures are called property procedures or property accessors. In addition to retrieving or storing the property's value, they can also perform custom actions, such as updating an access counter.

See the Msdn link here http://msdn.microsoft.com/en-us/library/sk5e8eth.aspx for explanation

Also this question has a wonderful explanation here What is Difference between Property and Variable in C#

Community
  • 1
  • 1
0

The way SecondInt appears is the sign for properties. and the way you did it as:

public int SecondInt { get; set; }

makes it the property. properties are easy to use (no need to call setter/getter etc.). Now SecondInt behaves as a property of your testclass.

Edit:

By refactoring in visual studio, you can automatically (or manually) do this:

private string m_MyProperty;
public string MyProperty 
{ 
  get { return m_MyProperty; }
  set { m_MyProperty = value; }
}

This way, your MyProperty property has been created, which sets and gets value for your private m_MyProperty string. and you can use it as:

String x = tstobj.MyProperty;
tstobj.MyProperty = x;
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
0

'firstInt' is a class field (variable) while 'SecondInt' is a .NET property. You can encapulate the processing SecondInt within your class if you need to by expanding the get and set methods.

doobop
  • 4,465
  • 2
  • 28
  • 39
0

Actually when you write a property like this:

public int second {get; set;}

called auto property, C# automatically uses a private variable like

private int _second;

(much like your first variable)

and use your property as below

public int second{ get{ return _second;} set{ _second = value;} }

something like getSecond and setSecond methods in java. so Properties are like two separate methods to getting and setting, and can has a backing field (a private variable) for storing data. Properties used for controlling access to internal data of class.

VahiD
  • 1,014
  • 1
  • 14
  • 30