Programmer7 Programmer7 - 3 months ago 21
C# Question

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.

Answer
  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.
Comments