Is there an equivalent for Delphi's “with” command in c#?

ぐ巨炮叔叔 提交于 2020-12-26 05:54:30

问题


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;

回答1:


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
   };



回答2:


No, that does not exist in C#.




回答3:


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
};



回答4:


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;



回答5:


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



来源:https://stackoverflow.com/questions/6055552/is-there-an-equivalent-for-delphis-with-command-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!