简体   繁体   English

是C#a = b = c; 等于b = c; 一个= C; 而C ++是b = c; A = B;?

[英]Is C# a=b=c; equal to b=c; a=c; whereas C++ is b=c; a=b;?

In another words, C# operator= will return the right value, isn't it? 换句话说,C#operator =会返回正确的值,不是吗? But C++ conventionally returns left value, right? 但是C ++通常会返回左边的值,对吧?

No, the result of the assignment expression is the value assigned, not the expression on the right. 不,赋值表达式的结果是赋值, 而不是右边的表达式。 So consider this: 所以考虑一下:

    byte a;
    int b;
    byte c = 10;

    a = b = c; // Fails to compile

This fails to compile because although b = c is valid, it's an expression of type int , which then can't be assigned to a which is of type byte . 这个编译失败,因为尽管b = c是有效的,它是类型的表达式int ,然后不能被分配到a其类型的byte

From the C# 4 language spec, section 7.17.1: 从C#4语言规范,第7.17.1节:

The result of a simple assignment expression is the value assigned to the left operand. 简单赋值表达式的结果是赋给左操作数的值。 The result has the same type as the left operand and is always classified as a value. 结果与左操作数的类型相同,并且始终归类为值。

EDIT: Here's proof that it's the value which was assigned to b which is used, not the value of c : 编辑:这里证明它是分配给b的值, 而不是 c的值:

using System;

class ACType
{
    public int Value { get; set; }

    public static implicit operator BType(ACType ac)
    {
        return new BType { Value = ac.Value / 2 };
    }
}

class BType
{
    public int Value { get; set; }

    public static implicit operator ACType(BType b)
    {
        return new ACType { Value = b.Value / 2 };
    }
}

class Test
{
    static void Main()
    {
        ACType a, c;
        BType b;

        c = new ACType { Value = 100 };
        a = b = c;

        Console.WriteLine(a.Value); // Prints 25
    }
}

The a = b = c; a = b = c; statement is equivalent to: 声明相当于:

BType tmp = c;
b = tmp;
a = tmp;

So the value of b isn't read afterwards (it's not equivalent to b = c; a = b; ) - so if b were actually a property, the behaviour of the property would be irrelevant other than for side-effects. 因此,之后不会读取 b的值(它不等于b = c; a = b; ) - 所以如果b实际上是一个属性,那么除了副作用之外,属性的行为将是无关紧要的。 It's whatever value was used in the assignment to b which is also used in the assignment to a . 是在b的赋值中使用任何值 ,它也用于赋值给a

In this case, the assignment to b requires a conversion from ACType to BType which creates a BType with Value=50. 在这种情况下,分配给b需要从转换ACTypeBType它创建了一个BType用值= 50。 The assignment to a then requires a conversion from BType to ACType , which creates an ACType with Value=25. 分配给a则需要从转换BTypeACType ,它创建一个ACType与值= 25。

No, it works the same in both languagues: a = b = c; 不,它在两个语言中都是一样的: a = b = c; works like this: a = (b = c); 像这样工作: a = (b = c); . The assignment operator works right-to-left, and the value returned is the result of the assignment, which is the left-hand-side. 赋值运算符从右向左工作,返回的值是赋值的结果,即左侧。 So conceptually, a = b = c; 从概念上讲, a = b = c; is the same as: b = c; a = b; 与: b = c; a = b;相同b = c; a = b; b = c; a = b; .

For C#, the assignment operators and the conditional operator (?:) are right-associative, meaning that operations are performed from right to left. 对于C#,赋值运算符和条件运算符(?:)是右关联的,这意味着操作从右到左执行。 For example, x = y = z is evaluated as x = (y = z). 例如,x = y = z被评估为x =(y = z)。 ( Source ) 来源

Same for C++ ( Source ) 与C ++相同( 来源

A useful tidbit about the semantics of a = b = c is that a will indeed receive the same value ( normally c *) that was assigned to b , the present value of b is not a factor. 约的语义的有用趣闻a = b = ca确实会接收到相同的值( 通常 c分配给*) b ,的当前b是不是一个因素。

Yes, that could be considered surprising. 是的,这可能被认为是令人惊讶的。 Consider the example: 考虑这个例子:

class Foo
{
    private int _bar;
    public int Bar
    {
        get { return _bar; }
        set { _bar = value + 1; }
    }
}

...

Foo foo = new Foo();
int a, c;
c = 4;

a = foo.Bar = c; // is 'a' 4 or 5? 

If the value passed through foo.Bar and the mutation follows into a , then a would be 5. However, that is not what happens. 如果值通过foo.Bar传递并且变异跟随a ,那么a将是5.但是,这不是发生的事情。 a and foo.Bar receive the same value, a does not receive the mutation from foo.Bar . afoo.Bar接收相同的值, a不接收来自foo.Bar的变异。 It's not directly your question, but it's interesting enough to post considering some of the comments. 这不是你的直接问题,但考虑到一些评论,发布它是有趣的。


*Jon Skeet had an interesting observation about types that are implicitly convertible to one another but have data loss/modification in the conversion. * Jon Skeet对可隐式转换为彼此但在转换中有数据丢失/修改的类型有一个有趣的观察。 In this case, the value may very well not be the original value of c , but the converted value, which may be different. 在这种情况下,该值很可能不是 c的原始值,而是转换后的值,它可能是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM