开发者

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

开发者 https://www.devze.com 2023-03-06 19:02 出处:网络
I was wondering if there is a command in C# which I can use like with command in Delphi? // li开发者_JAVA技巧ke this :

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

// li开发者_JAVA技巧ke 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;


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


No, that does not exist in C#.


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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消