Questions tagged [automatic-properties]

214 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,039
  • 1
  • 10
  • 12
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
  • 6,971
  • 10
  • 52
  • 94
29
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,441
  • 3
  • 18
  • 25
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…
27
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
  • 293
  • 3
  • 6
25
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…
colmde
  • 2,852
  • 1
  • 19
  • 42
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…
Vasya
  • 5,509
  • 7
  • 31
  • 46
24
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
  • 36,466
  • 70
  • 175
  • 253
24
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
  • 22,641
  • 14
  • 53
  • 80
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
  • 12,823
  • 19
  • 79
  • 132
23
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,256
  • 8
  • 36
  • 74
22
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
  • 8,883
  • 3
  • 53
  • 50
19
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
  • 327
  • 1
  • 4
  • 16
16
votes
3 answers

Auto-implemented properties with non null guard clause?

I do agree with Mark Seeman's notion that Automatic Properties are somewhat evil as they break encapsulation. However I do like the concise syntax, readability and convenience they bring. I quote: public string Name { get; set; } The problem with…
Can Gencer
  • 8,424
  • 5
  • 29
  • 51
16
votes
6 answers

What's the point of an auto property?

This might sound naive, but... class Widget { public int Foo { get; set; } } That's cool, and saves some boilerplate against using a backing field, but at that point, isn't it equivalent to simply: class Widget { public int Foo; } Seems…
Cheezmeister
  • 4,731
  • 3
  • 27
  • 37
1
2
3
14 15