Taswar Bhatti
The synonyms of software simplicity
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……

dotnet C#

C# has the as keyword, it is mainly used to cast object of one type to another, and if it fails it should return a null rather than crashing the program or throwing exception.

From MSDN documentation it states:
You can use the as operator to perform certain types of conversions between compatible reference types or nullable types. The as operator is like a cast operation. However, if the conversion isn’t possible, as returns null instead of raising an exception.

Differences between as and cast ( ) and when to use it:

  • Using “as“, I think the object is of the type, but if it isn’t give me a null, don’t crash the application.
  • Using cast, I know the object is of this type, and if it isn’t then crash the application.
  • Using is and cast is more costly than just using as
dotnet C#

So C# what is the yield keyword? The yield keyword in C# is basically an iterator, its a form of syntactic sugar added from C# 2.0 to create IEnumerable and IEnumerator objects, thus returning one element at a time.

From MSDN documentation it states.

When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. You use an iterator to perform a custom iteration over a collection. The following example shows the two forms of the yield statement.

Personally I have found using yield helpful in XML streaming where I have a very large document but would like to combine the use of XElement with XMLReader, such that the memory is not consumed like XDocument (where it loads the entire document into memory).

From the above code it will return an IEnumerable of XElement for us to process.

Some notes on using yield.

The declaration of an iterator must meet the following requirements:

  • The return type must be IEnumerable, IEnumerable<T>, IEnumerator, or IEnumerator<T>.
  • The declaration can’t have any ref or out parameters.

Also one cannot use yield in:

  • Anonymous methods
  • Methods that contain unsafe blocks
dotnet C#

In C# if one wishes to use C# constructor overloading they can use the “this” keyword right after to initialize the object, the only thing to note is one will not be able to do any validation of the data using this method.

Below I have listed 3 ways that one can initialize constructor overload in C#. There is also a base keyword that one can use to call the base class in their constructor initialization code

dotnet C#

C# readonly what is that for? The readonly keyword in C# is used as a modifier. A field can be declared with readonly modifier, and its assignment can only be in the Constructor or when the variable is declared.

An example:

The main thing to understand about readonly modifiers is that they are initialized at runtime, not like const where they initialized at compile time.

One advantage of using readonly is that you do not need to recompile external Dll that may use the value of it, but const would require a replaced of all the dll that are using the value.

The other advantage is to create immutable data structures in your program such they cannot be changed once constructed.

UA-4524639-2