Depending on your implementation and needs of this program you may want to look into the
Color
struct
it represents colors and there is aColors
class
which has many predefined colors created already. This would be usedful if your program is a visual program and you have to render the specified color on screen. Otherwise astring
is just fine.To elaborate more on Jeff Vanzella Jeff Vanzella's answer answer when he said
As for your question of having a better way: the only other way to set them is to make the property's set method public and set them through that.
This is of-course changing your program in a way that you really may not want, but it is good to know about.
public class Truck { public int Wheels { get; set; } public string Color { get; set; } public bool Loaded { get; set; } }
...Somewhere else, when declaring Truck
var truck = new Truck() { Wheels = 4, Color = "Blue", Loaded = true };
or
var truck = new Truck(); truck.Wheels = 4; truck.Color = "Blue"; truck.Loaded = true;
the above two method do the exact same thing, the former is just shorthand for the latter
Edit: Added point #3
You don't need to say
this._wheels = wheels
, in your case there are no variables in your constructor that conflict with_wheels
so you can just say_wheels = wheels;
In-fact that is partially the reasonprivate
class
scoped variables are often named with an underscore prefix. In some conventions, that underscore prefix isn't used, so theclass
scopedprivate
members are named something likewheels
, and then in the constructor you do have to saythis.wheels = wheels;
, the former referring to theclass
levelwheels
, and the latter referring to the constructor levelwheels
. This is confusing when people do that, which is why I prefer to do it the way you did it, and have all my privateclass
level members prefixed with an underscore. Note that I did specifyprivate
, if a variable ispublic
is should be Capitalized, with no underscores in the beginning(preferably none in the rest of the name), and a property, meaning it has the{get; set;}
or the more lengthy getters and setters you used in the OP.
Depending on your implementation and needs of this program you may want to look into the
Color
struct
it represents colors and there is aColors
class
which has many predefined colors created already. This would be usedful if your program is a visual program and you have to render the specified color on screen. Otherwise astring
is just fine.To elaborate more on Jeff Vanzella's answer when he said
As for your question of having a better way: the only other way to set them is to make the property's set method public and set them through that.
This is of-course changing your program in a way that you really may not want, but it is good to know about.
public class Truck { public int Wheels { get; set; } public string Color { get; set; } public bool Loaded { get; set; } }
...Somewhere else, when declaring Truck
var truck = new Truck() { Wheels = 4, Color = "Blue", Loaded = true };
or
var truck = new Truck(); truck.Wheels = 4; truck.Color = "Blue"; truck.Loaded = true;
the above two method do the exact same thing, the former is just shorthand for the latter
Edit: Added point #3
You don't need to say
this._wheels = wheels
, in your case there are no variables in your constructor that conflict with_wheels
so you can just say_wheels = wheels;
In-fact that is partially the reasonprivate
class
scoped variables are often named with an underscore prefix. In some conventions, that underscore prefix isn't used, so theclass
scopedprivate
members are named something likewheels
, and then in the constructor you do have to saythis.wheels = wheels;
, the former referring to theclass
levelwheels
, and the latter referring to the constructor levelwheels
. This is confusing when people do that, which is why I prefer to do it the way you did it, and have all my privateclass
level members prefixed with an underscore. Note that I did specifyprivate
, if a variable ispublic
is should be Capitalized, with no underscores in the beginning(preferably none in the rest of the name), and a property, meaning it has the{get; set;}
or the more lengthy getters and setters you used in the OP.
Depending on your implementation and needs of this program you may want to look into the
Color
struct
it represents colors and there is aColors
class
which has many predefined colors created already. This would be usedful if your program is a visual program and you have to render the specified color on screen. Otherwise astring
is just fine.To elaborate more on Jeff Vanzella's answer when he said
As for your question of having a better way: the only other way to set them is to make the property's set method public and set them through that.
This is of-course changing your program in a way that you really may not want, but it is good to know about.
public class Truck { public int Wheels { get; set; } public string Color { get; set; } public bool Loaded { get; set; } }
...Somewhere else, when declaring Truck
var truck = new Truck() { Wheels = 4, Color = "Blue", Loaded = true };
or
var truck = new Truck(); truck.Wheels = 4; truck.Color = "Blue"; truck.Loaded = true;
the above two method do the exact same thing, the former is just shorthand for the latter
Edit: Added point #3
You don't need to say
this._wheels = wheels
, in your case there are no variables in your constructor that conflict with_wheels
so you can just say_wheels = wheels;
In-fact that is partially the reasonprivate
class
scoped variables are often named with an underscore prefix. In some conventions, that underscore prefix isn't used, so theclass
scopedprivate
members are named something likewheels
, and then in the constructor you do have to saythis.wheels = wheels;
, the former referring to theclass
levelwheels
, and the latter referring to the constructor levelwheels
. This is confusing when people do that, which is why I prefer to do it the way you did it, and have all my privateclass
level members prefixed with an underscore. Note that I did specifyprivate
, if a variable ispublic
is should be Capitalized, with no underscores in the beginning(preferably none in the rest of the name), and a property, meaning it has the{get; set;}
or the more lengthy getters and setters you used in the OP.
Depending on your implementation and needs of this program you may want to look into the
Color
struct
it represents colors and there is aColors
class
which has many predefined colors created already. This would be usedful if your program is a visual program and you have to render the specified color on screen. Otherwise astring
is just fine.To elaborate more on Jeff Vanzella's answer when he said
As for your question of having a better way: the only other way to set them is to make the property's set method public and set them through that.
This is of-course changing your program in a way that you really may not want, but it is good to know about.
public class Truck { public int Wheels { get; set; } public string Color { get; set; } public bool Loaded { get; set; } }
...Somewhere else, when declaring Truck
var truck = new Truck() { Wheels = 4, Color = "Blue", Loaded = true };
or
var truck = new Truck(); truck.Wheels = 4; truck.Color = "Blue"; truck.Loaded = true;
the above two method do the exact same thing, the former is just shorthand for the latter
Edit: Added point #3
You don't need to say
this._wheels = wheels
, in your case there are no variables in your constructor that conflict with_wheels
so you can just say_wheels = wheels;
In-fact that is partially the reasonprivate
class
scoped variables are often named with an underscore prefix. In some conventions, that underscore prefix isn't used, so theclass
scopedprivate
members are named something likewheels
, and then in the constructor you do have to saythis.wheels = wheels;
, the former referring to theclass
levelwheels
, and the latter referring to the constructor levelwheels
. This is confusing when people do that, which is why I prefer to do it the way you did it, and have all my privateclass
level members prefixed with an underscore. Note that I did specifyprivate
, if a variable ispublic
is should be Capitalized, with no underscores in the beginning(preferably none in the rest of the name), and a property, meaning it has the{get; set;}
or the more lengthy getters and setters you used in the OP.
Depending on your implementation and needs of this program you may want to look into the
Color
struct
it represents colors and there is aColors
class
which has many predefined colors created already. This would be usedful if your program is a visual program and you have to render the specified color on screen. Otherwise astring
is just fine.To elaborate more on Jeff Vanzella's answer when he said
As for your question of having a better way: the only other way to set them is to make the property's set method public and set them through that.
This is of-course changing your program in a way that you really may not want, but it is good to know about.
public class Truck { public int Wheels { get; set; } public string Color { get; set; } public bool Loaded { get; set; } }
...Somewhere else, when declaring Truck
var truck = new Truck() { Wheels = 4, Color = "Blue", Loaded = true };
or
var truck = new Truck(); truck.Wheels = 4; truck.Color = "Blue"; truck.Loaded = true;
the above two method do the exact same thing, the former is just shorthand for the latter
Depending on your implementation and needs of this program you may want to look into the
Color
struct
it represents colors and there is aColors
class
which has many predefined colors created already. This would be usedful if your program is a visual program and you have to render the specified color on screen. Otherwise astring
is just fine.To elaborate more on Jeff Vanzella's answer when he said
As for your question of having a better way: the only other way to set them is to make the property's set method public and set them through that.
This is of-course changing your program in a way that you really may not want, but it is good to know about.
public class Truck { public int Wheels { get; set; } public string Color { get; set; } public bool Loaded { get; set; } }
...Somewhere else, when declaring Truck
var truck = new Truck() { Wheels = 4, Color = "Blue", Loaded = true };
or
var truck = new Truck(); truck.Wheels = 4; truck.Color = "Blue"; truck.Loaded = true;
the above two method do the exact same thing, the former is just shorthand for the latter
Edit: Added point #3
You don't need to say
this._wheels = wheels
, in your case there are no variables in your constructor that conflict with_wheels
so you can just say_wheels = wheels;
In-fact that is partially the reasonprivate
class
scoped variables are often named with an underscore prefix. In some conventions, that underscore prefix isn't used, so theclass
scopedprivate
members are named something likewheels
, and then in the constructor you do have to saythis.wheels = wheels;
, the former referring to theclass
levelwheels
, and the latter referring to the constructor levelwheels
. This is confusing when people do that, which is why I prefer to do it the way you did it, and have all my privateclass
level members prefixed with an underscore. Note that I did specifyprivate
, if a variable ispublic
is should be Capitalized, with no underscores in the beginning(preferably none in the rest of the name), and a property, meaning it has the{get; set;}
or the more lengthy getters and setters you used in the OP.
Depending on your implementation and needs of this program you may want to look into the
Color
struct
it represents colors and there is aColors
class
which has many predefined colors created already. This would be usedful if your program is a visual program and you have to render the specified color on screen. Otherwise astring
is just fine.To elaborate more on Jeff Vanzella's answer when he said
As for your question of having a better way: the only other way to set them is to make the property's set method public and set them through that.
This is of-course changing your program in a way that you really may not want, but it is good to know about.
public class Truck { public int Wheels { get; set; } public string Color { get; set; } public bool Loaded { get; set; } }
...Somewhere else, when declaring Truck
var truck = new Truck() { Wheels = 4, Color = "Blue", Loaded = true };
or
var truck = new Truck(); truck.Wheels = 4; truck.Color = "Blue"; truck.Loaded = true;
the above two method do the exact same thing, the former is just shorthand for the latter