In C#, is it necessary to lock when getting a non volatile property? I know we need to lock when setting the property. how about getting?
Now 3.0 provide automatic property, is it thread safe itself?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, automatic properties are not thread-safe. They are nothing more than syntactic sugar; the compiler automatically generates the private backing fields, just as if you’d written them out manually.
However, unless your application is accessing properties from multiple threads, there’s no reason to worry about this in the first place. It’s hard to tell from your question if your app is multi-threaded.