auto-properties

C# 3.0 Auto-Properties - useful or not?

I am used to create my Properties in C# using a private and a public field: private string title; public string Title { get { return title; } set { title = value; } } Now, with .net 3.0, we got auto-properties: public string Title { get; set; } I know this is more a philosophical/subjective questions, but is there any rea...

How do you give a C# Auto-Property a default value?

How do you give a C# Auto-Property a default value? I either use the constructor, or revert to the old syntax. Using the Constructor: class Person { public Person() { Name = "Default Name"; } public string Name { get; set; } } Using normal property syntax (with a default value) private string name = "Def...

C# 3.0 Auto-Properties - Is it possible to add custom behaviour?

I'd like to know if there is any way to add custom behaviour to the auto property get/set methods. An obvious case I can think of is wanting every set property method to call on any PropertyChanged event handlers as part of a System.ComponentModel.INotifyPropertyChanged implementation. This would allow a class to have numerous properti...

Learning about Auto-Implemented Properties

I have the simple class using auto-implemented properies: Public Class foo { public foo() { } public string BarName {get; set;} } I obviously use the variable BarName throughout my class and now need to add logic when the property value is set (it must be all upper case, go figure). Does this mean that I need to now crea...

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 seems convenient and concise, but how can I retain the initialization without adding a cons...

How to reset svn-properties according to new SVN config?

Hi, Recently I made a bunch of changes to my local svn config file. Mainly I corrected svn:mime-type properties of about 15 different file types. Now I need reset all previously checked in files according to this new configuration. SVN seems to trigger auto-prop only for ADDs and IMPORTs. So how do I do this without going individually a...

Why do automatic properties require both getters AND setters?

In C#, if I declare an auto-implemented property, why do I have to declare BOTH the get and set part? i.e. public string ThisWorks { get; set; } public string ThisDoesnt { get; } Isn't this just syntactic sugar - i.e. the compiler inserts a private field for the property? So why the problem? Curious. ...

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 = size; // <-- Legal assignment. } public int Size; } However, the following...

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 where he says "Attributes are not allowed on auto-implemented properties, so roll your own i...

Is there a way to get Visual Studio 2008 to stop formatting my AutoProperties?

In Visual Studio 2008's Options > Text Editor > C# > Formatting, I have the following settings ticked. Automatically format completed statement on ; Automatically format completed block on } This is really helpful for when I'm writing a method or a for/foreach/while/if statement. ie if I write void MyMethod(){} I want it to refo...

Is there a Reflector add-in or other tool that will handle auto properties?

Reflector shows this for auto properties: public string AddressLine1 { [CompilerGenerated] get { return this.<AddressLine1>k__BackingField; } [CompilerGenerated] set { this.<AddressLine1>k__BackingField = value; } } Is there any add-in or other tool that will convert it to: public strin...

Do you think "auto interface implementation" would be useful in .NET / C#

Consider this: public class interface Person : IPerson { int ID { get; protected set; } string FirstName { get; set; } string LastName { get; set; } string FullName { get { return FirstName + " " + LastName; } } } And this: public class StubPerson : IPerson { int ID { get { return 0; protected set { } } string FirstNa...

Can I create an automatic property (no private member) with get and set code?

In c#, I can do this: public int Foo { get; set; } Which is nice. But as soon as I want to do anything in the getter or setter, I have to change it to this: private int foo; public int Foo { get { return foo; } set { foo = value; DoSomething(); // all that other code, just to add this line! } } Is it po...

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 serialization engine (at least, by default) totally ignores automatic properties on deserializa...

How to customize Auto Properties in C# 3.0

Before C# 3.0 we done like this: class SampleClass { private int field; public int Property { get { return this.field } set { this.field = value } } } Now we do this: class SampleClass { public int Property { get; set; } } (Look ma! no fields!) Now if I want to customize the Getter or the Setter, the field must be explicit...

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 question is - how often do you see this abused? It seems like it has a high potential to v...

Is the implementation of Auto Properties in the spec?

Can I rely on the fact that the underlying field to a property named Foo is called "k__BackingField" ? ...

Multiline values in Subversions autoproperties possible?

Is it possible to create a property in the autoprops section of .subversion/config file which contains multiline values? So that it would look like: svn pg myprop will output 1st line of prop 2nd line of prop ...

Passing auto properties as ref

The C# compiler doesn't allow this. What's the reason for this? And what workaround I can use? Basically I need to swap some values around, but don't wanna have the same swapping code all over. ...

Auto Property that returns an interface

This is something curious that I saw in my coding today. Here is the sample code: public class SomeClass { public IUtils UtilitiesProperty { get; set; } } public interface IUtils { void DoSomething(); } public class Utils : IUtils { void DoSomething(); } This compiles fine. So what is UtilitiesProperty? Is it a Util? ...