0

What is the difference between

private string someText;

public string SomeText
{
    get;
    set;
}

and

public string SomeText
{
    get;
    set;
}
SubmarineX
  • 850
  • 5
  • 19
  • 38
  • 5
    I'm not a C#-expert but it seems like the first example doesn't really use the private string. You could probably learn a lot by reading the official [C# Programming Guide about properties](http://msdn.microsoft.com/en-us/library/w86s7x04.aspx) – Simon Forsberg Oct 16 '13 at 00:41

4 Answers4

3

nothing at all, your private member is not being used.

The compiler will compile

public string SomeText
{
     get;
     set;
}

to the equivalent of

private string _someText;

public string SomeText
{
   get { return _someText; }
   set { _someText = value; }
}
Rhys Bevilaqua
  • 2,102
  • 17
  • 20
  • I think an important point here is that the field is not actually called `_someText`, its name is something that you can't write in your code. – svick Oct 16 '13 at 01:05
  • @svick yep, but I figured it was better to show correct naming standards and say "equivalent" than to go hunting for the exact code it actually generates `[CompilerGenerated]private string k__BackingField;` [see here](http://stackoverflow.com/questions/1277018/c-sharp-3-0-automatic-properties-what-would-be-the-name-of-private-variable-c/1277082#1277082) – Rhys Bevilaqua Oct 23 '13 at 01:59
2

You do realise that your question is comparing two equal things? (

public string SomeText
{
    get;
    set;
}

)

I'm thinking what your question really is - is what is the difference between

private string someText;

public string SomeText
{
    get
     {
       return someText;
     }

    set 
     {
      someText = value;
     }
}

and :

public string SomeText
{
    get;
    set;
}

To which the answer is, in the 2nd example the backing fields still exist but are created for you - and you have no influence over the setting/getting; whereas in the first example you can put other checks in to make sure it's a valid value being set, etc.

NDJ
  • 5,189
  • 1
  • 18
  • 27
2

I think maybe you want to know the difference between

public class Test1
{
    private string _text;
    public string Text
    {
        get { return _text; }
        set { _text = value; }
    }
}

and

 public class Test2
{
    public string Text
    {
        get;
        set;
    }
}

If you disassemble these two classes into CIL , you will find it's almost the same except in the second case , the field was a auto generated one .

First case:

.property instance string Text
{
    .get instance string Syner.Test1::get_Text()
    .set instance void Syner.Test1::set_Text(string)
}


.field private string _text

Second case:

 .property instance string Text
{
    .get instance string Syner.Test2::get_Text()
    .set instance void Syner.Test2::set_Text(string)
}


.field private string <Text>k__BackingField
{
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
}
wlz
  • 563
  • 1
  • 4
  • 9
1

The second is just syntactic sugar for:

private string someText;

public string SomeText
{
    get { return someText; }
    set { someText = value; }
}

The second automatically handles the variable creation etc for you behind the scenes. In your first example, the private variable someText is never read/modified, it's just a class level variable that does nothing.

The reason you might want to use the first is if you need to do something more complicated in the getter/setter. For example you might want to check if something is initialized in the getter. Or you might want to validate the value in the setter.

Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66