我是否必须存储方法 C# 中的 return 值

Do I have to store return values from methods C#

我刚开始使用 C#,我注意到即使我 不要存储方法中的 return 值。示例:假设我有一个方法

public class Hello(){
    public bool GoodMorning(){
        return true;
    }
}

然后这个有效:

Hello.GoodMorning();

What happens to the return value that I never store?

它被丢弃了。

Is is bad practice to not store the return value?

不,不是真的。有时我们不使用返回的值。

What happens to the return value that I never store

他们都住在一所致力于被忽视的 return 价值观的小房子里。不,说真的,所有非引用对象(垃圾收集等)都会发生同样的情况。

Is is bad practice to not store the return value?

不是真的。有时一个方法 return 是您不感兴趣的东西,在这种情况下可以忽略它们

它根本不会捕获值。没有什么不好的事情发生。有时 return 值对您来说可能并不重要,只有在您希望从中访问状态或生产时才存在。即使它确实捕获了变量,它也会像对待所有局部变量一样对待它,并在退出范围后将其丢弃。