auto-properties

Skipping fields on model classes

Hello stackoverflow, Recently, after reading a lot of tutorials around the Internet, I've noticed that some developers skips writing the fields in their model classes and just go with properties, like this: public class MyClass { public string MyProperty { get; private set; } } What exactly is the benefit of doing this, apart fro...

How to prevent auto implemented properties from being serialized?

How can I prevent a auto implemented property from being serialized by the binary formatter? The [NonSerialized] attribute can only be used with fields. And the field is hidden when using auto implemented properties. ...

C# Auto Property - Is this 'pattern' best practice?

I seem to be using this sort of pattern in my code a lot , I know that it is not a simple Autoproperty any more as that would be: public IList<BCSFilter> BCSFilters { get; set; } The code I have been using is this: private IList<BCSFilter> _BCSFilters; /// <summary> /// Gets or sets the BCS filters. /// </summary> ...

How to set an autoproperty in the constructor of a struct?

Why is this valid public struct MyStruct { public MyStruct(double value) { myField = value; } private double myField; public double MyProperty { get { return myField; } set { myField = value; } } } and this is not public stru...

How can an auto-implemented property having different access levels be described by an interface?

This class property is what I'm trying to refactor into an interface. public class Stuff : IStuff { public int Number { get; protected internal set; } } Visual Studio 2008 refactoring tools extract the following interface // Visual Studio 2008's attempt is: public interface IStuff { int Number { get; } } ...

DefaultValue atttribute is not working with my Auto Property!!

Hello, I have the following Auto Property [DefaultValue(true)] public bool RetrieveAllInfo { get; set; } when I try to use it inside the code i find the default false for is false I assume this is the default value to a bool variable, does anyone have a clue what is wrong!? ...

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 inside my class in ways I normally would use a normal member field. The problem is that the pro...

Is implementing a singleton using an auto-property a good idea?

I recently found out about auto-properties and like them quite a lot. At this moment I am trying to use them everywhere where I can. Not really to just be able to use them everywhere, but more to see how well they work in most situations. Now I am making a singleton and thought:"Hey, let's try auto-properties here as well". public cla...

How to find out if a property is an auto-implemented property with reflection?

So in my case i am doing discovery of the structure of a class using reflection. I need to be able to find out if a property is an auto-implemented property by the PropertyInfo object. I assume that the reflection API does not expose such functionality because auto-properties are C# dependent, but is there any workaround to get this info...

Implementing Disposable pattern correctly - auto-implemented properties

One of the rules for implementing Dispose method says: "Throw an ObjectDisposedException from instance methods on this type (other than Dispose) when resources are already disposed. This rule does not apply to the Dispose method because it should be callable multiple times without throwing an exception." See: http://msdn.microsoft.com/...

Automatic transformation from getter/setter to properties

I have a big libray written in C++ and someone created an interface to use it in python (2.6) in an automatic way. Now I have a lot of classes with getter and setter methods. Really: I hate them. I want to reimplement the classes with a more pythonic interface using properties. The problem is that every class has hundreds of getters and...

Visual studio text editor "members" dropdown icons for auto implemented C# properties?

Is there any way (perhaps via add-in) that anyone knows to easily list or visualize the fields (and auto-properties) of a class in Visual Studio 2008 SP1 (standalone or with ReSharper 4.5)? I have been used to seeing fields of a class identified by the blue box/diamond icon in the Members dropdown in the Navigation bar of C# code tex...

Svn import with auto-props & pre-commit hook

My company's svn repo has a lot of MS Word docs in it. We've implemented a policy that all .doc files must have the svn:needs-lock property set to prevent parallel access on files that are hard to merge (we've also done this for xls, ppt, pdf etc.). We've implemented the policy by distributing a svn config with auto-props set appropriat...

Can't set breakpoints on an auto-property setter ? Why?

Apparently VS 2008 does not allow setting a breakpoint just on the setter of an auto-property. I.e. if I define an auto-property like this: public int CurrentFramesize { get; protected set; } and then try to set a breakpoint on the setter line, the whole auto-property turns breakpoint-red. This works j...

Quick create C# properties from variables

Hello, 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. ...

How to take advantage of an auto-property when refactoring this .Net 1.1 sample?

I see a lot of legacy .Net 1.1-style code at work like in example below, which I would like to shrink with the help of an auto-property. This will help many classes shrink by 30-40%, which I think would be good. public int MyIntThingy { get { return _myIntThingy; } set { _myIntThingy = value; } ...

In C# can I make auto-property perform some extra work with a help of an attribute?

This question is related but not the same as this: http://stackoverflow.com/questions/40730/how-do-you-give-a-c-auto-property-a-default-value I love auto-properties, but sometimes I have to do something like this: private string someName; public string SomeName { get { return someName; } set { someN...

VB.net automatic properties with different access level for set

In c# you can auto property a value with different level of access for the get and set . . . e.g. public String myString { get; private set; } Is there away to do that with automatic properties in vb.net or are you forced to go for the long winded implementation of properties? e.g. I don't want to do this all the time Dim _myStr...

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. Thanks David ...

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; namespace DataContracts { [Serializable] public class Class1 { private ...