简体   繁体   English

如果注释掉成员的构造函数调用,为什么我的程序不起作用?

[英]Why doesn't my program work if I comment out a member's constructor call?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{
    class Test
    {
        public StringBuilder a;

        /*public Test()
        {
            a = new StringBuilder();
        } */
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test testobj = new Test();
            testobj.a.Append("Hello");
            Console.WriteLine(testobj.a);
            Console.ReadLine();
        }
    }
}

Shouldn't the default constructor for Test Class call every default constructor for every file in the class? 测试类的默认构造函数不应该为该类中每个文件的每个默认构造函数调用吗?

You seem to have a C++ background. 您似乎具有C ++背景。 You need to make sure that you understand object references in C# better. 您需要确保您更好地理解C#中的对象引用。 a is of reference type and is null initialized by default. a是引用类型,默认为null初始化。

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

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