开发者

C# pre initialized class

What is the best practice to create pre-initialized class. For example

Chip chip = new Atmega8();

开发者_开发问答I would like to have its properties already defined like:

chip.Name = "Atmega8 AVR Chip";

and so on.

How to achieve it in C#?

Should I use readonly public properties or property with private set?


Have your constructor initialize the values:

class Atmega8 {
    public Atmega8 ()
    {  
        Name = "Atmega8 AVR Chip";
    }

    public string Name { get; set; }
}

If you intend Name to be the same for all instances, it might make sense to declare it abstract in the base class and override the getter:

abstract class Chip {
    public abstract string Name { get; }
}

class Atmega8 : Chip {
    public override string Name {
        get { return "Atmega8 AVR Chip"; }
    }
}

Because we haven't defined a set method, the value cannot be changed, much like a readonly variable except it isn't even stored anywhere and just returned on each call.


If you want the compiler to enforce that nothing can change the value of the field once initialized, then set it up as a read-only field, and populate it in the constructor of the class (or simply initialize it when you declare it; this doesn't work so well with inheritance though). If you don't care as long as nothing OUTSIDE the object can change it (meaning you will trust your own coding discipline to ensure it doesn't change internally), a get-only property with a backing field, or an auto-property with a private setter, are your bets.

IF you absitively posolutely DO NOT WANT the value to change for a particular class, EVER, then I would make it a get-only property returning either a string literal or a constant. I would recommend using the constant over the literal, as you can put the constants into their own static class which you can then use separately from each Chip class.

HOWEVER, there's a quirk of constants you should know. A constant value in .NET is stored in the manifest of not only the assembly containing the declaring code, but in every assembly that references the declaring assembly. Each assembly's code them uses the value from its own manifest. So, if the constant value EVER changes, any assembly that references the declaring assembly must be recompiled to update those assemblies' manifests with the new value. Otherwise, the constant will only have its new value when used from within the declaring assembly. For this reason, labeling a variable as constant should not be done lightly. Personally, my opinion is if the constant isn't some value on which the continued existence and functioning of the universe depends, like pi, e, the speed of light in a vacuum, Plank's Constant, Avogadro's Number, etc, then it isn't "constant". Anything else, like communication code ordinals, CAN change, even if doing so would break compatibility with every previous version of your program.


Depends what you want to accomplish.

It looks like you never want the value of Name to change. One approach would be to declare Name as abstract in Chip, and implement Name in each child class to return a constant string value.

abstract class Chip
{
    public abstract string Name { get; }
}

class Amiga8 : Chip
{
    public override string Name { get { return "Atmega8 AVR Chip"; } }
}

class Program
{

    static void Main(string[] args)
    {
        Chip chip = new Amiga8();
        Console.WriteLine(chip.Name);
    }
}


In the constructor of the Atmega8 class you can set a property to something. Ie:

public Atmega8() {
    Name = "Atmega8 AVR Chip";
}

If you do not want that to be changed in runtime you could mark the property as readonly ( only assignable through a constructor of declarative ).

private readonly string _Name = string.Empty;

public string Name {
    get { return _name; }
}

public Atmega8() {
    _Name = "Atmega8 AVR Chip";
}


Value of property cannot change -> Read-only public property.

Value of property can change -> Property with private set


If you don't want it to change, make the Name property a const or readonly on the Atmega8 class. Private set still allows the Name to change internally.


You're saying that you want the class to be populated at the same time it's initialized? Just populate the object in the constructor, like so:

class Test
{
    public Test()
    {
        this.Name = "Hello World";
    }

    //if you need to pass information into the constructor:
    public Test(string testName)
    {
        this.Name = testName;
    }
}

Then, you can do this to initialize it:

Test test = new Test(); //default name of Hello World!

OR

Test test = new Test("Bingo!");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜