Performance differences between automatic propreties and normally declared propreties. True or False? [closed]

StackOverflow https://stackoverflow.com/questions/11471871

Question

Is there any difference between auto-implemented properties and manually implemented ones, from a performance point of view?

Was it helpful?

Solution

because as we know they are created at runtime

Auto-properties aren't created at runtime, they are generated at compile time. Very much like using, they are helpful syntactic sugar to reduce the amount of typing you need to do. They simply translate into code you would have written manually anyway.

There is no performance difference. Aside from the backing field name, the resulting code is the same as if you did it manually.

As @dasblinkenlight highlights, the backing name, being a "safe name" (as in, could never create it in C#) can cause serialization issues where the name cannot be handled correctly.

OTHER TIPS

There's no difference. Automatic properties are converted to normal properties at compile time. so this:

public int Prop { get; set; }

is made into something equivalent to this:

private int _PropField;

public int Prop {
    get { return _PropField; }
    set { _PropField = value; }
}

Auto properties are syntactical sugar means they are shorthand of writing properties

Taken from MSDN :

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field can only be accessed through the property's get and set accessors.

http://msdn.microsoft.com/en-us/library/bb384054(v=vs.90).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top