In the C# chain, methods may return the same or different objects at any stage. The WITH block just calls the methods on the original object. ```csharp IThing thing = new ThingBuilder() // returns ThingBuilder instance .AddTitle("I'm a Thing!") // returns same ThingBuilder instance .CreatePreciousThing() // returns PreciousThing instance .SetPreciousLevel(100); // returns same PreciousThing instance // thing is now a PreciousThing instance ```
Al Gonzalez
Posts
-
Fluent Api in Pascal, nothing earth-shattering ... -
GIT Time again - what am I missing?With Git you don't just have all the code on your local machine, you also have (or can have) all the branches, versions and tags on the local machine. That way you can compare or even revert code with any prior version without connecting a centralized server. Now when you want to share your changes, you can connect and push to say GitHub and pull to get changes from other users. Branching in Git is also more lightweight since it isn't a full copy of the code base. BTW, while PRs are useful and desirable when working with other developers, they are a feature of online git services like Github and Gitlab not Git itself. Depending on your settings/configuration, you CAN push changes to main (trunk) without anybody seeing it first.
-
Keeping trackJust saw the reply from Andreas @User-3771503 and will be taking a look the **AzureDevOps.Technology-Framework-Monitor** project. Seems all the meta data could be useful. Mine is much simpler, but does store some details in a [LiteDB](https://www.litedb.org/) database file.
-
Keeping trackI use the code repository API (in this case Azure DevOps) to scan all our projects for a `.csproj` files to get the .NET version being used. It can be modified if we have other Languages/SDKs/Frameworks in use. For example reading the `package.json` for the ***node*** and ***npm*** versions.
-
Find My Keyboard CaretI seem to have the issue when typing on my laptop and the touchpad thinks I clicked on something. I don't seem to have the issue when using an external keyboard. Changing the touchpad sensitivity seems to help.
-
C# 9 nullable reference alertI would expect code that casts a
List<string>
to aIList
to manage nulls. If I just want to return a simpler interface, I'd cast toIList<string>
. -
Cosmetic vs More EfficientI don't think it matters much as the compilers are pretty efficient at this. In C#, I usually use the "null coalescing assignment operator (??=)"; so "inVal ??= internalVal;". Here are some functions to test the various ways to assign a default value:
void FixNullArg_SimpleIf(string arg = null) {
if (arg == null)
arg = "Fix null via simple if";
Console.WriteLine(arg);
}
void FixNullArg_TernaryOp(string arg = null)
{
arg = (arg == null) ? "Fix null via ternary operator" : arg;
Console.WriteLine(arg);
}
void FixNullArg_NullCoalescingOp(string arg = null)
{
arg = arg ?? "Fix null via ?? operator";
Console.WriteLine(arg);
}
void FixNullArg_NullCoalescingAssignmentOp(string arg = null)
{
arg ??= "Fix null via ??= operator";
Console.WriteLine(arg);
}
void FixNullArg_IsNullOrWhiteSpace(string arg = null)
{
arg = string.IsNullOrWhiteSpace(arg) ? "Fix null via IsNullOrWhiteSpace" : arg;
Console.WriteLine(arg);
}And here is the decompiled Intermediate Language (IL):
FixNullArg_SimpleIf:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldnull
IL_0003: ceq
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: brfalse.s IL_0010
IL_0009: ldstr "Fix null via simple if"
IL_000E: starg.s 01
IL_0010: ldarg.1
IL_0011: call System.Console.WriteLine
IL_0016: nop
IL_0017: retFixNullArg_TernaryOp:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: brfalse.s IL_0007
IL_0004: ldarg.1
IL_0005: br.s IL_000C
IL_0007: ldstr "Fix null via ternary operator"
IL_000C: starg.s 01
IL_000E: ldarg.1
IL_000F: call System.Console.WriteLine
IL_0014: nop
IL_0015: retFixNullArg_NullCoalescingOp:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: dup
IL_0003: brtrue.s IL_000B
IL_0005: pop
IL_0006: ldstr "Fix null via ?? operator"
IL_000B: starg.s 01
IL_000D: ldarg.1
IL_000E: call System.Console.WriteLine
IL_0013: nop
IL_0014: retFixNullArg_NullCoalescingAssignmentOp:
IL_0000: nop
IL_0001: ldarg.1
IL_0002: brtrue.s IL_000B
IL_0004: ldstr "Fix null via ??= operator"
IL_0009: starg.s 01
IL_000B: ldarg.1
IL_000C: call System.Console.WriteLine
IL_0011: nop
IL_0012: retFixNullArg_IsNullOrWhiteSpace:
IL_0