Reputation:
Can I encapsulate a private field through an automatic property in C#? When i use C# properties i surely can encapsulate private fields like.
private string owner;
public string Owner
{
get { return owner; }
set { owner=value;}
}
What happens when i use an automatic property?
public string Owner { get; set; }
That way I only interact with the property itself, right? Is there any way to use an automatic property to encapsulate a private field? How does it work?
Upvotes: 4
Views: 3646
Reputation: 1963
Interesting. As an aside, in VB.NET you also get auto-generated private fields from short-hand Property declarations, but you can directly access and interact with them (if you want/need to):
...
Public Property PropertyName As Object = Nothing
Sub New()
_PropertyName = "123"
End Sub
...
The auto-generated private field name is always the Property name prefixed with an underscore. (NOTE: it's NOT _camelCased, it's the same casing as the _PropertyName.)
Upvotes: 0
Reputation: 11697
Both piece of code you have given are nothing but same and will have same effect in your case. However using a local private property or rather call it property with backing fields can be used to give a default value.
private string owner = "I am the Owner";
public string Owner
{
get { return owner; }
set { owner=value;}
}
Also, backing fields are used if want to do any validations inside.
private string owner = "I am the Owner";
public string Owner
{
get { return owner; }
set
{
if(!string.IsNullOrEmpty(value))
owner=value;
}
}
They are also used for notifying property sometimes.
private string owner = "I am the Owner";
public string Owner
{
get { return owner; }
set
{
owner=value;
NotifyPropertyChanges("Owner");
}
}
Hope it helps.
Upvotes: 0
Reputation: 7401
There is a difference in the way an object is presented to the outside world, depending upon whether a value is exposed as:
public string Owner { get; set; }
which is a property, or
public string Owner;
which is a field.
If you have a private variable, there is no need for automatic properties as it doesn't matter to you if it's a field or a property inside your class. If you do later want to expose it externally, that's when you start having code like:
private string myPrivateValue;
public string myPrivateValueAsProperty {
get { return myPrivateValue; }
set { myPrivateValue = value; }
}
Upvotes: 0
Reputation: 1063338
Is there any way to use an automatic property to encapsulate a private field?
Yes; that is exactly what an automatically implemented property is. Simply: the compiler declares the field for you - you never see the field directly. Perhaps the real question here should be:
If I use an automatically implemented property, can I access the underlying field directly?
To which the answer is: no; just access the property instead. After JIT inlining, you'll never know the difference anyway.
Upvotes: 3
Reputation: 2590
the default property in C# compiles to a private field with a public getter and setter.
this
public string Name{get;set;}
compiles to this:
private string name
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
(actually it doesnt, the variable names are different and it uses accessor/mutators, but essentially they are the same thing)
Upvotes: 0
Reputation: 677
the private field is created at compile time with a unique name, it's being used behind the scenes. the automatic properties are implemented so that your type interface would not change if you, for example add validation on the setter. in your class you should then reference the public property. that way when you add the validation to a setter, your class would not have special privileges as to pass the validation process (if one would be implemented).
Upvotes: 0
Reputation: 62276
No there is way to do that what you're asking for.
Automatic property defines a field, but it's hidden and created at compile time.
If issue is a typing and you're using Visual Studio :
just type inside editor propfull and make double tap on TAB, Visual Studio will automatically create a property and a field encapsulatd inside it with the name specified by you.
Upvotes: 0