Delphi 中的“with”是否有对应的词? C# 中的命令?

发布于 2024-11-08 17:07:11 字数 307 浏览 0 评论 0原文

我想知道 C# 中是否有一个命令可以像 Delphi 中的 with command 一样使用?

// like this :
with(textbox1)
{
   .text="some text as text of text box";
   .tag=1231;
}

// 在德尔福中

with edit1 do 
 begin
   text="some text as text of edit1";
   tag=1231;
 end;

I was wondering if there is a command in C# which I can use like with command in Delphi?

// like this :
with(textbox1)
{
   .text="some text as text of text box";
   .tag=1231;
}

// in Delphi

with edit1 do 
 begin
   text="some text as text of edit1";
   tag=1231;
 end;

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

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

发布评论

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

评论(6

旧人 2024-11-15 17:07:11

不适用于已创建的实例。

但是,当您创建新实例时,您可以执行以下操作:

var textbox1 = 
   new Textbox
   {
       Text = "some text as text of text box",
       Tag = 1231
   };

Not for already created instances.

However, when you create a new instance you can do:

var textbox1 = 
   new Textbox
   {
       Text = "some text as text of text box",
       Tag = 1231
   };
廻憶裏菂餘溫 2024-11-15 17:07:11

不,C# 中不存在这种情况。

No, that does not exist in C#.

蓝眸 2024-11-15 17:07:11

不,它在 C# 中不存在,但是,当创建对象时,您可以这样做:

var textbox1 = new TextBox {
    Text = "some text as text of text box";
    Tag = 1231
};

No, it does not exist in C#, however, when creating an object, you can do like this:

var textbox1 = new TextBox {
    Text = "some text as text of text box";
    Tag = 1231
};
听你说爱我 2024-11-15 17:07:11

已经11年了。

C#添加了记录功能。您可以对 C# 记录使用 with 关键字。

由于记录是不可变的,当您使用 with 时,它会实例化并返回一个新对象。

public record FullName(string FirstName
                     , string LastName);

var name = new FullName("Jack", "Beyer");

var newName = name with { FisrtName = "Mohsen" };

It's been 11 years.

C# has added the records feature. You can use the with keyword with c# records.

Since records are immutable it instantiates and returns a new object when you use with.

public record FullName(string FirstName
                     , string LastName);

var name = new FullName("Jack", "Beyer");

var newName = name with { FisrtName = "Mohsen" };
傾旎 2024-11-15 17:07:11

有一种叫做使用的东西,但是比较对于 Delphi/Pascal 来说,它的工作方式更像是 try/finally。

There's something called using, but compared to Delphi/Pascal it's works more like try/finally.

寄与心 2024-11-15 17:07:11

不,但根据您想要执行的操作,以下方法可以工作:

TextBox t = textbox1;

t.text="some text as text of text box";
t.tag=1231;

No but depending on what you are trying to do, the following would work:

TextBox t = textbox1;

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