Answered by:
Auto-Implemented Properties c#

Question
-
Hi all,
1. could someone explain me what's the idea behind using Auto-Implemented Properties c#?
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
}I get the motivation to use properties for private field, so we can determine how one can access a private field. But here - it's just like defining the field to be public from the first place. no?
2. Is there a difference between defining a field to be "public const" or define it to have a get-only property ?
Thanks
Elad
Answers
-
(1) The main reason is so that you can change it later to use a private field (for whatever reason - perhaps you need to add some validation, or you need to execute some code when the property changes).
(2.1) If you define a field to be "public const" then it will be "compiled-in" to code that uses it. This means that if you were to change the value in a class library, you would be forced to recompile all the assemblies that used that class library. If the value was a property (or if it was read-only) then you wouldn't be forced to recompile the dependent assemblies.
(2.2) Some things use reflection to get at properties, and would therefore treat const or readonly fields differently. An example is the Form editor.
- Marked as answer by elad2109 Tuesday, May 25, 2010 1:33 PM
-
Thanks a lot!
1. Don't fully understand. So why not using private field or public field for the first time?
If you say it's only for future changing - It doesn't save or server anything- so why no use it fully when it's needed?
Because changing from a public field to a property is a breaking change which will require all dependent assemblies to be recompiled.
All replies
-
(1) The main reason is so that you can change it later to use a private field (for whatever reason - perhaps you need to add some validation, or you need to execute some code when the property changes).
(2.1) If you define a field to be "public const" then it will be "compiled-in" to code that uses it. This means that if you were to change the value in a class library, you would be forced to recompile all the assemblies that used that class library. If the value was a property (or if it was read-only) then you wouldn't be forced to recompile the dependent assemblies.
(2.2) Some things use reflection to get at properties, and would therefore treat const or readonly fields differently. An example is the Form editor.
- Marked as answer by elad2109 Tuesday, May 25, 2010 1:33 PM
-
Hi,
Thank you for your nice post.
1. Actually, that is a short way with less coding, but 'fields' are not same as 'properties'. e.g. you can use 'properties' for binding but you can't use 'fields' for binding.
2. Yes, you can change the underlay value of get-only property inside of the class but you can't change a constant field. i.e.
class c { public const int constInt = 7;//unchangable private getOnlyPropertyUnderlayValue; public getOnlyProperty { get { return getOnlyPropertyUnderlayValue; } } public c() { getOnlyPropertyUnderlayValue = 7;//changable } }
Hope this helps.
Yasser
WHAT'S NEW IN THE .NET FRAMEWORK 4:
Article: Comparison of parallel and sequential computing in .NET Framework -
Using properties enables you to have private access to their set function for example. Furthermore, you can view information to the user that are not necessarily in the class. For example consider a class that keeps a length in meters in the class and with a property shows that value in feet. Another good practice is triggering when a property changes.
-
Hi,
Basic Idea behind the same is that you can also check if there are any validation and also some default value in the Set Property which is also called Write-Only Properties.
For Example:
private int _ID;
public int ID
{
get
{
return _ID;
}
set
{
if(value>0)
{
_ID = value;
}
else
{
_ID = 0;
}
}
Thanks,
Paras Sanghani
-
He wasn't asking what the point of properties is - he knows that ("I get the motivation to use properties for private field, so we can determine how one can access a private field.").
He was asking what the point of AUTO properties is, compared to public fields.
-
-
-
-
Thanks a lot!
1. Don't fully understand. So why not using private field or public field for the first time?
If you say it's only for future changing - It doesn't save or server anything- so why no use it fully when it's needed?
Because changing from a public field to a property is a breaking change which will require all dependent assemblies to be recompiled.