C# equivalent of Pascal "With"

  • Thread starter Thread starter DrOrbit
  • Start date Start date
D

DrOrbit

In Pascal/Delphi you can say

With MyStructure do begin
Field1 := something;
Field2 := somethingElse;
:
// and so on...
end;

"With MyStructure" avoids having to prepend "MyStructure" to every
reference to MyStructure's fields. Is there an equivalent in C#?
 
"With MyStructure" avoids having to prepend "MyStructure" to every
reference to MyStructure's fields. Is there an equivalent in C#?

No.

Many people coming to C# from languages that have it, really want it though.

Mark
 
Given "instanceOfAClassWithAReallyLongName",
the idiom is something like
ClassWithAReallyLongName c = instanceOfAClassWithAReallyLongName;
c.Property1 = x;
c.Property2 = y;
//etc.
but then, you might want to build a helper class that
just handles the assignment.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
 
Someone onced mentioned this language feature during a whiteboard
open-session talk with Anders Hejlsberg. The conversation went
something like this:

"I wish that C# had something like Pascal's 'with' for record
selection."

Hejlsberg's answer was:

"Well, there's no harm in wishing, is there?"

For my part, I agree with Mr. Hejlsberg: I don't miss this feature at
all, given that I now have an IDE that fills in most of those long
names for me. It made some sense back in the days of writing source
code in text editors. These days I think it would cause more problems
(difficulties reading code) than it would solve (ease of editing).
 
I think the best way to do this is to create a method in the target
class; within that method, "this." is optional (and better left out).

Peace,
--Carl
 
Back
Top