在 New 语句之后内联调用实例方法 [英] Call instance method inline after New statement

查看:26
本文介绍了在 New 语句之后内联调用实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此代码转换为 VB.net

How can i convert this code to VB.net

public void SetBooks(IEnumerable<Book> books)
    {
        if (books == null)
            throw new ArgumentNullException("books");

        new System.Xml.Linq.XDocument(books).Save(_filename);
    }

http://converter.telerik.com/ 中说:

Public Sub SetBooks(books As IEnumerable(Of Book))
        If books Is Nothing Then
            Throw New ArgumentNullException("books")
        End If
        New System.Xml.Linq.XDocument(books).Save(_filename)
End Sub

但是visual studio 说语法错误."因为新"

But visual studio says "Syntax error." because of "New"

这种情况下的关键字是什么,我在 Google 上搜索过,但没有结果.

What is the keyword for this situation, i searched on Google but no result.

推荐答案

在 VB.NET(与 C# 相对)中,您不能初始化一个对象并在一个语句中使用它.你需要两个:

You cannot initialize an object and use it in one statement in VB.NET (as opposed to C#). You need two:

Dim doc = New System.Xml.Linq.XDocument(books)
doc.Save(_filename)

在 C# 中,构造函数返回创建对象的实例,在 VB.NET 中则不然.

In C# the constructor returns the instance of the created object, in VB.NET not.

这篇关于在 New 语句之后内联调用实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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