c#string用new操作符实例化后报错

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

namespace Study2
{
    class Program
    { 
        static void Main(string[] args)
        {

            string bob = new string();

            bob = "Hello World";
            Console.WriteLine(bob);   

        }
        
    }

错误提示 string does not contain a constructor that takes 0 arguments;

为什么写成 string bob = “Hello World”;就没有问题?

原因很简单,因为它不支持 0 个参数的构造函数。 

https://docs.microsoft.com/en-us/dotnet/api/system.string.-ctor?view=net-5.0

 

可以改为  string bob = new string("");

string类的构造函数都在这儿了,没有0个参数的,所以会报错

应该这样写:

string bob=“”;