Selecting the correct code
-
I have been studying C#. When writing C# code I seem to have three types of code I could use for variables but are not certain when I need to use the code. I could type this Code One
public string Name
{
get;
set;
}or I could type Code Two
public string Name
{
get
{
return name;
}
set
{
name = value;
}or I could type Code Three
public void SetLength (double len)
{
length = len
}And sometimes Code One and Code Three are used. I think that one of these codes are used if I make the Variables private instead of public. Any help would be welcome thanks Brian
-
I have been studying C#. When writing C# code I seem to have three types of code I could use for variables but are not certain when I need to use the code. I could type this Code One
public string Name
{
get;
set;
}or I could type Code Two
public string Name
{
get
{
return name;
}
set
{
name = value;
}or I could type Code Three
public void SetLength (double len)
{
length = len
}And sometimes Code One and Code Three are used. I think that one of these codes are used if I make the Variables private instead of public. Any help would be welcome thanks Brian
I'd like to suggest you do some study of what Fields, and Properties, are in C#. Note that in your examples above you never declare variables 'name and 'length. Start here: [^], [^], [^]. And, listen to Jon Skeet: [^] Properties are actually a form of Method, technically called an "accessor." Before auto-properties came along ... in C# 3.0 ... ('set and 'get only), this was a canonical pattern for Properties:
private int _mInt; // private backing store
public int MInt // public property
{
get { return _mInt; } set { _mInt = value; }
}Now, when you write:
public int MInt { get; set; }
The private backing field is created behind the scenes. These two examples are, functionally, identical. Some basic research and experimenting, now, will really benefit you.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
I'd like to suggest you do some study of what Fields, and Properties, are in C#. Note that in your examples above you never declare variables 'name and 'length. Start here: [^], [^], [^]. And, listen to Jon Skeet: [^] Properties are actually a form of Method, technically called an "accessor." Before auto-properties came along ... in C# 3.0 ... ('set and 'get only), this was a canonical pattern for Properties:
private int _mInt; // private backing store
public int MInt // public property
{
get { return _mInt; } set { _mInt = value; }
}Now, when you write:
public int MInt { get; set; }
The private backing field is created behind the scenes. These two examples are, functionally, identical. Some basic research and experimenting, now, will really benefit you.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
Thanks Bill. I'm studying some code examples. Thanks for letting me know about the upgrade in C# causes things to happen in the background. Maybe some code examples are for the older version of C#. In your reply you said Start here: [^], [^], [^]. And, listen to Jon Skeet: [^] I'm, not certain if there was a site address that you wanted me to go to? Brian
-
Thanks Bill. I'm studying some code examples. Thanks for letting me know about the upgrade in C# causes things to happen in the background. Maybe some code examples are for the older version of C#. In your reply you said Start here: [^], [^], [^]. And, listen to Jon Skeet: [^] I'm, not certain if there was a site address that you wanted me to go to? Brian
Each of those ^ links should take you to relevant content ... if they're broken, let me know. May I suggest re-framing your concern from "Selecting the correct code" to getting a good grasp of C# language fundamentals so that, in the future, you can make wise choices about what semantics and structures to use ? :) You have the possibility of 'static variables, as well, and, the options for access: 'public, 'private, 'protected, 'internal. C# is a very rich, and deep, language: for better, and worse, there are multiple ways to implement many fundamental structures, and relationships. cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
Each of those ^ links should take you to relevant content ... if they're broken, let me know. May I suggest re-framing your concern from "Selecting the correct code" to getting a good grasp of C# language fundamentals so that, in the future, you can make wise choices about what semantics and structures to use ? :) You have the possibility of 'static variables, as well, and, the options for access: 'public, 'private, 'protected, 'internal. C# is a very rich, and deep, language: for better, and worse, there are multiple ways to implement many fundamental structures, and relationships. cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
Hi Bill. I'm trying to get a good understanding of C# and these are some of the things that beginners to C$ have trouble with but I'm hoping that the more I study the code and try writing some small programs of my own then everything should fall into place. I'm more use to using Basic (in the past) which had global variables. Brian
-
Each of those ^ links should take you to relevant content ... if they're broken, let me know. May I suggest re-framing your concern from "Selecting the correct code" to getting a good grasp of C# language fundamentals so that, in the future, you can make wise choices about what semantics and structures to use ? :) You have the possibility of 'static variables, as well, and, the options for access: 'public, 'private, 'protected, 'internal. C# is a very rich, and deep, language: for better, and worse, there are multiple ways to implement many fundamental structures, and relationships. cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
Thanks Bill for the useful links. Lots of useful information at these links. Brian
-
I have been studying C#. When writing C# code I seem to have three types of code I could use for variables but are not certain when I need to use the code. I could type this Code One
public string Name
{
get;
set;
}or I could type Code Two
public string Name
{
get
{
return name;
}
set
{
name = value;
}or I could type Code Three
public void SetLength (double len)
{
length = len
}And sometimes Code One and Code Three are used. I think that one of these codes are used if I make the Variables private instead of public. Any help would be welcome thanks Brian
When you use <pre> tags, please make sure you select the correct language from the dropdown list that appears when you click the code button above the edit box. And also make sure that the checkbox marked "Treat my content as plain text, not as HTML" below the edit box, is unchecked.
-
Hi Bill. I'm trying to get a good understanding of C# and these are some of the things that beginners to C$ have trouble with but I'm hoping that the more I study the code and try writing some small programs of my own then everything should fall into place. I'm more use to using Basic (in the past) which had global variables. Brian
A combination of study, and coding small-scale examples that use the information and concepts you are studying, is an excellent way to learn. You can search CodeProject to find books that I, and other people, have recommended. This one is free (in English): [^] ... [^]
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot