Programmer7
Programmer7

Reputation: 159

C# Objects and Constructors best practices

namespace Area
{
    public class Rectangle
    {
        private double length;
        private double width;

    public Rectangle() { }

    public Rectangle(double length, double width)
    {
        this.Length = length;
        this.Width = width;
    }

    public double Length
    {
        get
        {
            return length;
        }

        set
        {
            length = value;
        }
    }

    public double Width
    {
        get
        {
            return width;
        }

        set
        {
            width = value;
        }
    }

    public double getArea()
    {
        return width * length;
    }

    public double getPerimeter()
    {
        return 2 * width + 2 * length;
    }

    public double getDiagonal()
    {
        return Math.Sqrt(Math.Pow(width, 2) + Math.Pow(length, 2));
    }

I want to make sure I am using best practices with C# Objects. Please use the above example for reference.

1. Is it necessary that I type the first empty Constructor? In class the Instructor always did on each program but never really gave an answer as to why.

public Rectangle() { }

2. Inside my Custom Constructor Visual Studio generates it like this: this.Length = length;

I know that the "this" keyword is not necessary the way it is typed, but in class the instructor sometimes changed it to lowercase like this:

this.length = length;

But sometimes he didn't change it. Which way is best practices?

And is the left side the actual Property? And then the right side is the field?

So it is, Property equals field?

3. And finally, in this case cant I just type my properties as:

public string Length { get; set; }

instead of the way Visual Studio generates with the return and value.

Sorry for the long post, I am tired of getting different answers at school and want one final answer on this, thanks.

Upvotes: 1

Views: 5901

Answers (3)

Enigmativity
Enigmativity

Reputation: 117027

I would suggest that your class look like this:

public class Rectangle
{
    public Rectangle(double length, double width)
    {
        this.Length = length;
        this.Width = width;
    }

    public double Length { get; set; }
    public double Width { get; set; }
    public double Area { get { return this.Width * this.Length; } }
    public double Perimeter { get { return 2.0 * (this.Width + this.Length); } }
    public double Diagonal { get { return Math.Sqrt(Math.Pow(this.Width, 2.0) + Math.Pow(this.Length, 2.0)); } }
}

Upvotes: 3

Kolichikov
Kolichikov

Reputation: 3020

  1. See here for why you might want a blank constructor. To summarize, adding a non blank constructor will stop the compiler from generating a blank one for you (the compiler assumes that if you wanted it, you would have defined it with the other constructors you wrote). Some things, like serialization, will not work without a blank constructor, so that's a reason you might want to add one.

  2. In my career, I've mostly seen people avoid using this in constructors. Maybe avoid isn't the right word, but unless it's unclear, they just didn't bother to put it there. This is probably too minor an issue to lose any sleep over.

UPDATE based on some of your comments

When you write

public Rectangle(double length, double width)
{
    Length = length; //parameter length assigned to field length by virtue of property Length
}

you are assigning the parameter length to the property Length, which itself assigns the passed in value to the length private field. Since C# is case sensitive, Length and length aren't confused in any scenario, and you don't need to specify the this keyword.

Inside a method with a parameter called length, the language is assuming that you are referring to the parameter of the method when you type length. So if you try to do something like this:

    public Rectangle(double length, double width)
    {
        length = length; //warning: Assignment made to same variable; did you mean to assign to something else
    }

The compiler doesn't try and assume that you are assigning the property to the field, and this is just assigning the length parameter to itself. In this case, you would use the this keyword to tell the compiler that you want to assign the parameter length to the private field length, like this:

    public Rectangle(double length, double width)
    {
        this.length = length; //no warning
    }

END UPDATE

  1. Yes, you could declare the property as just Property {get;set;}. This feature is only from C# 3.0 and is called auto-implemented properties (see this link). Before that you had to provide the implementation yourself.

Upvotes: 0

Programmer7
Programmer7

Reputation: 159

I changed my class to this:

public class Rectangle
    {
        public Rectangle(double length, double width)
        {
            Length = length;
            Width = width;
        }

        public double Length { get; set; }

        public double Width { get; set; }

        public double getArea()
        {
            return Width * Length;
        }

        public double getPerimeter()
        {
            return 2 * Width + 2 * Length;
        }

        public double getDiagonal()
        {
            return Math.Sqrt(Math.Pow(Width, 2) + Math.Pow(Length, 2));
        }
    }

If anyone has any other feedback on anything above that you recommend to change please give it, I catch on very fast and want to learn the correct way.

Upvotes: 0

Related Questions