Take the 2-minute tour ×

Is it possible for me to declare a variable, without a type, then specify the type based on some conditions? For example, I want to create a SHA Hash object based what size the user would like to use:

        //get the Sha hasher
        var shaHash;

        switch (this.HASH_ALGORITHM)
            case HashAlgorithm.SHA256:  //HashAlgorithm is an enum.
                shaHash = SHA256.Create();
            case HashAlgorithm.SHA384:
                shaHash = SHA384.Create();
            case HashAlgorithm.SHA512:
                shaHash = SHA512.Create();

        //... do hashing

Is this possible?

share|improve this question
Did you try it? –  CodeNaked Jun 24 '11 at 14:08
Implicitly-typed local variables must be initialized. So var shaHash; is a compile error. –  Joe Jun 24 '11 at 14:11
@CodeNaked, yes, my code will not get passed the compiler. –  Petey B Jun 24 '11 at 14:14
exactly. But what are you trying to accomplish that you won't gain trough declaring the base class instead? –  Johny Skovdal Jun 24 '11 at 14:20
@Kelrond, the base class will work fine in my case. Thanks. –  Petey B Jun 24 '11 at 14:24

6 Answers 6

up vote 18 down vote accepted

No, that won't work. But, given that all three of those types inherit from System.Security.Cryptography.HashAlgorithm, you could declare the variable of that type:

HashAlgorithm shaHash;

    // snip


Just to add, the reason that var shaHash; will not work is because that var is just a shorthand way of saying "I'm not sure of the best type to use here, so please infer it for me". The compiler requires that you specify an initial value so that it can determine the best type to use.

share|improve this answer

Not without resorting to dynamic; you need to use inheritance or interfaces for this. So the type could be e.g. object, or if the different SHA classes have another common superclass or if they implement some interface, you could use that superclass or interface. Otherwise, you could create adapter classes with a common superclass and wrap the SHA objects in the adapters.

share|improve this answer

Use base type:

HashAlgorithm shaHash = null;
share|improve this answer

If you have a parent class in which you declare a type, and 3 other classes which inherit after the parent class (children), then you can create a method which would be declared as returning the parent type, but inside you would return each of the child types. This will work and compile as the parent type will be automatically casted on to a child type. It will not work the other way round however (you cannot cast a type from a child to parent).

share|improve this answer

It is not posible. In order to use var you need the compiler to know what will the type of your variable be and that is only posible when you assign a value. Also you cannot say var varname = null; because null does not define a type.

share|improve this answer

In c# 4 you can declare a dynamic type.

share|improve this answer
You can, but you shouldn’t use dynamic in this case. –  Konrad Rudolph Jun 24 '11 at 14:11
Yeah you are right he should use the base type they all inherit from. –  Ben Robinson Jun 24 '11 at 14:12

Your Answer


