Static parameters in C# methods

I have doubt in C# about Static class usage in methods. Suppose that we have a method with two parameter int and Enum in another class.

public void DemoMethod(int pricePerTenant , TenantType tenantType){
    //Method implementation    
}

If we implement a static class instead of Enum, C# don't allow passing static class as method parameter

public static class TenantType
{
   public static readonly int TenantAdmin = 1;
   public static readonly int TenantUser = 2;
   public static readonly int PublicUser = 3;
}

//With static class parameters
public void DemoMethod(int pricePerTenant , TenantType tenantType){
    //Method implementation    
}

Why C# CLR refuse to take Static class as parameters?

Thanks

Jon Skeet
people
quotationmark

You can never instantiate a TenantType - so the only value you could possibly pass into DemoMethod would be null. Think about how you'd be calling the method - if you were expecting to call (say) DemoMethod(10, TenantType.TenantUser) then that would be an int argument instead of a TenantType anyway.

Basically, a static class never has instances, so it doesn't make sense to allow them to be used anywhere that you'd be considering instances - including method parameters and type arguments. You should be grateful that C# is catching your error so early, basically - that's one of the benefits of static classes.

In this case it sounds like really you should have an enum instead:

public enum TenantType
{
    Admin = 1,
    User = 2,
    PublicUser = 3
}

At that point you can accept it as a parameter - you'll still be able to call DemoMethod(10, TenantType.User), but in a type-safe way where unless the method really cares about the integer mapping, it never has to see it.

people

See more on this question at Stackoverflow