Nick C

Nick C

  • NA
  • 1
  • 0

Questions on C# concepts

Jan 5 2009 5:33 AM

Hello there,

I am a C# developer who had recently been assigned to re-factor lots of old code. The code I'm working with is plagued with lots of duplication, inconsistent naming conventions, poor segmentation of code and little or no helpful documentation. To prevent this from happening again, I am currently drafting out a guideline document to govern how C# development should be done moving forward.

After looking up a few documents and guides related to C# online, I have gained a deeper understanding on how conventional development should be like and have applied a lot of what I've learnt in the code (e.g. usage of helper classes, consistent naming conventions of variables, namespaces, grouping of code via Regions, etc.) and also incorporated a tool to enforce it (StyleCop). However, I am still not sure on the following areas:

  1. Usage of properties VS fields in subclasses

    Take a look at the two versions of the code below:

class Vehicle

{

private int wheels;

private double weight;


public int Wheels

{

get { return this.wheels; }

set { this.wheels = value; }

}


public double Weight

{

get { return this.weight; }

set { this.weight = value; }

}

}

class Car : Vehicle

{

private string type;

private string manufacturer;


public Car(string type, string manufacturer, int wheels, double weight)

{

this.Type = type;

this.Manufacturer = manufacturer;

this.Wheels = wheels;

this.Weight = weight;

}


public string Type

{

get { return type; }

set { type = value; }

}


public string Manufacturer

{

get { return manufacturer; }

set { manufacturer = value; }

}

}

class Vehicle

{

protected int wheels;

protected double weight;

}


class Car : Vehicle

{

private string type;

private string manufacturer;


public Car(string type, string manufacturer, int wheels, double weight)

{

this.wheels = type;

this.manufacturer = manufacturer;

this.wheels = wheels;

this.weight = weight;

}

}

As you can see in code #1, the Car class constructs the object using properties, whereas the in code #2, the Car class constructs it using fields (which requires the fields from the superclass to be protected). From what I read in general (and what StyleCop pointed out), class member fields are better kept private. Personally, I feel that the usage of properties allows me to encapsulate certain behavior such as the limitation of wheels in this example, which makes me more inclined to use them. However, it does look weird that a class has to use getter/setter methods on its own member variables. What is the general view on this?

  1. Creation of subclass objects passing in superclasses

    Is there a way to create a subclass object by passing in a superclass, then populating whatever values that differs between them? For example, taking the code above, to create a Car object from an existing Vehicle object, the following code is used:

    public Car(Vehicle vehicle, string type, string manufacturer)

    {

    this.Wheels = vehicle.Wheels;

    this.Weight = vehicle.Weight;

    this.type = type;

    this.manufacturer = manufacturer;

    }

    However, if the Vehicle class has plenty of fields, the code can quickly get unwieldy. Is there an easier way to create a subclass from an existing superclass?

  2. Public static properties VS public readonly strings

    I often use configuration classes and expose constant string values for messages, labels, etc. What is the best way to expose these constants for reading (no writing is involved)? Through public static properties or public readonly strings?

  3. Naming – events

    What is a good naming convention for event names? I find that the auto-generated names by Visual Studio can get confusing, often when the control name has a very long name like btnThisIsAVeryVeryVeryLongNameButton_Clicked. Also, StyleCop states that method names should begin with an uppercase letter. When it is normal for prefixing .NET controls with its control type, such as lbl, btn, txt, etc., how do I get around this? How would you guys name your events?

Thanks and I hope to hear from you guys soon!