[英]Static parameters in C# methods
I have doubt in C# about Static class usage in methods. 我在C#中对方法中的静态类用法有疑问。 Suppose that we have a method with two parameter int and Enum in another class. 假设我们在另一个类中有一个带有两个参数int和Enum的方法。
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 如果我们实现一个静态类而不是Enum,则C#不允许将静态类作为方法参数传递
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? 为什么C#CLR拒绝采用静态类作为参数?
Thanks 谢谢
You can never instantiate a TenantType
- so the only value you could possibly pass into DemoMethod
would be null
. 您永远无法实例化TenantType
因此,您可能传递给DemoMethod
的唯一值将为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. 考虑一下您将如何调用该方法-如果您希望调用(例如) DemoMethod(10, TenantType.TenantUser)
那么无论如何这将是一个int
参数,而不是TenantType
。
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. 您应该很高兴C#这么早就发现错误,基本上-这是静态类的好处之一。
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. 那时您可以接受它作为参数-您仍然可以调用DemoMethod(10, TenantType.User)
,但是以类型安全的方式进行,除非该方法真正关心整数映射,否则它永远DemoMethod(10, TenantType.User)
看见。
Because if you specify TenantType tenantType
as a parameter, you tell C#, that you require an instance of TenantType
here. 因为如果您将TenantType tenantType
指定为参数,则会告诉C#,您在此处需要TenantType
的实例 。 And since you don't pass one, it won't work. 而且由于您未通过一项,因此无法使用。
This, in turn, will: 反过来,这将:
public void DemoMethod(int pricePerTenant, int tenantType) {
}
DemoMethod(3, TenantType.TenantAdmin);
But . 但是 。 There is a method similar to yours, which will actually work: 有一种类似于您的方法,该方法实际上可以工作:
public class TenantType {
private int value;
private TenantType(int newValue)
{
value = newValue;
}
public override bool Equals(object obj)
{
return (obj is TenantType && (obj as TenantType).value == this.value;
}
public override int GetHashCode()
{
return value;
}
public static bool operator == (TenantType left, TenantType right)
{
return left.Equals(right);
}
public static bool operator != (TenantType left, TenantType right)
{
return !(left.Equals(right));
}
public static TenantType Admin = new TenantType(1);
public static TenantType User = new TenantType(2);
public static TenantType PublicUser = new TenantType(3);
}
public void DemoMethod(int pricePerTenant, TenantType tenantType)
{
}
DemoMethod(4, TenantType.Admin);
As you see, this requires a lot more work than the simplest solution with enums (after all, this is precisely what they were created for). 如您所见,这比使用枚举的最简单解决方案需要更多的工作(毕竟,这正是它们创建的目的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.