如何在c#中初始化类的新对象

时间:2014-01-30 13:35:51

标签: c# vb.net

Dim objConn As New Pclient.Connection

上面的命令为Pclient类创建新对象。但如何在C#

中做同样的事情

它在VB中创建一个新对象,但是如何用C#

编写

1 个答案:

答案 0 :(得分:5)

var objConn = new Pclient.Connection();

或者您可以指定类型

  Pclient.Connection  objConn = new Pclient.Connection();

您可以尝试

等在线转换器
  1. developerfusion
  2. converter telerik
  3. 并且您还可以更好地了解Initialize Objects by Using an Object Initializer


    vb.net

    Dim hero As New SuperHero
    
    With hero 
      .Name = "SpamMan" 
      .PowerLevel = 3 
    End With
    

    in Visual Studio 2008 +

    Dim hero As New superhero With {.Name = "SpamMan", .PowerLevel = 3}
    

    <强> C#

    SuperHero hero = new SuperHero() { Name = "SpamMan", PowerLevel = 3 }; 
    

    取自VB.NET and C# Comparison

    的示例代码