= 运算符(C# 参考)

赋值运算符 (=) 将右操作数的值存储在左操作数表示的存储位置、属性或索引器中,并将值作为结果返回。 操作数的类型必须相同(即右操作数必须可以隐式转换为左操作数的类型)。

备注

不能重载赋值运算符。 不过,可为类型定义隐式转换运算符,这样就可以对这些类型使用赋值运算符。 有关更多信息,请参见 使用转换运算符(C# 编程指南)

示例

class Assignment
{
    static void Main()
    {
        double x;
        int i;
        i = 5; // int to int assignment
        x = i; // implicit conversion from int to double
        i = (int)x; // needs cast
        Console.WriteLine("i is {0}, x is {1}", i, x);
        object obj = i;
        Console.WriteLine("boxed value = {0}, type is {1}",
                  obj, obj.GetType());
        i = (int)obj;
        Console.WriteLine("unboxed: {0}", i);
    }
}
/*
Output:
i is 5, x is 5
boxed value = 5, type is System.Int32
unboxed: 5
 */

请参见

参考

C# 运算符

概念

C# 编程指南

其他资源

C# 参考