C# 创建自定义布尔值?
如何创建bool语法的副本,而不使用C# 创建自定义布尔值?,c#,syntax,C#,Syntax,如何创建bool语法的副本,而不使用true和false我将使用启用和禁用?我希望它能像这样使用 sBool E = Enabled; sBool f = Disabled; if (e || f == Disabled) { Do something... } 就这样做一个枚举 public enum sBool { Enabled, Disabled } 然后声明代码如下所示: sBool E = sBool.Enabled; sBool f = sBool.
true
和false
我将使用启用
和禁用
?我希望它能像这样使用
sBool E = Enabled;
sBool f = Disabled;
if (e || f == Disabled)
{
Do something...
}
就这样做一个枚举
public enum sBool
{
Enabled,
Disabled
}
然后声明代码如下所示:
sBool E = sBool.Enabled;
sBool f = sBool.Disabled;
if (E == sBool.Disabled || F == sBool.Disabled)
{
//Do something...
}
Boolean Enabled = true;
Boolean Disabled = false;
public sealed class Status: IEquatable<Status>
{
public Status(bool isEnabled)
{
_isEnabled = isEnabled;
}
public bool IsEnabled
{
get { return _isEnabled; }
}
public bool IsDisabled
{
get { return !_isEnabled; }
}
public bool Equals(Status other)
{
return other != null && this.IsEnabled == other.IsEnabled;
}
public static implicit operator bool(Status status)
{
return status.IsEnabled;
}
public static Status Enabled
{
get { return _enabled; }
}
public static Status Disabled
{
get { return _disabled; }
}
private readonly bool _isEnabled;
private static readonly Status _enabled = new Status(true);
private static readonly Status _disabled = new Status(false);
}
编辑:修复了
如果语法只需创建这样的枚举即可
public enum sBool
{
Enabled,
Disabled
}
然后声明代码如下所示:
sBool E = sBool.Enabled;
sBool f = sBool.Disabled;
if (E == sBool.Disabled || F == sBool.Disabled)
{
//Do something...
}
Boolean Enabled = true;
Boolean Disabled = false;
public sealed class Status: IEquatable<Status>
{
public Status(bool isEnabled)
{
_isEnabled = isEnabled;
}
public bool IsEnabled
{
get { return _isEnabled; }
}
public bool IsDisabled
{
get { return !_isEnabled; }
}
public bool Equals(Status other)
{
return other != null && this.IsEnabled == other.IsEnabled;
}
public static implicit operator bool(Status status)
{
return status.IsEnabled;
}
public static Status Enabled
{
get { return _enabled; }
}
public static Status Disabled
{
get { return _disabled; }
}
private readonly bool _isEnabled;
private static readonly Status _enabled = new Status(true);
private static readonly Status _disabled = new Status(false);
}
编辑:修复了如果语法您可以使用枚举,如下所示:
enum Status
{
Enabled,
Disabled
}
var e = Status.Enabled;
if (e == Status.Disabled)
{
// Do something
}
我不确定您的用例是什么,但就代码可读性/可维护性而言,我想说使用枚举是最简单的解决方案,也最容易让其他开发人员理解。您可以使用枚举,如下所示:
enum Status
{
Enabled,
Disabled
}
var e = Status.Enabled;
if (e == Status.Disabled)
{
// Do something
}
我不确定您的用例是什么,但就代码可读性/可维护性而言,我想说使用枚举是最简单的解决方案,也是其他开发人员最容易理解的解决方案。没有真正的好方法。
您可以利用枚举实际上只是具有奇特名称的整数这一事实,并使用位运算符模拟逻辑运算符
因此:
其实没有一个好方法可以做到这一点。
您可以利用枚举实际上只是具有奇特名称的整数这一事实,并使用位运算符模拟逻辑运算符
因此:
这是一个小小的欺骗,但您可以在两个变量上声明如下:
sBool E = sBool.Enabled;
sBool f = sBool.Disabled;
if (E == sBool.Disabled || F == sBool.Disabled)
{
//Do something...
}
Boolean Enabled = true;
Boolean Disabled = false;
public sealed class Status: IEquatable<Status>
{
public Status(bool isEnabled)
{
_isEnabled = isEnabled;
}
public bool IsEnabled
{
get { return _isEnabled; }
}
public bool IsDisabled
{
get { return !_isEnabled; }
}
public bool Equals(Status other)
{
return other != null && this.IsEnabled == other.IsEnabled;
}
public static implicit operator bool(Status status)
{
return status.IsEnabled;
}
public static Status Enabled
{
get { return _enabled; }
}
public static Status Disabled
{
get { return _disabled; }
}
private readonly bool _isEnabled;
private static readonly Status _enabled = new Status(true);
private static readonly Status _disabled = new Status(false);
}
现在,您可以在代码中编写:
Boolean sBool = Enabled;
缺点:Enabled和Disabled没有特殊的颜色。这有点欺骗,但您可以声明如下两个变量:
sBool E = sBool.Enabled;
sBool f = sBool.Disabled;
if (E == sBool.Disabled || F == sBool.Disabled)
{
//Do something...
}
Boolean Enabled = true;
Boolean Disabled = false;
public sealed class Status: IEquatable<Status>
{
public Status(bool isEnabled)
{
_isEnabled = isEnabled;
}
public bool IsEnabled
{
get { return _isEnabled; }
}
public bool IsDisabled
{
get { return !_isEnabled; }
}
public bool Equals(Status other)
{
return other != null && this.IsEnabled == other.IsEnabled;
}
public static implicit operator bool(Status status)
{
return status.IsEnabled;
}
public static Status Enabled
{
get { return _enabled; }
}
public static Status Disabled
{
get { return _disabled; }
}
private readonly bool _isEnabled;
private static readonly Status _enabled = new Status(true);
private static readonly Status _disabled = new Status(false);
}
现在,您可以在代码中编写:
Boolean sBool = Enabled;
缺点:启用和禁用没有特殊颜色。如果sBool在项目中扮演重要角色,您可以选择实现相应的全尺寸结构(而不是枚举):
如果sBool在您的项目中扮演重要角色,您可以选择实现相应的全尺寸结构(而不是枚举):
我只想使用bool,但是如果您真的想用最可读的语法将逻辑封装在一个单独的类中,您可以这样做:
sBool E = sBool.Enabled;
sBool f = sBool.Disabled;
if (E == sBool.Disabled || F == sBool.Disabled)
{
//Do something...
}
Boolean Enabled = true;
Boolean Disabled = false;
public sealed class Status: IEquatable<Status>
{
public Status(bool isEnabled)
{
_isEnabled = isEnabled;
}
public bool IsEnabled
{
get { return _isEnabled; }
}
public bool IsDisabled
{
get { return !_isEnabled; }
}
public bool Equals(Status other)
{
return other != null && this.IsEnabled == other.IsEnabled;
}
public static implicit operator bool(Status status)
{
return status.IsEnabled;
}
public static Status Enabled
{
get { return _enabled; }
}
public static Status Disabled
{
get { return _disabled; }
}
private readonly bool _isEnabled;
private static readonly Status _enabled = new Status(true);
private static readonly Status _disabled = new Status(false);
}
我只想使用bool,但是如果您真的想用最可读的语法将逻辑封装在一个单独的类中,您可以这样做:
sBool E = sBool.Enabled;
sBool f = sBool.Disabled;
if (E == sBool.Disabled || F == sBool.Disabled)
{
//Do something...
}
Boolean Enabled = true;
Boolean Disabled = false;
public sealed class Status: IEquatable<Status>
{
public Status(bool isEnabled)
{
_isEnabled = isEnabled;
}
public bool IsEnabled
{
get { return _isEnabled; }
}
public bool IsDisabled
{
get { return !_isEnabled; }
}
public bool Equals(Status other)
{
return other != null && this.IsEnabled == other.IsEnabled;
}
public static implicit operator bool(Status status)
{
return status.IsEnabled;
}
public static Status Enabled
{
get { return _enabled; }
}
public static Status Disabled
{
get { return _disabled; }
}
private readonly bool _isEnabled;
private static readonly Status _enabled = new Status(true);
private static readonly Status _disabled = new Status(false);
}
这行不通。它需要从int到bool的隐式转换。您知道如何删除sBool前缀吗?所以它只是“==Disabled”?除了if
结构中的语法之外,我不知道为什么@xen-0不起作用。丹尼尔,我不认为有什么办法,你为什么要那样做?这行不通。它需要从int到bool的隐式转换。您知道如何删除sBool前缀吗?所以它只是“==Disabled”?除了if
结构中的语法之外,我不知道为什么@xen-0不起作用。丹尼尔,我不认为有什么办法,你为什么要这么做?你知道如何删除状态前缀吗?所以它将被“==禁用”?您始终可以创建自己的类来覆盖相等运算符。我会举个例子。编辑:看起来有人已经这样做了。你知道如何删除状态前缀吗?所以它将被“==禁用”?您始终可以创建自己的类来覆盖相等运算符。我会举个例子。编辑:看起来有人已经这样做了。如果可能的话,我还需要删除sBool前缀,例如“==Disabled
”如果可能的话,我还需要删除sBool前缀,例如“==Disabled
”??