Questions tagged [automatic-properties]

219 questions
8
votes
3 answers

No ivars -> What am I missing?

I never use ivars. I only use properties -- sometimes assign properties with primitive types, and sometimes on a "private" class extension. I've seen the advantages of not using ivars in switching to ARC -- I have some borrowed code with lots of…
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
8
votes
3 answers

How to default svn:keywords to enabled?

Is there a way to enable svn:keywords by default so that this property does not need to be turned on for each keyword every time a new source file is added?
jacknad
  • 13,483
  • 40
  • 124
  • 194
8
votes
1 answer

Why does ReSharper need to scan all files when converting a property to an auto property?

Is there any difference between accessing a property that has a backing field private int _id; public int Id { get { return _id; } set { _id = value; } } versus an auto-property? public int Id { get; set; } The…
mflodin
  • 1,093
  • 1
  • 12
  • 22
8
votes
5 answers

How can I put validation in the getter and setter methods in C#?

In C#, I can have a property without having the need to declare a private variable. My VB6 code that looked like this 'local variable(s) to hold property value(s) Private mvarPhoneNumber As String 'local copy Public Property Let PhoneNumber(ByVal…
abhi
  • 3,082
  • 6
  • 47
  • 73
8
votes
2 answers

Read-Only Auto-Property for Simple Types: Initializer VS Expression Body Getter

In C# 6.0, the new syntax let us write read-only auto-properties with using an initializer: public bool AllowsDuplicates { get; } = true; Likewise, we can write it using an expression body getter: public bool AllowsDuplicates => true; For simple…
Mikkel R. Lund
  • 2,336
  • 1
  • 31
  • 44
8
votes
3 answers

c# constructors vs auto-properties and object initializers

I have used auto properties a lot but I have gone more and more away from that setting up classes with readonly backing fields initialized in the constructor. I remove all setters and only add the back if the property clearly need a setter. I find…
terjetyl
  • 9,497
  • 4
  • 54
  • 72
8
votes
2 answers

Purpose of automatic properties in .NET

Why is this: public string Foo {get;set;} considered better than this: public string Foo; I can't for the life of me work it out. Can anyone shed some light? Thanks
David
  • 15,750
  • 22
  • 90
  • 150
8
votes
1 answer

How to disable automatic id creation in Django?

I want to disable automatic id creation in Django Models. Is it possible to do so? How?
8
votes
4 answers

Why do C# automatic properties not support default values like VB 2010?

Looking at the new VB 2010 features, I stumbled upon support for Auto-Implemented Properties. Since I'm working with C#, this seemed quite familiar, but I noticed that VB did add a feature I would love to have in C#: setting an arbitrary default…
Rob van Groenewoud
  • 1,824
  • 1
  • 13
  • 17
7
votes
5 answers

Do auto-implemented properties support attributes?

I was told that in c# attributes are not allowed on the auto-implemented properties. Is that true? if so why? EDIT: I got this information from a popular book on LINQ and could not believe it! EDIT: Refer page 34 of LINQ Unleashed by Paul Kimmel…
suhair
  • 10,895
  • 11
  • 52
  • 63
7
votes
8 answers

Quick create C# properties from variables

For C#, I hate writing out the variables and then writing out all the properties. Isn't there a way to select all variables, right click and create all the properties.
user204588
  • 1,613
  • 4
  • 31
  • 50
7
votes
4 answers

Is always prefixing (auto)properties with the this-keyword considered a good practice?

Ever since I found out about auto properties, I try to use them everywhere. Before there would always be a private member for every property I had that I would use inside the class. Now this is replaced by the auto property. I use the property…
Matthijs Wessels
  • 6,530
  • 8
  • 60
  • 103
7
votes
2 answers

Generate custom setter using attributes

In classes whose instances I persist using an object database, I keep having to do this: private string _name; public string Name { get { return this._name; } set { _name = value; this.Save(); } } whereas I would much rather type…
andyhasit
  • 14,137
  • 7
  • 49
  • 51
7
votes
4 answers

How often do you see abuse of C# shorthand getters/setters?

In C# you can create getter/setters in a simpler way than other languages: public int FooBar { get; set; } This creates an internal private variable which you can't address directly, with the external property 'FooBar' to access it directly. My…
dreadwail
  • 15,098
  • 21
  • 65
  • 96
7
votes
2 answers

Setting a private setter using an object initializer

Why is it possible to use an object initializer to set a private set auto property, when the initializer is called from within the class which owns the auto property? I have included two class as an example. public class MyClass { public string…