C# 中的等效语法是什么(如果有)?

发布于 2024-10-04 14:07:26 字数 63 浏览 6 评论 0原文

C# 是否有 VB6 的等效项

With 
End With

Does C# have any equivalent for VB6

With 
End With

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

千と千尋 2024-10-11 14:07:26

没有什么完全等效的东西,但 C# 3 获得了在构造时设置属性的能力:

var person = new Person { Name = "Jon", Age = 34 };

和集合:

var people = new List<Person>
{
    new Person { Name = "Jon" },
    new Person { Name = "Holly"}
};

它绝对不能替代所有使用With,但其中一些值得了解。

There's nothing quite equivalent, but C# 3 gained the ability to set properties on construction:

var person = new Person { Name = "Jon", Age = 34 };

And collections:

var people = new List<Person>
{
    new Person { Name = "Jon" },
    new Person { Name = "Holly"}
};

It's definitely not a replacement for all uses of With, but worth knowing for some of them.

原野 2024-10-11 14:07:26

C# 没有任何等效语法。最接近的是对象初始值设定项,但它们并不相同:

var obj = new SomeThing {
    Height = 100,
    Text = "Hello, World",
    ForeColor = System.Drawing.Color.Green
}

C# does not have any equivalent syntax. The closest are object initializers, but they are not the same:

var obj = new SomeThing {
    Height = 100,
    Text = "Hello, World",
    ForeColor = System.Drawing.Color.Green
}
断念 2024-10-11 14:07:26

不。

最接近的是对象和列表初始值设定项

Person p = new Person()
{
    FirstName = "John",
    LastName = "Doe",
    Address = new Address()
    {
        Street = "1234 St.",
        City = "Seattle"
    }
};

No.

What comes close are object and list initializers.

Person p = new Person()
{
    FirstName = "John",
    LastName = "Doe",
    Address = new Address()
    {
        Street = "1234 St.",
        City = "Seattle"
    }
};
毅然前行 2024-10-11 14:07:26

它绝不是等价的,但是,如果您想减少打字量,您可以这样做。

{
  var o = myReallyReallyReallyReallyLongObjectName;
  o.Property1 = 1;
  o.Property2 = 2;
  o.Property3 = 3;
}

It is by no means an equivalent, however, if it is the typing you're trying to reduce, you can do.

{
  var o = myReallyReallyReallyReallyLongObjectName;
  o.Property1 = 1;
  o.Property2 = 2;
  o.Property3 = 3;
}
玉环 2024-10-11 14:07:26

没有与 Visual Basic 的 With 关键字等效的 C#。

There is no C# equivalent to Visual Basic's With keyword.

情未る 2024-10-11 14:07:26

一种近乎等效的方法是调用类成员的方法。您不必在类成员中重复命名拥有对象 - 它隐含在以下事实中:该函数是为给定实例调用的成员。

出于这个原因,我怀疑在 C# 中直接等效于 With/End With 是否是一个好主意。如果您发现自己在给定范围内一遍又一遍地键入对象的名称,则很好地表明所讨论的代码将成为该对象的类的良好方法。

One near-equivalent would be calling a method that is a member of a class. You don't have to repeatedly name the owning object inside class members - it's implicit in the fact that the function is a member, called for a given instance.

I doubt a direct equivalent of With/End With is a good idea in C# for this reason. If you found yourself typing an object's name over and over in a given scope, it's a good indication that the code in question would make a good method on that object's class.

没有伤那来痛 2024-10-11 14:07:26

没有直接的等价物。您可以在构造时设置属性,正如其他人所解释的那样,或者您可以将表达式分配给具有短名称的变量。以下内容在语义上应该是等效的:

With <expression>
    .something ...
    .somethingElse ...
End With

var w = <expression>;
w.something ...
w.somethingElse ...

There is no direct equivalent. You can set properties on construction, as others explained, or you can assign your expression to a variable with a short name. The following should be semantically equivalent:

With <expression>
    .something ...
    .somethingElse ...
End With

var w = <expression>;
w.something ...
w.somethingElse ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文