简体   繁体   English

将vb.net转换为C#:一行代码

[英]Translate vb.net to C#: one line of code

This link offers sample code to infer the schema of an XML file, in VB.NET. 该链接提供了示例代码,以在VB.NET中推断XML文件的架构。 One particular line fails in my translation to C#, namely, 我翻译成C#时,有一行特别失败,即,

Dim schema As XmlSchema = schemaSet.Schemas()(0)

My translation is 我的翻译是

XmlSchema schema = schemaSet.Schemas()[0];

I cannot see what is wrong with my translation? 我看不到翻译有什么问题吗?

XmlSchemaSet.Schemas() returns an ICollection which you can't access by index. XmlSchemaSet.Schemas()返回一个ICollection ,您不能通过索引访问它。 If you use use .NET 3.5 you can use Linq to do: 如果使用.NET 3.5,则可以使用Linq来执行以下操作:

schemaSet.Schemas().Cast<XmlSchema>().First();

Otherwise you have to use a foreach loop and stop after the first iteration. 否则,您必须使用foreach循环并在第一次迭代后停止。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM