I'm watching an instructional video on C# and they showed a shortcut (type "prop", tab twice) it generates this
public int Height { get; set; }
So then he went on a shortcut about using => instead of this. It tried to combine the two but got an error in Length:
class Box
{
private int length;
private int height;
private int width;
private int volume;
public Box(int length, int height, int width)
{
this.length = length;
this.height = height;
this.width = width;
}
public int Length { get => length; set => length = value; } // <-error
public int Height { get; set; }
public int Width { get; set; }
public int Volume { get { return Height * Width * Length; } set { volume = value; } }
public void DisplayInfo()
{
Console.WriteLine("Length is {0} and height is {1} and width is {2} so the volume is {3}", length, height, width, volume = length * height * width);
}
}
Volume works fine, but I was interested in seeing if I can shorten the code like I'm trying to do with Length.
- What am I doing wrong, can it be done that way? 2. Is there a shorter was to set properties (am I on the right track)