简体   繁体   English

在 C# 中创建 object 的实例时,带和不带 () 符号有什么区别?

[英]What is the difference between with and without () symbol in creating an instance of an object in C#?

I have a class Product我有一个 class产品

public class Product
{
    public string Name { get; set; }
}

Could you please help me to explain the difference between two case of creating the instance of Product class below?您能否帮我解释一下创建下面的Product class 实例的两种情况之间的区别?

var instanceOne = new Product
{
    Name = "iPhone 4"
}

and

var instanceTwo = new Product()
{
    Name = "iPhone 5"
}

Thanks谢谢

There is no difference, they're the same (assuming you meant to initialize the Name property, not define it, and it can't be static), except for one very specific case:没有区别,它们是相同的(假设您要初始化Name属性,而不是定义它,并且它不能是静态的),除了一种非常特殊的情况:

Product instanceOne = new()
{
    Name = "arf"
};

If you use type-targetted new , you have to use the new() {...} version, because new {...} defines an anonymous type instead.如果您使用以类型为目标的new ,则必须使用new() {...}版本,因为new {...}定义了一个匿名类型。

The () is redundant in the following example: ()在以下示例中是多余的:

var instanceTwo = new Product()
{
    Name = "iPhone 5"
}

You can just write:你可以写:

var instanceTwo = new Product // <----- () deleted
{
    Name = "iPhone 5"
}

There is no difference between these two.这两者之间没有区别。

Please note that object initalisers allow specifying constructor parameters too.请注意object 初始化程序也允许指定构造函数参数。

class Cat 
{
    public Cat() {}

    public Cat(string name) { Name = name; }

    public string Name { get; set; }

    public string Color { get; set; }
}

(...)

var lightning = new Cat(Name: "Lightning")
{
    Color = "White"
};

var storm = new Cat
{
    Name = "Storm",
    Color = "Gray"
};

A tiny bit more多一点

Please also note that另请注意

var storm = new Cat
{
    Name = "Storm",
    Color = "Gray"
};

is not the same as不一样

var storm = new Cat();
storm.Name = "Storm";
storm.Color = "Gray";

The former results in:前者导致:

var __a = new Cat();
__a.Name = "Storm";
__a.Color = "Gray"; 
var storm = __a;

as shown in Overview of C# 3.0 .C# 3.0 概述中所示。

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

相关问题 C#中的赋值和创建字符串实例有什么区别? - What is the difference Between Assignment and Creating Instance of String in C#? C#在类外部创建静态对象与在类内部创建静态对象有什么区别? - C# What is the difference between creating a static object outside a class VS creating it inside a class? C#中Array和object []有什么区别? - What is the difference between Array and object[] in C#? 在VB.NET和C#之间创建实例的区别 - Difference in creating an instance between VB.NET & C# C#中表达式中的新Object()和新Object {}之间有什么区别? - What is the difference between new Object() and new Object{} in expressions in C# C#中`new object()`和`new {}`有什么区别? - What is the Difference Between `new object()` and `new {}` in C#? 命名空间,类,对象和实例之间有什么区别? - What is the difference between a namespace, a class, an object and an instance? C#中类变量和实例变量的区别 - Difference between class and instance variables in C# 在C#中返回IEnumerable实例和yield return语句之间的确切区别是什么? - What is the exact difference between returning an IEnumerable instance and the yield return statement in C# OOPS概念:在C#中传递对象引用和创建类对象有什么区别? - OOPS Concepts: What is the difference in passing object reference to interface and creating class object in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM