gpt4 book ai didi

c# - 为什么我们不能将对象直接分配给返回相同类型的方法?

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:38 25 4
gpt4 key购买 nike

我是出于好奇才问这个问题的,而且我知道还有其他方法可以这样做。

我想知道,如果某个方法正在返回某种类型的对象,我们可以直接修改该对象的属性(即时 - 无需将其引用到局部变量中)。

为什么我们需要将其引用到局部变量中以更改对象本身?

在编译器级别会存在哪些逻辑困难会限制程序员这样做。

参见下面的示例代码:

static Demo StaticDemoInstance;
static void Main(string[] args)
{
//allowed: means I can directly modify property of static instance
// received from method
GetDemo().Name = "UpdateDemo";

//allowed: means I can get instance and overwrite it with other instance
// but not directly from method
Demo d = GetDemo();
d = new Demo("NewCreatedDemo", false);

//not allowed: means I can't do second step directly on method
// question:
// when I can update instance property without receiving instance on local variable
// what possible violation/difficulty (in compiler) will be there so it doesn't allow this
GetDemo() = new Demo("UpdatedDemoFromGetMeth", false);
}

static Demo GetDemo() => StaticDemoInstance ??
StaticDemoInstance = new Demo("StaticDemo", false);

最佳答案

你总是可以使用 Ref return

Starting with C# 7.0, C# supports reference return values (ref returns). A reference return value allows a method to return a reference to a variable, rather than a value, back to a caller. The caller can then choose to treat the returned variable as if it were returned by value or by reference. The caller can create a new variable that is itself a reference to the returned value, called a ref local.

限制

There are some restrictions on the expression that a method can return as a reference return value. Restrictions include:

  • The return value must have a lifetime that extends beyond the execution of the method. In other words, it cannot be a local variable in the method that returns it. It can be an instance or static field of a class, or it can be an argument passed to the method. Attempting to return a local variable generates compiler error CS8168, "Cannot return local 'obj' by reference because it is not a ref local."

  • The return value cannot be the literal null. Returning null generates compiler error CS8156, "An expression cannot be used in this context because it may not be returned by reference."

  • A method with a ref return can return an alias to a variable whose value is currently the null (uninstantiated) value or a nullable type for a value type.

  • The return value cannot be a constant, an enumeration member, the by-value return value from a property, or a method of a class or struct. Violating this rule generates compiler error CS8156, "An expression cannot be used in this context because it may not be returned by reference."

一个非常人为的例子而不是我推荐的,但是它确实实现了你的目标(学术上)

示例

static Demo StaticDemoInstance;

...

static ref Demo GetDemo()
{
if (StaticDemoInstance == null)
StaticDemoInstance = new Demo("StaticDemo", false);

return ref StaticDemoInstance;
}

..

GetDemo() = new Demo("UpdatedDemoFromGetMeth", false);

更新

可以通过更复杂的示例和修改看到它的使用

例子

static Demo Test()
{
return StaticDemoInstance;
}

...

GetDemo() = new Demo("UpdatedDemoFromGetMeth", false);
var someObject = Test();
someObject= new Demo("Test", false);

Console.WriteLine(StaticDemoInstance.Name);
Console.WriteLine(someObject.Name);

输出

UpdatedDemoFromGetMeth
Test

Test 方法(不是 ref 返回)并且只给你一个引用的副本(因为缺少更好的术语),如果你覆盖它,你的静态变量到 StaticDemoInstance 不变

关于c# - 为什么我们不能将对象直接分配给返回相同类型的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50711683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com