With block equivalent in C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
VB.NET has a With block that lets you call multiple members on the same object without repeating the variable name. C# has no direct equivalent. However, C# offers several constructs that achieve similar conciseness: object initializers, local variables with short names, extension methods, and the using alias. Each approach trades off readability, safety, and applicability differently.
The VB.NET With Block
Inside the With block, the dot prefix refers to myButton. Every member access is implicitly scoped to that object.
C# Alternative 1: Object Initializer
When creating a new object, C# object initializers provide the closest syntax:
This only works at construction time. You cannot use it to configure an existing object after creation.
C# Alternative 2: Short Local Variable
The simplest workaround for an existing object — assign it to a short-named variable:
Since b is a reference (for reference types), changes apply to the original object. This is the most common pattern in C# codebases.
C# Alternative 3: Extension Method for Fluent Configuration
Create a generic extension method that accepts an Action<T>:
The Apply method passes the object into the lambda, simulating a With block. Returning obj enables chaining if needed.
C# Alternative 4: Fluent Builder / Method Chaining
Many C# libraries use fluent APIs that return this from each method:
You can design your own classes to support this pattern:
C# Alternative 5: using static for Scoped Methods
When calling many static methods from the same class, using static reduces repetition:
This does not apply to instance members but eliminates the class prefix for static calls.
C# 12 Primary Constructors and init Properties
Modern C# provides init properties and required members that reduce boilerplate at construction:
This enforces immutability after construction — a different goal from With, but it addresses the same verbosity problem.
Comparison Table
| Approach | Works on Existing Objects | Type-Safe | Requires Extra Code |
| Object initializer | No (creation only) | Yes | No |
| Short local variable | Yes | Yes | No |
Apply extension | Yes | Yes | One-time extension method |
| Fluent builder | Depends on design | Yes | Per-class builder |
using static | Static members only | Yes | No |
Common Pitfalls
- Using object initializers on existing objects: Object initializers only work in
newexpressions. You cannot writeexistingButton { Text = "X" }— use a local variable or lambda instead. - Value type gotcha with short variable alias: For structs,
var b = existingStructcopies the value. Changes tobdo not affect the original. Useref var b = ref existingStruct(C# 7+) if you need a reference. - Overusing the Apply pattern: Wrapping every property assignment in a lambda adds a closure allocation and reduces debuggability. Use it sparingly for configuration-heavy sections, not for one or two property sets.
- Fluent builders hiding mutation: If a builder mutates the object in place (instead of creating copies), calling
.Build()twice returns the same modified object. Document whether the builder is reusable or single-use. - Assuming C# will add a
withkeyword for classes: C# records havewithexpressions, but they create a copy — they do not mutate the original.var b2 = b1 with { Text = "New" }gives a new record instance, not aWithblock.
Summary
- C# has no direct
Withblock equivalent from VB.NET - Object initializers handle the creation case cleanly
- For existing objects, assign to a short local variable — simplest and most idiomatic
- The
Apply<T>extension method simulates aWithblock using a lambda - Fluent APIs and method chaining provide a similar developer experience for well-designed classes
- C# records support
withexpressions, but they create copies rather than mutating in place

