C#交替创建对象实例并指定其中一个属性的方法 [英] C# alterntive way of crating an instance of an object with assigning one of its properties

查看:71
本文介绍了C#交替创建对象实例并指定其中一个属性的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者,我想知道两种方式相同吗?

前四行代码是否与最后两行相同?



I'm a beginner in C# and I would like to know of both ways down are equal?
Is the first 4 lines of code are the same as the last 2 lines?

var genre = new Genre()
{
   Name = "Search results for " + q
};
//--------------------------------------  
      
Genre newGenre = new Genre();
newGenre.Name = "Search results for" + q;

推荐答案

两种方式都相同,但你会更频繁地看到第二种方式,因为它更容易阅读。关于你在做什么更清楚。
Both ways are equal but you'll see the second way more often because it is easier to read for most. It is more clear as to what you are doing.


这篇关于C#交替创建对象实例并指定其中一个属性的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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