Questions tagged [automatic-properties]
219 questions
37
votes
7 answers
How to prevent auto implemented properties from being serialized?
How can I prevent a auto implemented property from being serialized by the binary formatter?
The [NonSerialized] attribute can only be used with fields. And the field is hidden when using auto implemented properties.

hopla
- 1,048
- 1
- 11
- 15
36
votes
8 answers
how to override set in C# of automatic properties
I want to use auto-implemented properties, but at the same time I want to call a method every time the property is changed.
public float inverseMass
{
get;
set
{
onMassChanged();
inverseMass = value;
}
}
Does the…

MhdSyrwan
- 1,613
- 3
- 19
- 26
30
votes
5 answers
How to set default value for Auto-Implemented Properties in ASP.NET
I came to know that C# 3.0 comes with a new feature of Auto-Implemented Properties,I liked it as we don't have to declare extra private varible in this (compare to earlier property), earlier I was using a Property i.e.
private bool isPopup =…

Imran Rizvi
- 7,331
- 11
- 57
- 101
28
votes
4 answers
Can't set breakpoints on an auto-property setter ? Why?
Apparently VS 2008 does not allow setting a breakpoint just on the setter of an auto-property.
I.e. if I define an auto-property like this:
public int CurrentFramesize
{
get;
protected set;
}
and then try to set a…

Cristian Diaconescu
- 34,633
- 32
- 143
- 233
28
votes
2 answers
How to find out if a property is an auto-implemented property with reflection?
So in my case i am doing discovery of the structure of a class using reflection. I need to be able to find out if a property is an auto-implemented property by the PropertyInfo object. I assume that the reflection API does not expose such…

Zoki
- 303
- 3
- 6
27
votes
5 answers
Getter without body, Setter with
I have a property which is currently automatic.
public string MyProperty { get; set; }
However, I now need it to perform some action every time it changes, so I want to add logic to the setter. So I want to do something like:
public string…

komodosp
- 3,316
- 2
- 30
- 59
25
votes
7 answers
Are C# auto-implemented static properties thread-safe?
I would like to know if C# automatically implemented properties, like public static T Prop { get; set; }, are thread-safe or not. Thanks!

Miguel Angelo
- 23,796
- 16
- 59
- 82
24
votes
5 answers
Automatically implemented property in struct can not be assigned
I have a next code:
struct T
{
public T(int u)
{
this.U = 10; //Errors are here
}
public int U { get; private set; }
}
C# compiler give me a pair of errors in stated line:
1) Backing field for automatically implemented…

Tadeusz
- 6,453
- 9
- 40
- 58
24
votes
3 answers
DefaultValue attribute is not working with my Auto Property
I have the following Auto Property
[DefaultValue(true)]
public bool RetrieveAllInfo { get; set; }
when I try to use it inside the code i find the default false for is false I assume this is the default value to a bool variable, does anyone have a…

Ahmed Magdy
- 5,956
- 8
- 43
- 75
23
votes
4 answers
Encapsulate Collection in C#
Since 3.0 C# has great syntax sugar like auto-properties which a lot simplify implementation of encapsulation principle. This is good if you use it with atomic values, so you can replace encapsulation pattern like this:
private string _name;
public…

Vitaliy Ulantikov
- 10,157
- 3
- 61
- 54
23
votes
2 answers
Why is `this` not available in C# 6.0 Auto-Property Initialization?
I have the following code class:
public class Foo
{
public Nested Bar { get; } = new Nested(this);
public class Nested
{
public Nested(Foo foo)
{
foo.DoSomething();
}
}
private void…

Luke Vo
- 17,859
- 21
- 105
- 181
23
votes
5 answers
Why doesn't Java have automatic properties like C#?
C# has automatic properties which greatly simplify your code:
public string Name { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
Whereas Java has you write this much code:
private String name;
private…

Sergio Tapia
- 40,006
- 76
- 183
- 254
22
votes
5 answers
How to set value of property where there is no setter
I have seen various questions raised and answered where we can invoke a private setter using reflection such as this one:
Is it possible to get a property's private setter through reflection?
However I have some code which has a property i need to…

Luke De Feo
- 2,025
- 3
- 22
- 40
21
votes
3 answers
Expression-bodied properties vs. {get; set;}
When I have Visual Studio 2017 generate properties for me, it will always use the new expression-bodied properties, for example:
private static string username;
internal static string Username { get => username; set => username = value; }
Is there…

Max
- 207
- 1
- 6
- 20
18
votes
4 answers
In C#, how do you mix a default get with an explicit set?
I want to do something like this:
class Foo
{
bool Property
{
get;
set
{
notifySomethingOfTheChange();
// What should I put here to set the value?
}
}
}
Is there anything I can put…

Matt
- 21,026
- 18
- 63
- 115