Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C# auto-implemented static properties thread-safe?

Tags:

I would like to know if C# automatically implemented properties, like public static T Prop { get; set; }, are thread-safe or not. Thanks!

like image 509
Miguel Angelo Avatar asked Jan 15 '10 20:01

Miguel Angelo


People also ask

Is C+ Same as C?

C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language. C is (mostly) a subset of C++.

What does %c mean in C programming?

While it's an integer, the %c interprets its numeric value as a character value for display. For instance for the character a : If you used %d you'd get an integer, e.g., 97, the internal representation of the character a. vs. using %c to display the character ' a ' itself (if using ASCII)

Is there a C+?

C+ is a slightly above average grade on an assignment (usually within an educational context)... There is much debate on this topic... Low and High level languages: 1.

What is C language with example?

C language is a system programming language because it can be used to do low-level programming (for example driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C. It can't be used for internet programming like Java, .Net, PHP, etc.


2 Answers

Section 10.7.4 of the C# specification states:

When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field. The following example:

public class Point {   public int X { get; set; } // automatically implemented   public int Y { get; set; } // automatically implemented } 

is equivalent to the following declaration:

public class Point {   private int x;   private int y;   public int X { get { return x; } set { x = value; } }   public int Y { get { return y; } set { y = value; } } } 

That's what we promise, and that's what you get. The point of auto properties is to do the most basic, simple, cheap thing; if you want to do something fancier then you should write a "real" property.

like image 110
Eric Lippert Avatar answered Oct 02 '22 16:10

Eric Lippert


It appears not. This is the decompilation with Reflector:

private static string Test {     [CompilerGenerated]     get     {         return <Test>k__BackingField;     }     [CompilerGenerated]     set     {         <Test>k__BackingField = value;     } } 
like image 21
quentin-starin Avatar answered Oct 02 '22 14:10

quentin-starin