Questions tagged [automatic-properties]
219 questions
2309
votes
23 answers
What is the best way to give a C# auto-property an initial value?
How do you give a C# auto-property an initial value?
I either use the constructor, or revert to the old syntax.
Using the Constructor:
class Person
{
public Person()
{
Name = "Initial Name";
}
public string Name { get;…

bentford
- 33,038
- 7
- 61
- 57
406
votes
14 answers
Public Fields versus Automatic Properties
We're often told we should protect encapsulation by making getter and setter methods (properties in C#) for class fields, instead of exposing the fields to the outside world.
But there are many times when a field is just there to hold a value and…

I. J. Kennedy
- 24,725
- 16
- 62
- 87
216
votes
4 answers
Initializing C# auto-properties
I'm used to writing classes like this:
public class foo {
private string mBar = "bar";
public string Bar {
get { return mBar; }
set { mBar = value; }
}
//... other methods, no constructor ...
}
Converting Bar to an auto-property…

dlamblin
- 43,965
- 20
- 101
- 140
157
votes
17 answers
C# 3.0 auto-properties — useful or not?
Note: This was posted when I was starting out C#. With 2014 knowledge, I can truly say that auto-properties are among the best things that ever happened to the C# language.
I am used to create my properties in C# using a private and a public…

Michael Stum
- 177,530
- 117
- 400
- 535
143
votes
10 answers
Difference between Property and Field in C# 3.0+
I realize that it seems to be a duplicate of What is the difference between a Field and a Property in C#? but my question has a slight difference (from my point of view):
Once I know that
I will not use my class with "techniques that only works on…

p4bl0
- 5,354
- 5
- 24
- 25
125
votes
14 answers
C# Lazy Loaded Automatic Properties
In C#,
Is there a way to turn an automatic property into a lazy loaded automatic property with a specified default value?
Essentially, I am trying to turn this...
private string _SomeVariable
public string SomeVariable
{
get
{
…

ctorx
- 6,841
- 8
- 39
- 53
92
votes
11 answers
What are Automatic Properties in C# and what is their purpose?
Could someone provide a very simple explanation of Automatic Properties in C#, their purpose, and maybe some examples? Try to keep things in layman's terms, please!

dennis
- 1,077
- 1
- 8
- 7
82
votes
4 answers
C# Custom getter/setter without private variable
I learned c# recently, so when I learned to write properties, I was taught to do it like this:
public string Name { get; set; }
Auto properties are great! But now I'm trying to do something a little more complicated, so I need to write a custom…

Tin Can
- 2,540
- 2
- 29
- 39
64
votes
3 answers
Automatic Properties and Structures Don't Mix?
Kicking around some small structures while answering this post, I came across the following unexpectedly:
The following structure, using an int field is perfectly legal:
struct MyStruct
{
public MyStruct ( int size )
{
this.Size =…

Mike Rosenblum
- 12,027
- 6
- 48
- 64
54
votes
2 answers
Why is it necessary to call :this() on a struct to use automatic properties in c#?
If I define a struct in C# using automatic properties like this:
public struct Address
{
public Address(string line1, string line2, string city, string state, string zip)
{
Line1 = line1;
Line2 = line2;
City = city;
…

NerdFury
- 18,876
- 5
- 38
- 41
53
votes
11 answers
C# Automatic Properties
I'm a bit confused on the point of Automatic properties in C# e.g
public string Forename{ get; set; }
I get that you are saving code by not having to declare a private variable, but what's the point of a property when you are not using any get or…

Gavin
- 17,053
- 19
- 64
- 110
51
votes
4 answers
C# automatic property deserialization of JSON
I need to deserialize some JavaScript object represented in JSON to an appropriate C# class. Given the nice features of automatic properties, I would prefer having them in these classes as opposed to just having fields. Unfortunately, the .NET…

Tamas Czinege
- 118,853
- 40
- 150
- 176
51
votes
9 answers
C# Automatic Properties - Why Do I Have To Write "get; set;"?
If both get and set are compulsory in C# automatic properties, why do I have to bother specifying "get; set;" at all?

Ben Aston
- 53,718
- 65
- 205
- 331
46
votes
2 answers
Why does an overridden get-only property stay null when set in base class constructor?
I tried the following example:
public class TestBase
{
public virtual string ReadOnly { get; }
public TestBase()
{
ReadOnly = "from base";
}
}
class Test : TestBase
{
public override string ReadOnly { get; }
public…

apfelstrudel24
- 442
- 4
- 8
40
votes
3 answers
xCode 6 how to fix "Use of undeclared identifier" for automatic property synthesis?
I'm using xCode6 Beta 3, and am running into an issue where a code which previously compiled fine (xCode 5.1.1 or xCode6 beta 2) suddenly started to give me "Use of undeclared identifier" errors when accessing an automatically synthesized instance…

Alex Stone
- 46,408
- 55
- 231
- 407