为什么不能非静态字段进行内部结构初始化? [英] Why can't non-static fields be initialized inside structs?

查看:186
本文介绍了为什么不能非静态字段进行内部结构初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个code座:

struct Animal
{
    public string name = ""; // Error
    public static int weight = 20; // OK

    // initialize the non-static field here
    public void FuncToInitializeName()
    {
        name = ""; // Now correct
    }
}

  • 为什么我们可以初始化一个静态字段中的非静态字段内的结构,但不?
  • 为什么我们必须初始化非静态的方法机构?
    • Why can we initialize a static field inside a struct but not a non-static field?
    • Why do we have to initialize non-static in methods bodies?
    • 推荐答案

      看一看<一href="http://www.dotnetconsult.co.uk/weblog/commentview.aspx/03805a0c-525f-4b7d-b0a0-f5f2773b4a7c">Why不能值类型有默认的构造函数?

      这篇关于为什么不能非静态字段进行内部结构初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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