Possible Duplicates:
Why use getters and setters?
C# 3.0 Auto-Properties - useful or not?
Is there a difference between defining properties the following way -
// private, with getter & setter
private string fName;
public string Name
{
get { return this.fName }
set { this.fName = value }
}
// define as a Property
public string Name { get; set;}
As far as I can tell, it only looks like a stylistic preference. Am I missing something?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
Differences:
The second version is what's known as an automatically implemented property (or "automatic property" for short). They were introduced in C# 3. If you're only writing code which looks like the first version - i.e. there's no logic involved - then automatic properties are great. You can always add logic later by converting it into the first form. All your code will be compatible with that change, in both source and binary compatibility terms.
Be aware that in versions prior to C# 6, automatic properties don't allow you to specify default values, and there's no such thing as a genuinely readonly automatic property (i.e. one without a getter). The closest you can come is a public getter with a private setter, e.g.
public string Name { get; private set; }
It's not quite the same, but it's close enough in many situations.
The first is a standard property. You must define a field to store the value in. The second is an auto-implemented property, only availible in C# 3.0 and later.
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