Answered by:
Doubt on Auto-Properties in c# 6.0

Question
-
User269846090 posted
public string First { get; set; } = "Jane";
What is the use of this line?
Why I am asking this question is.... the following do the same..
public string First { get {return ="Jane"} set{somevariable = value} };
Tuesday, May 10, 2016 4:53 AM
Answers
-
User603616845 posted
Hi,
Doubt on Auto-Properties in c# 6.0The auto-property initializer allows assignment of properties directly within their declaration. For read-only properties, it takes care of all the ceremony required to ensure the property is immutable. Consider, for example, the FingerPrint class in this example:
public class FingerPrint { public DateTime TimeStamp { get; } = DateTime.UtcNow; public string User { get; } = System.Security.Principal.WindowsPrincipal.Current.Identity.Name; public string Process { get; } = System.Diagnostics.Process.GetCurrentProcess().ProcessName; }
You can take best answer from here.
Hope this will help you.
thanks
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 10, 2016 5:59 AM -
User-821857111 posted
public string First { get; set; } = "Jane";
What is the use of this line?
Why I am asking this question is.... the following do the same..
public string First { get {return ="Jane"} set{somevariable = value} };
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 10, 2016 6:13 AM
All replies
-
User603616845 posted
Hi,
Doubt on Auto-Properties in c# 6.0The auto-property initializer allows assignment of properties directly within their declaration. For read-only properties, it takes care of all the ceremony required to ensure the property is immutable. Consider, for example, the FingerPrint class in this example:
public class FingerPrint { public DateTime TimeStamp { get; } = DateTime.UtcNow; public string User { get; } = System.Security.Principal.WindowsPrincipal.Current.Identity.Name; public string Process { get; } = System.Diagnostics.Process.GetCurrentProcess().ProcessName; }
You can take best answer from here.
Hope this will help you.
thanks
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 10, 2016 5:59 AM -
User-821857111 posted
public string First { get; set; } = "Jane";
What is the use of this line?
Why I am asking this question is.... the following do the same..
public string First { get {return ="Jane"} set{somevariable = value} };
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 10, 2016 6:13 AM -
User269846090 posted
Hi Mikes....
I thought there may be some other usage but now I am clear. Thanks for your reply.
Tuesday, May 10, 2016 10:24 AM -
User303363814 posted
The statement "the following do the same" is not correct.
Your first example declares a string property which is initialised to "Jane".
The second example does not compile. That is the major difference.
Tuesday, May 10, 2016 11:05 AM -
User753101303 posted
Hi,
Depends what you expect the second sample would do. At best (fixing compile time errors) it seems it would ALWAYS return the "Jane" value.
The first exemple is just to ensure the *initial* value is "Jane" but you can then change this value whenever you want (and of course return this new value when reading the property).
Tuesday, May 10, 2016 11:23 AM