Skip to main content

Posts

C# Extension Methods - To Extend or Not To Extend...

I've been thinking a lot about extension methods lately, and I must admit I both love them and hate them. They are a lot like sugar, they taste so nice and sweet, but they'll rot your teeth if you eat them too much.   I can't deny that they are useful and handy. After all, one of the major components of the Shared Component library where I work is a set of useful extension methods to help simplify common repetitive tasks. But, I also can't deny that they tend to be overused and abused to willy-nilly extend every living type.   So what constitutes a good extension method? Obviously, you can write an extension method for nearly anything whether it is a good idea or not. Many times, in fact, an idea seems like a good extension method but in retrospect really doesn't fit.   So what's the litmus test? To me, an extension method should be like in the movies when a person runs into their twin, separated at birth. You just know you're related. Obviousl...

String Concat() vs. Format() vs. StringBuilder

I was looking through my groups’ C# coding standards the other day and there were a couple of legacy items in there that caught my eye.  They had been passed down from committee to committee so many times that no one even thought to second guess and try them for a long time.  It’s yet another example of how micro-optimizations can often get the best of us and cause us to write code that is not as maintainable as it could be for the sake of squeezing an extra ounce of performance out of our software. So the two standards in question were these, in paraphrase: Prefer  StringBuilder  or  string.Format()  to  string  concatenation. Prefer  string.Equals()  with case-insensitive option to  string.ToUpper().Equals(). Now some of you may already know what my results are going to show, as these items have been compared before on many blogs, but I think it’s always worth repeating and trying these yourself.  So let’s dig in. ...

String Class in Dot Net

Definition: - String is a data type that Represents text; that is, a series of Unicode characters. Thread Safety: - This type is safe for multithreaded operations. A string is a sequential collection of Unicode characters, typically used to represent text, while a   String  is a sequential collection of   System.Char   objects that represents a string. The value of the   String  is the content of the sequential collection, and the value is immutable. A   String  is called immutable because its value cannot be modified once it has been created. Methods that appear to modify a   String  actually return a new   String  containing the modification. If it is necessary to modify the actual contents of a string-like object, use the   System.Text.StringBuilder   class. A single   Char  usually represents a single code point; that is, the numeric value of the   Char  equals the code point. ...