csharp

There are 4 entries for the tag csharp

Primitive Obsession, Custom String Types, and Self Referencing Generic Constraints

I was once accused of primitive obsession. Especially when it comes to strings. Guilty as charged! There’s a lot of reasons to be obsessed with string primitives. Many times, the data really is a just a string and encapsulating it in some custom type is just software “designerbation.” Also, strings are special and the .NET Framework heavily optimizes strings through techniques like string interning and providing classes like the StringBuilder. But in many cases, a strongly typed class that better represents the domain is the right thing to do. I think System.Uri and its corresponding UriBuilder is a...

More Versioning Fun With Optional Arguments

In my last blog post, I covered some challenges with versioning methods that differ only by optional parameters. If you haven’t read it, go read it. If I do say so myself, it’s kind of interesting. ;) In this post, I want to cover another very subtle versioning issue with using optional parameters. At the very end of that last post, I made the following comment. By the way, you can add overloads that have additional required parameters. So in this way, you are in the same boat as before. However, this can...

Versioning Issues With Optional Arguments

One nice new feature introduced in C# 4 is support for named and optional arguments. While these two features are often discussed together, they really are orthogonal concepts. Let’s look at a quick example of these two concepts at work. Suppose we have a class with one method having the following signature. // v1 public static void Redirect(string url, string protocol = "http"); This hypothetical library contains a single method that takes in two parameters, a required string url and an optional string protocol. The following shows...

Splitting Pascal/Camel Cased Strings

Found this post in RossCode which mentions a blog post that discusses how to bind enumerations to drop downs, something I’ve done quite often. RossCode has an issue with using this approach personally because typically, the display text of a drop down should have spaces between words, which is not allowed in an enum value. For example... public enum UglinessFactor { ButtUgly, Fugly, NotSoBad, } In the preceding enumeration, you’d probably want the dropdown to display “Butt Ugly” and not...