C# 定义结构中字段的最大值和最小值
我有一个C# 定义结构中字段的最大值和最小值,c#,struct,C#,Struct,我有一个struct和一个整数变量。我的变量值必须介于1200和1599之间。但在默认构造函数中,我无法控制变量的值。如何在我的结构中执行此操作 struct MyStruct { public int x; public MyStruct(int x) { if(x>1599) this.x=1599; else if(x<1200) this.x=1200;
struct
和一个整数变量。我的变量值必须介于1200和1599之间。但在默认构造函数中,我无法控制变量的值。如何在我的结构中执行此操作
struct MyStruct
{
public int x;
public MyStruct(int x)
{
if(x>1599)
this.x=1599;
else if(x<1200)
this.x=1200;
else
this.x=x;
}
}
struct MyStruct
{
公共int x;
公共MyStruct(intx)
{
如果(x>1599)
这个.x=1599;
否则,如果(x使用带有支持字段的属性:
struct MyStruct
{
private const int MIN_VALUE = 1200;
private const int MAX_VALUE = 1599;
private int x;
public int X
{
get { return x + MIN_VALUE; }
set
{
if(value > MAX_VALUE)
x = MAX_VALUE;
else if(value < MIN_VALUE)
x = MIN_VALUE;
else
x = value;
x -= MIN_VALUE;
}
}
// constructor is not really needed anymore, but can still be added
}
struct MyStruct
{
private int x;
public MyStruct(int x)
{
if (x > 1599)
this.x = 399;
else if (x < 1200)
this.x = 0;
else
this.x = x - 1200;
}
public int X { get { return x+1200; } }
}
struct MyStruct
{
私有常量int最小值=1200;
私有常量int MAX_值=1599;
私人INTX;
公共整数X
{
获取{返回x+MIN_值;}
设置
{
如果(值>最大值)
x=最大值;
否则如果(值<最小值)
x=最小值;
其他的
x=值;
x-=最小值;
}
}
//构造函数不再是真正需要的,但仍然可以添加
}
我将该属性与我的setter和Damien_(不信者的getter)结合起来,得到了x
的初始状态。我也同意Tim关于“幻数”常数的观点,并补充了这一点。因此,请将这两个也归功于“我的答案”
同样,正如DatVM已经说过的:公共字段/属性应该根据通用的C#命名指南以大写字母开头。这也使您可以对支持字段使用相同的名称,但以小写字母开头(我个人不喜欢丑陋的#)
最后但并非最不重要的一点:请阅读rexcfnghk的答案,即使它不是真正的答案,因为他也是绝对正确的。使用带有支持字段的属性:
struct MyStruct
{
private const int MIN_VALUE = 1200;
private const int MAX_VALUE = 1599;
private int x;
public int X
{
get { return x + MIN_VALUE; }
set
{
if(value > MAX_VALUE)
x = MAX_VALUE;
else if(value < MIN_VALUE)
x = MIN_VALUE;
else
x = value;
x -= MIN_VALUE;
}
}
// constructor is not really needed anymore, but can still be added
}
struct MyStruct
{
private int x;
public MyStruct(int x)
{
if (x > 1599)
this.x = 399;
else if (x < 1200)
this.x = 0;
else
this.x = x - 1200;
}
public int X { get { return x+1200; } }
}
struct MyStruct
{
私有常量int最小值=1200;
私有常量int MAX_值=1599;
私人INTX;
公共整数X
{
获取{返回x+MIN_值;}
设置
{
如果(值>最大值)
x=最大值;
否则如果(值<最小值)
x=最小值;
其他的
x=值;
x-=最小值;
}
}
//构造函数不再是真正需要的,但仍然可以添加
}
我将该属性与我的setter和Damien_(不信者的getter)结合起来,得到了x
的初始状态。我也同意Tim关于“幻数”常数的观点,并补充了这一点。因此,请将这两个也归功于“我的答案”
同样,正如DatVM已经说过的:公共字段/属性应该根据通用的C#命名指南以大写字母开头。这也使您可以对支持字段使用相同的名称,但以小写字母开头(我个人不喜欢丑陋的#)
最后但并非最不重要的一点:请阅读rexcfnghk的答案,即使它不是真正的答案,因为他也是绝对正确的。您应该将x改为属性
:
private int _x;
public int x {
get
{
return _x;
}
set
{
_x = value;
if (_x > 1599)
{
_x = 1599
}
else if (_x < 1200)
{
_x = 1200
}
}
}
private int\ux;
公共整数x{
得到
{
返回x;
}
设置
{
_x=值;
如果(_x>1599)
{
_x=1599
}
否则如果(_x<1200)
{
_x=1200
}
}
}
注:至于C#命名约定,它应该被称为X
(大写),而不是X
您应该将X改为属性
:
private int _x;
public int x {
get
{
return _x;
}
set
{
_x = value;
if (_x > 1599)
{
_x = 1599
}
else if (_x < 1200)
{
_x = 1200
}
}
}
private int\ux;
公共整数x{
得到
{
返回x;
}
设置
{
_x=值;
如果(_x>1599)
{
_x=1599
}
否则如果(_x<1200)
{
_x=1200
}
}
}
注:至于C#命名约定,它应该被称为X
(大写),而不是X
关于使用属性和私有支持字段的另一个变体:
struct MyStruct
{
private const int MIN_VALUE = 1200;
private const int MAX_VALUE = 1599;
private int x;
public int X
{
get { return x + MIN_VALUE; }
set
{
if(value > MAX_VALUE)
x = MAX_VALUE;
else if(value < MIN_VALUE)
x = MIN_VALUE;
else
x = value;
x -= MIN_VALUE;
}
}
// constructor is not really needed anymore, but can still be added
}
struct MyStruct
{
private int x;
public MyStruct(int x)
{
if (x > 1599)
this.x = 399;
else if (x < 1200)
this.x = 0;
else
this.x = x - 1200;
}
public int X { get { return x+1200; } }
}
struct MyStruct
{
私人INTX;
公共MyStruct(intx)
{
如果(x>1599)
这个.x=399;
否则如果(x<1200)
这个.x=0;
其他的
这个.x=x-1200;
}
公共int X{get{return X+1200;}}
}
这确保默认构造值为“范围内”
但是任何变体都会引入一些开销,因此这取决于您是否可以接受。关于使用属性和私有备份字段的另一个变体:
struct MyStruct
{
private const int MIN_VALUE = 1200;
private const int MAX_VALUE = 1599;
private int x;
public int X
{
get { return x + MIN_VALUE; }
set
{
if(value > MAX_VALUE)
x = MAX_VALUE;
else if(value < MIN_VALUE)
x = MIN_VALUE;
else
x = value;
x -= MIN_VALUE;
}
}
// constructor is not really needed anymore, but can still be added
}
struct MyStruct
{
private int x;
public MyStruct(int x)
{
if (x > 1599)
this.x = 399;
else if (x < 1200)
this.x = 0;
else
this.x = x - 1200;
}
public int X { get { return x+1200; } }
}
struct MyStruct
{
私人INTX;
公共MyStruct(intx)
{
如果(x>1599)
这个.x=399;
否则如果(x<1200)
这个.x=0;
其他的
这个.x=x-1200;
}
公共int X{get{return X+1200;}}
}
这确保默认构造值为“范围内”
但是任何变体都会引入一些开销,所以这是否可以接受取决于您
我的变量值必须介于1200和1599之间
在C#中,您不能为struct
s定义自己的默认构造函数。如果您有一个MyStruct
数组,比如var myArray=new MyStruct[5]
,将调用MyStruct
的默认构造函数,myArray
中的元素都将x
等于0,这根据您的要求是无效的
这就是为什么我认为您的结构设计不正确的原因
√ 执行确保所有实例数据设置为零、假或空(视情况而定)的状态有效
这可以防止在创建结构数组时意外创建无效实例
如果在调用结构
的默认构造函数时需要参数验证,请改用类
另外,您当前的MyStruct
设计使其可变。请查看原因
我的变量值必须介于1200和1599之间
在C#中,您不能为struct
s定义自己的默认构造函数