Is there a way in C # to access the properties of an object in old, Pascal with a "keyword"?

This is the Pascal sample I want to achieve in C #:

With myBook do
  Title  := 'Some Book';
  Author := 'Victor John Saliba';
  ISBN   := '0-12-345678-9';
  Price  := 25.5;



source to share

3 answers

You can find an explanation here.


  • Little or nonexistent benefits of reading. We thought the benefits of reading were small or nonexistent. I won't go so far as to say that the with statement makes the code less readable, but some people probably will.
  • Increased language complexity. Adding a with statement will make the language more complex. For example, VB had to add a new syntax to the language to eliminate potential ambiguity between the local variable (Text) and the "with" property of the target (.Text). Other ways to deal with this problem also involve language complexity. Another approach is to push the scope and force the property to hide the local variable, but then there is no way to refer to the local without adding escape syntax.
  • legacy of C ++. C ++ has never had a c operator, and the absence of such a statement is not usually considered by C ++ developers. Also, we didn't feel that the other changes were changes in the types of people who write, platform changes, other language changes, etc. - made with more necessary statements.


Only when designing.

var foo = new Foo
  Title = "lol",
  Author = "Som Gai",
  ISBWhatever = "111"


VB.NET has the 'with' keyword, but C # doesn't.



No no. This has been discussed before and most people don't want to.

This hurts readability, creates ambiguities, makes debugging easier, and IntelliSense largely compensates for usability.

To respond to your last comment, you can of course write:

myBook.Title = "Some Book";




All Articles