0

Some times, I see classes contains properties declared like this:

public int MyProperty { get; set; }

Other properties are declared like this:

private int myVar;

public int MyProperty
{
    get { return myVar; }
    set { myVar = value; }
}

I come from Java background, so, I cannot Understand well the second (full) declaration.

Why we should have a private part and a public part in the second case? In which case we use the first declaration rather than the second (or the inverse). can any one explain any other differences?

  • use this link: https://stackoverflow.com/questions/1961536/why-should-i-use-a-private-variable-in-a-property-accessor – hassan.ef Apr 30 '19 at 08:04
  • This is Encapsulation: https://www.geeksforgeeks.org/c-sharp-encapsulation/ – asd Apr 30 '19 at 08:12
  • 1
    The former is a short hand notation for common use case. – Brian Rasmussen Apr 30 '19 at 08:23
  • 1
    @juanpe the lik explains the full property but doesn't states the differences. –  Apr 30 '19 at 09:29
  • @BromLem search by encansulaption. To `public int MyProperty { get; set; }` you have access directly to the property. In the other way, you can add logic to return o set the value. – asd Apr 30 '19 at 09:34

3 Answers3

2

The first case is actually a short-form of the second one in which you are just using the get and set accessors to retrieve and assign the value respectively.

Without adding any other logic

They are called as auto implemented properties.

About your question about why first int is defined as private? Well, that is what properties are! They provide a mechanism to read,write value of a private field.

Shad
  • 1,185
  • 1
  • 12
  • 27
  • regarding the second part of your answer, if we declare the property like this `private int MyProperty { get; set; }`, the read write mecanism doesn't work? –  Apr 30 '19 at 09:43
  • It will still work.. By setting access modifer to 'private' for a property you can now read, write the value using other members or a nested class within the same class! :) – Shad Apr 30 '19 at 09:51
1

In your example, they are exactly the same. The first is just shorthand for the second.

So why would we ever use the second?

What if we wanted to validate our value? We could do something like this:

private int myVar;

public int MyProperty
{
    get { return myVar; }
    set 
    { 
        if(value < 0)
            throw new Exception("Property can't be less than 0!").
        myVar = value; 
    }
}

Other examples could be if you need to recalculate something when MyProperty is set. Or maybe you need to raise an event to let other classes know it's been changed etc.

Loocid
  • 6,112
  • 1
  • 24
  • 42
  • 1
    I see, but validation could be done by the caller (parent logic) just before setting the value. if we have many modules which need to update/do calculation, we will end up with huge setter.. –  Apr 30 '19 at 08:56
0

In c#, Encapsulation is a process of binding the data members and member functions into a single unit. In c#, class is the real time example for encapsulation because it will combine a various type of data members and member functions into a single unit.

Her Example

microsoft docs

For example, you provide a library and do not want a second person to have a lot of information about your program or some other features or other class access classes. In this way, you can define your library in the same way as other libraries we use in the project, but we do not have access to its internal parts. In this case, you encapsulate your class.

That is, you can not access class functions or features elsewhere in the program, for example:

public int MyProperty { get; set; } Is Global In Your Project

private int myVar;

public int MyProperty
{
    get { return myVar; }
    set { myVar = value; }
}

You have access to MyProperty and not myVar

saman samadi
  • 436
  • 2
  • 8
  • 19