Questions tagged [automatic-properties]
214 questions
2072
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;…
![](../../users/profiles/946.webp)
bentford
- 30,501
- 7
- 56
- 57
371
votes
12 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…
![](../../users/profiles/8677.webp)
I. J. Kennedy
- 21,946
- 16
- 59
- 87
204
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…
![](../../users/profiles/459.webp)
dlamblin
- 40,676
- 19
- 92
- 127
156
votes
18 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…
![](../../users/profiles/91.webp)
Michael Stum
- 167,397
- 108
- 388
- 523
142
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…
![](../../users/profiles/47432.webp)
p4bl0
- 4,825
- 5
- 22
- 25
105
votes
12 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
{
…
![](../../users/profiles/146828.webp)
ctorx
- 6,373
- 7
- 36
- 52
76
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!
![](../../users/profiles/753540.webp)
dennis
- 907
- 1
- 7
- 7
68
votes
3 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…
![](../../users/profiles/350538.webp)
Tin Can
- 1,790
- 2
- 24
- 30
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 =…
![](../../users/profiles/10429.webp)
Mike Rosenblum
- 11,708
- 6
- 46
- 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;
…
![](../../users/profiles/6146.webp)
NerdFury
- 17,581
- 5
- 36
- 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…
![](../../users/profiles/66058.webp)
Gavin
- 15,943
- 18
- 60
- 106
50
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…
![](../../users/profiles/8954.webp)
Tamas Czinege
- 110,351
- 39
- 146
- 173
49
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?
![](../../users/profiles/38522.webp)
Ben Aston
- 45,997
- 54
- 176
- 303
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…
![](../../users/profiles/10727296.webp)
apfelstrudel24
- 442
- 3
- 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…
![](../../users/profiles/967484.webp)
Alex Stone
- 41,555
- 51
- 213
- 379