Questions tagged [automatic-properties]

219 questions
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,822
  • 5
  • 33
  • 52
15
votes
3 answers

How do I write private set auto-properties in VB 10?

in C#: public string Property { get; private set; } in VB? Please vote or/and share your ideas!
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
15
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,895
  • 3
  • 31
  • 37
14
votes
2 answers

Explicit implementation of an interface using an automatic property

Is there any way to implement an interface explicitly using an automatic property? For instance, consider this code: namespace AutoProperties { interface IMyInterface { bool MyBoolOnlyGet { get; } } class MyClass :…
user181813
  • 1,861
  • 6
  • 24
  • 42
14
votes
3 answers

How to create an auto-property faster in Delphi IDE?

I need to create and manage many simple published properties. I call them auto-properties if they look like that: private FTitle: string; published property Title: string read FTitle write FTitle; Usually I create them next way: Adding…
Andrew
  • 3,696
  • 3
  • 40
  • 71
13
votes
6 answers

Using a private auto property instead of a simple variable for a programming standard

In a discussion with a peer, it was brought up that we should consider using auto properties for all class level variables... including private ones. So in addition to a public property like so: public int MyProperty1 { get; set; } Our private…
Biggert
  • 278
  • 4
  • 21
13
votes
3 answers

Code contracts on auto-implemented properties

Is there any way to put contracts on automatically implemented properties in .NET? (And how if the answer is 'Yes')? (I assume using .NET code contracts from DevLabs)
wh1t3cat1k
  • 3,146
  • 6
  • 32
  • 38
13
votes
1 answer

Resharper doesn't automatically convert to auto properties in Serializable classes - should I?

I ran across this issue today and was able to determine that, when doing code cleanup, R# will not convert properties from having backing fields to auto properties in classes that are decorated with the SerializableAttribute, e.g. using System;…
Matt Mills
  • 8,692
  • 6
  • 40
  • 64
13
votes
4 answers

C# 6 auto-properties - read once or every time?

I follow a pattern when setting certain properties whereby I check to see if the corresponding field is empty, returning the field if not and setting it if so. I frequently use this for reading configuration settings, for example, so that the…
08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
13
votes
3 answers

F# interfaces and properties

I'm trying to get a grip on F#, and in the process I am converting some C# code. I'm having some trouble with defining properties in an interface and implementing them in a type. Consider the following code: module File1 type IMyInterface = …
Eyvind
  • 5,221
  • 5
  • 40
  • 59
12
votes
4 answers

How to properly define class properties?

When defining a new class within a project what is the correct/best practice for doing so? In the past I have created classes such as: public class MyClass { public string FirstName {get; set;} public string LastName {get; set;} …
rlcrews
  • 3,482
  • 19
  • 66
  • 116
12
votes
2 answers

Adding Auto-Implemented Property to class using Roslyn

I'm trying to learn Roslyn by building an existing but simple application from the ground up, which seems to be a productive way to learn this. Anyhow, I have the following code: var root = (CompilationUnitSyntax)document.GetSyntaxRoot(); //…
Beaker
  • 2,804
  • 5
  • 33
  • 54
11
votes
1 answer

Auto-properties with or without backing field - preference?

I know that when using auto-properties, the compiler creates its own backing field behind the screen. However, in many programs I read to learn from, I see people explicitly write private int _backingField; public int Property { get { return…
Taelia
  • 591
  • 3
  • 20
11
votes
5 answers

Use of properties vs backing-field inside owner class

I love auto-implemented properties in C# but lately there's been this elephant standing in my cubicle and I don't know what to do with him. If I use auto-implemented properties (hereafter "aip") then I no longer have a private backing field to use…
mikesigs
  • 10,491
  • 3
  • 33
  • 40
11
votes
4 answers

C# 3.0 :Automatic Properties - what would be the name of private variable created by compiler

I was checking the new features of .NET 3.5 and found that in C# 3.0, we can use public class Person { public string FirstName { get; set; } public string LastName { get; set; } } instead of private string name; public string Name { …
Shyju
  • 214,206
  • 104
  • 411
  • 497
1 2
3
14 15