简体   繁体   English

C#中的“with”运算符是什么?

[英]What is the "with" operator for in C#?

I've came across this code:我遇到了这个代码:

var rectangle = new Rectangle(420, 69);
var newOne = rectangle with { Width = 420 }

I was wondering about with keyword in C# code.我想知道 C# 代码中的with关键字。 What is it for?它是做什么用的? And how can it be used?以及如何使用它? And what benefits does it bring to the language?它给语言带来了什么好处?

It's an operator used in expressions for easier duplication of an object, overriding some of it's public properties/fields (optional) with expression - MSDN它是表达式中使用的运算符,用于更轻松地复制对象,用表达式覆盖它的一些公共属性/字段(可选) - MSDN

Currently it can only be used with records.目前它只能用于记录。 But maybe there will be no such restriction in the future (assumption).但也许将来不会有这样的限制(假设)。

Here's an example how it can be used:这是一个如何使用它的示例:

// Declaring a record with a public property and a private field
record WithOperatorTest
{
    private int _myPrivateField;

    public int MyProperty { get; set; }

    public void SetMyPrivateField(int a = 5)
    {
        _myPrivateField = a;
    }
}

Now let's see how with operator can be used:现在让我们看看如何with运算符:

var firstInstance = new WithOperatorTest
{
    MyProperty = 10
};
firstInstance.SetMyPrivateField(11);
var copiedInstance = firstInstance with { };
// now "copiedInstance" also has "MyProperty" set to 10 and "_myPrivateField" set to 11.

var thirdCopiedInstance = copiedInstance with { MyProperty = 100 };
// now "thirdCopiedInstance " also has "MyProperty" set to 100 and "_myPrivateField" set to 11.

thirdCopiedInstance.SetMyPrivateField(-1);
// now "thirdCopiedInstance " also has "MyProperty" set to 100 and "_myPrivateField" set to -1.

NOTE for reference types from MSDN: MSDN 中引用类型的注意事项:

In the case of a reference-type member, only the reference to a member instance is copied when an operand is copied.在引用类型成员的情况下,复制操作数时仅复制对成员实例的引用。 Both the copy and original operand have access to the same reference-type instance.副本和原始操作数都可以访问相同的引用类型实例。

That logic can be modified by modifying the copy constructor of a record type.可以通过修改记录类型的复制构造函数来修改该逻辑。 Quote from MSDN:引用自 MSDN:

By default, the copy constructor is implicit, that is, compiler-generated.默认情况下,复制构造函数是隐式的,即编译器生成的。 If you need to customize the record copy semantics, explicitly declare a copy constructor with the desired behavior.如果您需要自定义记录复制语义,请显式声明具有所需行为的复制构造函数。

protected WithOperatorTest(WithOperatorTest original)
{
   // Logic to copy reference types with new reference
}

And in terms of what benefits it gives, I think it should be quite obvious now, that it makes copying of instances much easier and convenient.就它带来的好处而言,我认为现在应该很明显了,它使复制实例变得更加容易和方便。

Basically, the with operator will create a new object instance (records only, for now), by "coping values" from the "source" object and override some named properties in the destination object.基本上, with运算符将创建一个新的对象实例(目前仅记录),通过“处理值”从“源”对象并覆盖目标对象中的一些命名属性。

For example, instead of doing this:例如,不要这样做:

var person = new Person("John", "Doe")
{
    MiddleName = "Patrick"
};
 
var modifiedPerson = new Person(person.FirstName, person.LastName)
{
    MiddleName = "William"
};

you can do this:你可以这样做:

var modifiedPerson = person with
{
    MiddleName = "Patrick"
};

Basically, you will write less code.基本上,您将编写更少的代码。

Use this source to get more details on the example above and official documentation for more examples.使用 此来源获取有关上述示例的更多详细信息和更多示例的官方文档

Short answer is the following: with keyword in C# was added for easier copy of complicated objects, with a possibility to override some of the public properties.简短的回答是: with在C#关键字是对复杂物体,更容易的副本,添加,有可能重写某些公共属性的。 Examples are already briefly provided in the accepted answer.已接受的答案中已经简要提供了示例。

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

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