explicit ,implicit
explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换
explicti 表示显式转换,如从 A -> B 必须进行强制类型转换(B = (B)A)
implicit 表示隐式转换,如从 B -> A 只需直接赋值(A = B)
隐式转换可以让我们的代码看上去更漂亮、更简洁易懂,所以最好多使用 implicit 运算符。
[StructLayout(LayoutKind.Sequential)]
public struct TypedValue
{
private System.Type _type;
private object _val;
public TypedValue(System.Type type, object val)
{
this._type = type;
this._val = val;
}
public System.Type Type
{
get
{
if (this._type == null)
{
return typeof(string);
}
return this._type;
}
}
public object Value
{
get
{
return this._val;
}
}
public static implicit operator TypedValue(string val)
{
return new TypedValue(typeof(string), val);
}
public static implicit operator TypedValue(long? val)
{
return new TypedValue(typeof(long), val);
}
public static implicit operator TypedValue(int? val)
{
return new TypedValue(typeof(int), val);
}
public static implicit operator TypedValue(short? val)
{
return new TypedValue(typeof(short), val);
}
public static implicit operator TypedValue(decimal? val)
{
return new TypedValue(typeof(decimal), val);
}
public static implicit operator TypedValue(float? val)
{
return new TypedValue(typeof(float), val);
}
public static implicit operator TypedValue(double? val)
{
return new TypedValue(typeof(double), val);
}
public static implicit operator TypedValue(bool? val)
{
return new TypedValue(typeof(bool), val);
}
public static implicit operator TypedValue(byte[] val)
{
return new TypedValue(typeof(byte[]), val);
}
public static implicit operator TypedValue(Guid? val)
{
return new TypedValue(typeof(Guid), val);
}
public static implicit operator TypedValue(DateTime? val)
{
return new TypedValue(typeof(DateTime), val);
}
}
public struct TypedValue
{
private System.Type _type;
private object _val;
public TypedValue(System.Type type, object val)
{
this._type = type;
this._val = val;
}
public System.Type Type
{
get
{
if (this._type == null)
{
return typeof(string);
}
return this._type;
}
}
public object Value
{
get
{
return this._val;
}
}
public static implicit operator TypedValue(string val)
{
return new TypedValue(typeof(string), val);
}
public static implicit operator TypedValue(long? val)
{
return new TypedValue(typeof(long), val);
}
public static implicit operator TypedValue(int? val)
{
return new TypedValue(typeof(int), val);
}
public static implicit operator TypedValue(short? val)
{
return new TypedValue(typeof(short), val);
}
public static implicit operator TypedValue(decimal? val)
{
return new TypedValue(typeof(decimal), val);
}
public static implicit operator TypedValue(float? val)
{
return new TypedValue(typeof(float), val);
}
public static implicit operator TypedValue(double? val)
{
return new TypedValue(typeof(double), val);
}
public static implicit operator TypedValue(bool? val)
{
return new TypedValue(typeof(bool), val);
}
public static implicit operator TypedValue(byte[] val)
{
return new TypedValue(typeof(byte[]), val);
}
public static implicit operator TypedValue(Guid? val)
{
return new TypedValue(typeof(Guid), val);
}
public static implicit operator TypedValue(DateTime? val)
{
return new TypedValue(typeof(DateTime), val);
}
}
explicit
explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。
static explicit operator target_type { source_type identifier }
参数:
- target_type 目标类型
- source_type 源类型。
- identifier Something。
注意:
- 转换运算符将源类型转换为目标类型。源类型提供转换运算符。与隐式转换不同,必须通过强制转换的方式来调用显式转换运算符。如果转换操作可能导致异常或丢失信息,则应将其标记为
explicit。这可以防止编译器无提示地调用可能产生无法预见后果的转换操作。
implicit
implicit 关键字用于声明隐式的用户定义类型转换运算符。
static implicit operator target_type { source_type identifier }
注意:
- 隐式转换可以通过消除不必要的类型转换来提高源代码的可读性。但是,因为可以在程序员未指定的情况下发生隐式转换,因此必须注意防止令人不愉快的后果。一般情况下,隐式转换运算符应当从不引发异常并且从不丢失信息,以便可以在程序员不知晓的情况下安全使用它们。如果转换运算符不能满足那些条件,则应将其标记为
explicit。
operator
operator 关键字用于在类或结构声明中声明运算符。运算符声明可以采用下列四种形式之一:
public static result-type operator unary-operator ( op-type operand )
public static result-type operator binary-operator ( op-type operand, op-type2 operand2 )
public static implicit operator conv-type-out ( conv-type-in operand )
public static explicit operator conv-type-out ( conv-type-in operand )
参数:
- result-type 运算符的结果类型。
- unary-operator 下列运算符之一:+ - ! ~ ++ — true false
- op-type 第一个(或唯一一个)参数的类型。
- operand 第一个(或唯一一个)参数的名称。
- binary-operator 其中一个:+ - * / % & | ^ << >> == != > <
>= <= - op-type2 第二个参数的类型。
- operand2 第二个参数的名称。
- conv-type-out 类型转换运算符的目标类型。
- conv-type-in 类型转换运算符的输入类型。
public abstract class CamlExpression : ICamlExpression
{
private CamlExpression And(CamlExpression q)
{
return new JoinCamlExpression(LogicalJoin.And, this, q);
}
private CamlExpression Or(CamlExpression q)
{
return new JoinCamlExpression(LogicalJoin.Or, this, q);
}
public static CamlExpression operator &(CamlExpression q1, CamlExpression q2)
{
return q1.And(q2);
}
public static CamlExpression operator |(CamlExpression q1, CamlExpression q2)
{
return q1.Or(q2);
}
/// <summary>
/// Operator trues the specified right.
/// </summary>
/// <param name="right">The right.</param>
/// <returns></returns>
public static bool operator true(CamlExpression right)
{
return false;
}
/// <summary>
/// Operator falses the specified right.
/// </summary>
/// <param name="right">The right.</param>
/// <returns></returns>
public static bool operator false(CamlExpression right)
{
return false;
}
#region ICamlExpression Members
public virtual void ToCaml(XmlNode pNode)
{
ToCaml(null, pNode);
}
public abstract void ToCaml(IFieldInternalNameProvider provider , XmlNode parentNode );
public override string ToString()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml( "<Where/>" );
this.ToCaml( null , doc.DocumentElement);
return doc.InnerXml;
}
{
private CamlExpression And(CamlExpression q)
{
return new JoinCamlExpression(LogicalJoin.And, this, q);
}
private CamlExpression Or(CamlExpression q)
{
return new JoinCamlExpression(LogicalJoin.Or, this, q);
}
public static CamlExpression operator &(CamlExpression q1, CamlExpression q2)
{
return q1.And(q2);
}
public static CamlExpression operator |(CamlExpression q1, CamlExpression q2)
{
return q1.Or(q2);
}
/// <summary>
/// Operator trues the specified right.
/// </summary>
/// <param name="right">The right.</param>
/// <returns></returns>
public static bool operator true(CamlExpression right)
{
return false;
}
/// <summary>
/// Operator falses the specified right.
/// </summary>
/// <param name="right">The right.</param>
/// <returns></returns>
public static bool operator false(CamlExpression right)
{
return false;
}
#region ICamlExpression Members
public virtual void ToCaml(XmlNode pNode)
{
ToCaml(null, pNode);
}
public abstract void ToCaml(IFieldInternalNameProvider provider , XmlNode parentNode );
public override string ToString()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml( "<Where/>" );
this.ToCaml( null , doc.DocumentElement);
return doc.InnerXml;
}
class JoinCamlExpression : CamlExpression
{
public readonly LogicalJoin LogicalJoin;
public readonly CamlExpression Left;
public readonly CamlExpression Right;
public JoinCamlExpression(LogicalJoin join, CamlExpression left, CamlExpression right)
{
LogicalJoin = join;
Left = left;
Right = right;
}
public override void ToCaml(IFieldInternalNameProvider provider, XmlNode parentNode)
{
XmlElement node = parentNode.OwnerDocument.CreateElement(LogicalJoin.ToString());
parentNode.AppendChild(node);
Left.ToCaml(provider, node);
Right.ToCaml(provider, node);
}
}
enum LogicalJoin
{
And,
Or
}
{
public readonly LogicalJoin LogicalJoin;
public readonly CamlExpression Left;
public readonly CamlExpression Right;
public JoinCamlExpression(LogicalJoin join, CamlExpression left, CamlExpression right)
{
LogicalJoin = join;
Left = left;
Right = right;
}
public override void ToCaml(IFieldInternalNameProvider provider, XmlNode parentNode)
{
XmlElement node = parentNode.OwnerDocument.CreateElement(LogicalJoin.ToString());
parentNode.AppendChild(node);
Left.ToCaml(provider, node);
Right.ToCaml(provider, node);
}
}
enum LogicalJoin
{
And,
Or
}