我应该使用new Type()还是键入()来调用构造函数

时间:2022-10-03 15:29:46

Both syntaxes are equivalent (at least I suppose they are).

两个语法都是等价的(至少我认为它们是等价的)。

let o1 = new Object()

or

let o2 = Object()

Which way do you use more often? What about readability issues?

你更常用哪一种方式?可读性问题呢?

3 个解决方案

#1


11  

They are the same.

他们是相同的。

I prefer using 'new', with little good reason other than it is what I am accustomed to in other languages, and it makes it easier to findstr/grep for constructor calls (lacking smart tools to 'find all references' in a solution).

我更喜欢使用“new”,除了我在其他语言中习惯使用“new”之外,几乎没有什么好的理由,而且它使构造函数调用的findstr/grep更加容易(缺少在解决方案中“查找所有引用”的智能工具)。

#2


18  

I feel like omitting "new" is a bit more functional, so that's my preference. I like that you can treat a constructor just like any other function returning an instance of a type.

我觉得省略“new”更实用一些,所以这是我的偏好。我喜欢这样,您可以将构造函数看作是返回类型实例的任何其他函数。

#3


10  

I second what kvb says. Additionally, I omit new because this allows me to invoke members directly on the constructed object, just like in C#. For example, the follow code works:

我赞成kvb的说法。另外,我省略了new,因为这允许我直接在构造对象上调用成员,就像c#一样。例如,下面的代码工作:

DateTime(2012, 12, 21).ToString()

The following code does not work and results in a compiler error:

以下代码无效,导致编译器错误:

new DateTime(2012, 12, 21).ToString()

So now I need to parenthesize the new expression in order to get it to compile:

所以现在我需要插入这个新表达式来编译:

(new DateTime(2012, 12, 21)).ToString()

I don't like extra parentheses, so I avoid using new now.

我不喜欢额外的括号,所以我现在避免使用new。

The one case where I would still use it is when I am creating an object that implements IDisposable. This is because if I omit the new keyword when creating a IDisposable the F# compiler gives a warning:

我仍然使用它的一个例子是,当我创建一个实现IDisposable的对象时。这是因为如果我在创建IDisposable时省略了new关键字,f#编译器会发出警告:

let writeFile =
    let writer = StreamWriter(@"hello.txt")
    writer.WriteLine("hello")

Results in this:

结果:

warning FS0760: It is recommended that objects that support the IDisposable interface are created using 'new Type(args)' rather than 'Type(args)' to indicate that resources may be owned by the generated value

警告FS0760:建议使用“new Type(args)”而不是“Type(args)”创建支持IDisposable interface的对象,以表明资源可能属于生成的值

I can get rid of the warning by adding in the new keyword:

我可以通过添加新的关键字来消除警告:

let writeFile =
    let writer = new StreamWriter("hello.txt")
    writer.WriteLine("hello")

The warning also causes me to realize that I should really have a use binding for my IDisposable:

这个警告也让我意识到,我的IDisposable本会有一个真正的用处:

let writeFile =
    use writer = new StreamWriter("hello.txt")
    writer.WriteLine("hello")

But if I wrote my code to use new all the time then I would never get the warning reminding me that I am working with an IDisposable!!!

但是如果我一直写代码来使用new,我将永远不会得到警告提醒我我正在使用IDisposable!!!

It also makes for less typing. ;-)

这也减少了打字量。:-)

#1


11  

They are the same.

他们是相同的。

I prefer using 'new', with little good reason other than it is what I am accustomed to in other languages, and it makes it easier to findstr/grep for constructor calls (lacking smart tools to 'find all references' in a solution).

我更喜欢使用“new”,除了我在其他语言中习惯使用“new”之外,几乎没有什么好的理由,而且它使构造函数调用的findstr/grep更加容易(缺少在解决方案中“查找所有引用”的智能工具)。

#2


18  

I feel like omitting "new" is a bit more functional, so that's my preference. I like that you can treat a constructor just like any other function returning an instance of a type.

我觉得省略“new”更实用一些,所以这是我的偏好。我喜欢这样,您可以将构造函数看作是返回类型实例的任何其他函数。

#3


10  

I second what kvb says. Additionally, I omit new because this allows me to invoke members directly on the constructed object, just like in C#. For example, the follow code works:

我赞成kvb的说法。另外,我省略了new,因为这允许我直接在构造对象上调用成员,就像c#一样。例如,下面的代码工作:

DateTime(2012, 12, 21).ToString()

The following code does not work and results in a compiler error:

以下代码无效,导致编译器错误:

new DateTime(2012, 12, 21).ToString()

So now I need to parenthesize the new expression in order to get it to compile:

所以现在我需要插入这个新表达式来编译:

(new DateTime(2012, 12, 21)).ToString()

I don't like extra parentheses, so I avoid using new now.

我不喜欢额外的括号,所以我现在避免使用new。

The one case where I would still use it is when I am creating an object that implements IDisposable. This is because if I omit the new keyword when creating a IDisposable the F# compiler gives a warning:

我仍然使用它的一个例子是,当我创建一个实现IDisposable的对象时。这是因为如果我在创建IDisposable时省略了new关键字,f#编译器会发出警告:

let writeFile =
    let writer = StreamWriter(@"hello.txt")
    writer.WriteLine("hello")

Results in this:

结果:

warning FS0760: It is recommended that objects that support the IDisposable interface are created using 'new Type(args)' rather than 'Type(args)' to indicate that resources may be owned by the generated value

警告FS0760:建议使用“new Type(args)”而不是“Type(args)”创建支持IDisposable interface的对象,以表明资源可能属于生成的值

I can get rid of the warning by adding in the new keyword:

我可以通过添加新的关键字来消除警告:

let writeFile =
    let writer = new StreamWriter("hello.txt")
    writer.WriteLine("hello")

The warning also causes me to realize that I should really have a use binding for my IDisposable:

这个警告也让我意识到,我的IDisposable本会有一个真正的用处:

let writeFile =
    use writer = new StreamWriter("hello.txt")
    writer.WriteLine("hello")

But if I wrote my code to use new all the time then I would never get the warning reminding me that I am working with an IDisposable!!!

但是如果我一直写代码来使用new,我将永远不会得到警告提醒我我正在使用IDisposable!!!

It also makes for less typing. ;-)

这也减少了打字量。:-)