Shorthand notation for class member initialization

In a C # block, I can define and initialize a variable as follows:

var xyz = new Xyz();

The type xyzwill be set accordingly.

However, at the class level I have to specify the type twice:

class Abc
{
    Xyz xyz = new Xyz();
}

Is there a shorthand syntax for printing a type name twice?

This is not such a big problem with short types, xyzbut a shorter notation could help with the LongTypeNames names.

+3
source share
2 answers

If you use several specific types and want to reduce them, you can create an alias using the using statement, for example:

using ShortName = Abc.Xyz.ClassWithAVeryLongNameThatYouDontLikeTypingTooOften;

then inside this file you can do something like:

class Abc
{
    ShortName xyz = new ShortName();
}

But as far as I know, there is no varclass level equivalent .

+6

All Articles