I was checking the new features of .NET 3.5 and found that in C# 3.0, we can use
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
instead of
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
If i use the Automatic Properties,what will be the private variable name for Name ? the tutorials on internet says that compiler will automatically create a private variable.So how can i use /access the private variable,if i want to use it in a method in this class ?
As already said: You cannot access the automatically generated variable (without using bad tricks). But I assume you are asking that question because you want to have only a getter, but still want to use automatic properties ... right? In that case you can use this one:
public string FirstName { get; private set; }
Now you have a private setter and a public getter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With