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? 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...
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...
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...
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...
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...
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.
...
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...
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...
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...
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...
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...
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...
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...
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...
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...
Can I rely on the fact that the underlying field to a property named Foo is called "k__BackingField" ?
...
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
...
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.
...
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? ...