8

I have looked at at least 10 SO questions on get/set but cannot find mine. So I hope this is not a duplicate.

public class myint
{
    public int value{get;set;}
}

vs

public class myint
{
    public int value;
}

The above 2 codes look the same to me. If I want to use the myint class, I just write the code below and it can run on either class.

myint A;
A.value=10;

So what is the get/set use for?

user3398315
  • 331
  • 6
  • 17
  • They are simply a good practice to follow in this case. Usually you use them when you have backing store for the corresponding Property. – bit Mar 19 '14 at 05:34
  • Read a book about c# and properties. Start by google why use properties. This question is basic and have plenty of answes – ilansch Mar 19 '14 at 06:31

5 Answers5

2

You're asking what the difference is between using a public instance variable vs. getter/setter properties I assume.

Properties allow you to further encapsulate logic around getting or setting a variable, for example adding simple validation logic. You could throw an exception if someone sets your value to less than zero for example. You could also add further logic in the getter/setter to for example synchronize a specific field.

A few other differences:

  • Properties can be used for data binding easily in most .NET UI frameworks.
  • Reflection works differently.
  • Differing access levels for get/set vs. for example your instance variable you can choose between readonly, private, protected, static, etc. as a whole.
  • There is more overhead accessing a property. This is usually unimportant in most use cases other than games and highly performance sensitive situations.
therewillbesnacks
  • 1,015
  • 6
  • 8
1

http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be 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.

Here are few things off the top of my head that differentiate a public {get;set;} vs a public member variable:

  1. Properties are needed for data binding.
  2. get and set can have different accessors (e.g. public int Value {get; protected set;}
  3. get;set; can be part of a interface e.g. interface IHasValueGetter { public int Value {get;}}

What is the difference between a Field and a Property in C#?

Community
  • 1
  • 1
DeveloperGuo
  • 636
  • 5
  • 15
1

here is when do we use get set
According to the Property Usage Guidelines on MSDN:

  • Use a property when the member is a logical data member. In the following member declarations, Name is a property because it is a
    logical member of the class.

Use a method when:

  • The operation is a conversion, such as Object.ToString.
  • The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
  • Obtaining a property value using the get accessor would have an observable side effect.
  • Calling the member twice in succession produces different results.
  • The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order.
  • The member is static but returns a value that can be changed.
  • The member returns an array. Properties that return arrays can be very misleading. Usually it is necessary to return a copy of the
    internal array so that the user cannot change internal state. This,
    coupled with the fact that a user can easily assume it is an indexed
    property, leads to inefficient code. In the following code example,
    each call to the Methods property creates a copy of the array. As a
    result, 2n+1 copies of the array will be created in the following
    loop.
Awtszs
  • 323
  • 1
  • 8
  • 33
0

you can remove get and set it will not affect the code and working due to the reason that you have defined a variable of type int with the Access type of public so that properties are mostly used to access the private members of class which is in your case do not exist so go on and remove it how ever if in top most class you define a variable with Private modifier the to access it get and set are necessary properties!

Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29
0
// This is an example of property...
public class myint
{
    public int value{get;set;}
}

// This is an example of field...
public class myint
{
    public int value;
}

The difference:

  • Databinding techniques only works on properties, and not on fields
  • Fields may be used as input to out/ref arguments. Properties may not.
  • Properties may throw exceptions - fields will never do that

    Example:

    class Person
    {
       private string _name;
       public string FirstName
       {
           get
           {
               return _name ?? string.Empty;
           }
           set
           {
               if (value == null)
                  throw new System.ArgumentNullException("value");
               _name = value;
           }
       }
    

    }

  • Properties support different accessibility for getters/setters - fields do not (but fields can be made readonly)

Usman Khalid
  • 3,032
  • 9
  • 41
  • 66