1

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.

  1. 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)
  • Seems to be working for me: https://tio.run/##fZJNT8MwDIbPy68wO3VV962dChyAw5CGhITEDohDtoUuUpZMSboxVf3txU3TtWyISD3Udl4/fuO16a@VZkWRGi4TeDsZy3YxWQtqDLxqlWi6g4x0jKWWr@Gg@AZeKJeBsRovfHwC1YnpYcmD@oYVfncg2RHwL5jMovEogumsFxPMDBZMJnaLBZNRTEin86ikUYINlppbtuCSBd05E0JFsFRabG4gG@XdCJqrqNPJSU48HvYgGQE8e80P1DLg0oJwpfFVfMt4srXX8SPf/FV@UCLdMeR0iXQlcPpyqKZF1JKNGqmeu1FxlcduuRmIevQ23TlbaWC2zXjOOlVMtkDRgjZX2dubm0HCUOm@bgSm/YsqBypSFkMOwyHc9pnWSl9KzSscJ@UEsPyyZumg/i15dw56ogw0s6mWtXjoFUIPXhIZV1cZ3ybNfw3rVvCJm72gp2f5pYJLw/9YK28ON@VKAZUb77SLjKtIZXMZmCCKQu9ZzVIGp@Uq1g9fP7q7EzXI3uWwlg/9SpxfLS@KHw – The Lyrist Jun 19 '18 at 21:02
  • You don't need a private `length` field in your current example, just like `height` and `width`, you can remove the private fields and just use auto-properties with `PropertyName { get; set; }` – Rufus L Jun 19 '18 at 21:07
  • You also probably shouldn't have a `set` for `Volume`, since it's a calculated field. If someone changed `Volume`, how would you adjust the other size properties? – Rufus L Jun 19 '18 at 21:10
  • Possible duplicate of [Why am I getting this compilation error in my abstract base class?](https://stackoverflow.com/questions/34161885/why-am-i-getting-this-compilation-error-in-my-abstract-base-class) – Camilo Terevinto Jun 19 '18 at 21:13
  • 3
    for `Volume`, you might just mean `public int Volume => Height * Width * Length;` – Marc Gravell Jun 19 '18 at 21:17
  • @RufusL , I get that. And he did say those fields were no longer necessary, I am interested in the methodology. – Tony Cossio Jun 19 '18 at 21:21
  • @MarcGravell thanks, that actually works a lot better. – Tony Cossio Jun 19 '18 at 21:24
  • 1
    @TheLyrist the error box states (field) int Box.length Only assignment, call, decrement, and new object expressions can be used as a statement EDIT: I was using C#6. need to upgrade. – Tony Cossio Jun 19 '18 at 21:29

1 Answers1

4

You can use the => expression-bodied member syntax as a shortcut for read-only properties in C# 6.0 (you can't use them with a set) and in C# 7.0 they were expanded to include set accessors as you have in your code (which require backing fields, also as you have).

Most likely you're on C#6 so you're getting an error on the set syntax.

You asked how to shorten your code, and since you don't have a need for a private backing member (you aren't modifying the value in the set or get accessors), it would be shortest to get rid of them and just use auto-implemented properties for the ones that the user can set. Then you can use => for the Volume property, since it should be read-only (because it's a calculated field):

I believe this is the shortest code for the class you've described:

class Box
{
    public int Length { get; set; }
    public int Height { get; set; }
    public int Width { get; set; }
    public int Volume => Height * Width * Length;

    public Box(int length, int height, int width)
    {
        Length = length;
        Height = height;
        Width = width;
    }

    public void DisplayInfo()
    {
        Console.WriteLine("Length = {0}, Height = {1}, Width = {2}, Volume = {3}", 
            Length, Height, Width, Volume);
    }

}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Neat, this makes the code much simpler. thanks. EDIT: Another user did mention I was using the older version, which was the reason why I got the initial error. But he was downvoted and deleted his answer. – Tony Cossio Jun 19 '18 at 21:30