Taswar Bhatti
The synonyms of software simplicity
dotnet C#

In C# when using the ref keyword it causes an argument to be passed by reference, not by value.
In order to use a ref as a parameter, both the method definition and the calling method must explicitly use the ref keyword, and also the variable must be initialized before passing in.

In the example below we have two methods, one using the ref keyword the other not.

But what about objects that are reference types. Here is where the confusion starts that the concept of passing by reference with the concept of reference types, the concepts are not the same. A method parameter can pass in a ref type regardless of its type (value or reference).

Below is an example that shows when a Person object reference type is used.

Last but not least I personally do not recommend using ref keyword and also from
The Framework Design Guidelines it also recommend to avoid using ref.

Framework Design Guideline

dotnet C#

.NET provides in its System.IO namespace the Path class which performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.

Most of the time we see develeopers writing code like

But by using Path.Combine we can provide a cross platform path

By using the Path and Combine method, one can at least eliminate some of the headache of porting to another platform.

GhostDoc

Recently have been going through some old code to review the comments in them from other developers and what I find out is developers tend to have really bad comments & documentation in their code

Example:

From the above code it is obvious that it is the constructor but does the comment tell me anything? I understand that sometimes it may be hard to document or comment certain method, thus I really strongly suggest at least use GhostDoc

What is GhostDoc?
GhostDoc is a Visual Studio extension that automatically generates XML documentation comments for methods and properties based on their type, parameters, name, and other contextual information.

Here is the result of using GhostDoc on the Person Class

As one can see Ghostdoc does a better job, don’t get me wrong GhostDoc still requires one to go through the comments here is an example where it tries to make sense.

Riches the text selection changed. ????? As you can see it didn’t do one of the best job for this comment, but at least one can modify it and it may become “RichTextBox selection has changed event handler”

Personally I would recommend using the tool but just take it with a grain of salt and always check the documentation that it generates.

linq

Here is a LINQ tip where you may wish to order a collection with an existing ordering of another collection.

Example:

Currently as it stands our data is stored in the order of { 5, 13, 7, 1, 8 } and we wish to order them in terms of { 1, 8, 5, 7, 13 }, we can use LINQ OrderBy to do so.

By doing so we end up with { 1, 8, 5, 7, 13 }, now if we wanted to put elements that dont have matching at the end of the list we can modify the OrderBy like this.

dotnet C#

Obsolete or Deprecated?

How do you mark a class or method as deprecated/obsolete?

By using the Obsolete attribute

dotnet C#

The ?? operator is called the null coalescing operator. It is used for providing a default value for Nullable types or reference types.

Example:

One of the disadvantage of ?? is it can create code that is not that readable.
e.g a ?? b ?? c ?? d ?? e

dotnet C#

In C# you can have property with different scope (property accessors). Properties in C# can be marked as public, private, protected, internal, or protected internal.

Example:

dotnet C#

C# 4.0 allows one to use named and optional parameters.

Example:

One can call the method in multiple ways

Additional note, one cannot provide a gap in arguments.
Example:

dotnet C#

The using statement in C# is a form of shortcut for try and finally block of code. Things to note is in order to use the using statement the object needs to implement the IDisposable interface, and using does not catch Exception, it just guarantees the call of Dispose.

Example:

Can be replaced with

One will not need to call the Close method of sql connection, since the dispose will do that.

interview

In interviews there is always the Singleton question, I personally refuse to answer it on interview. The reason is very simple I don’t code singletons anymore. I use my IoC Container to provide me with a singleton. Sorry if you dont know what an IoC (Inversion of Control Container) is, then you are still in the old ages of coding.

For once and for all here is the hand written code of a singleton in multi-thread environment.

And here is in an IoC container Unity & StructureMap

As you can see one can have a singleton with one line of code in Unity and say 2 lines of code in StructureMap, so I really wonder if one needs to proof that a double check is needed in a multi thread environment for singleton.

End rant……

UA-4524639-2