C#为什么不能通过实例方法初始化字段? [英] C# why cannot fields be initialized via instance method?

查看:74
本文介绍了C#为什么不能通过实例方法初始化字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我很好奇为什么不允许通过实例方法初始化字段?



示例代码:

Hi. I am curious about why it isn't allowed to initialize a field via an instance method?

An example code:

class SomeRandomName
        {
            public int age = ReturnAge();

            public int ReturnAnAge()
            {
                return 23;
            }
        }





我的尝试:



看看Microsoft Docs。它提到实例字段不允许引用另一个实例字段,但没有说明实例方法。



What I have tried:

Looked at Microsoft Docs. It mentions that an instance field is not allowed to refer to another instance field but doesn't say anything about instance methods.

推荐答案

部分原因是你需要拼写你的方法在定义和调用中以相同的方式命名...



但主要是因为字段初始化是在实例构造之前执行的:构造函数必须是第一个调用的方法实例(或其他方法不能依赖任何其他信息)。

字段必须在实际构造函数开始执行之前初始化,或者它可以设置被字段初始化程序覆盖的值!



你可以用静态方法做到这一点:

Partly because you need to spell your method name the same way in the definition and the call...

But mostly because field initialization is performed before instance construction: and the constructor must be the first method called in the instance (or the other methods could not rely on any other information).
And fields must be initialized before the actual constructor begins to execute, or it could set values that are overwritten by the field initializers!

You can do it with static methods:
class SomeRandomName
    {
    public int age = ReturnAge();

    public static int ReturnAge()
        {
        return 23;
        }
    }

因为静态方法无法访问任何实例数据,因此必须在创建实例之前完全准备好。







Because a static method cannot access any instance data, and so must be completely prepared before an instance is created.



引用:

所以实例field必须包含在编译时已知的数据,或者它应该使用静态方法,因为静态类是在任何实例对象之前使用其默认静态构造函数创建的。我理解正确吗?但是,如果您将该字段设为指向此类型对象的对象引用,则它可以工作。你能解释一下为什么会这样吗?



如果你使用这样的嵌套类,情况也一样:

So the instance field must contain data that is known at compile time or it should use a static method because the static class is created with its default static constructor before any instance object. Do I understand it correctly? But it works if you make the field an object reference that points to object of this type. Can you explain why this happens?

Also the same is the case if you use a nested class like this:

class SomeRandomName
    {
    public InnerRandomClass irc = new InnerRandomClass();
    
    public class InnerRandomClass
        {
        
        }
    
    }







引用:

因此实例字段必须包含编译时已知的数据

So the instance field must contain data that is known at compile time

否,初始化数据不必在编译时知道:

No, the initializer data doesn't have to be known at compile time:

Quote:

class SomeRandomName

{

public int age = ReturnAge() ;



public static int ReturnAge()

{

MyDB db = new MyDB();

返回db.GetAge();

}

}

class SomeRandomName
{
public int age = ReturnAge();

public static int ReturnAge()
{
MyDB db = new MyDB();
return db.GetAge();
}
}

没问题 - MyDB类从SQL读取数据并返回它。

Is fine - the MyDB class reads the data from SQL and returns it perhaps.

引用:

它应该使用静态方法,因为静态类是用它创建的在任何实例对象之前的efault静态构造函数。

it should use a static method because the static class is created with its default static constructor before any instance object.

是的!



Yes!

引用:

但如果你做的话它会起作用该字段是指向此类对象的对象引用。你能解释一下为什么会这样吗?

But it works if you make the field an object reference that points to object of this type. Can you explain why this happens?

不,因为这对我没有任何意义! :笑:

再试一次,举例说明。

No, because that doesn't make any sense to me! :laugh:
Try explaining again, with examples.

引用:

也是如果你使用嵌套类

就是这种情况那不是问题,因为嵌套类只是一个包含所有它的父类的全名的类 - 实际的类没有构造任何不同于非嵌套类,只有名称受嵌套影响。

That's not a problem because a "nested class" is just a class with a full name that includes all it's "parent classes" - the actual class is not constructed any different from a non-nested class, only the name is affected by nesting.


这篇关于C#为什么不能通过实例方法初始化字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆