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

dotnet C#

From the C# documentation it states that the @ symbol is an Identifier.
2.4.2 Identifiers

The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

So what it really means is it allows you to sue reserved words.
For example

The other place using @ symbol is when defining strings with escape characters.

Personally I would not suggest/recommend using the @ symbol as a variable name; naming something string @string just confuses the reader of your code.

bom

Was detecting a file stream if it was Unicode encoded or not, so though I would share the code.

Let say you an xml file that you need to detect if it has the Unicode BOM in the file.
If you wish to know more about BOM look it up at wikipedia

Hope that helps

dotnet C#

C# Func vs. Action? What is the difference between a Func vs an Action in C#?

The main difference is whether one wants the delegate to return a value or not. By using Func it will expect the delegate to return a value and Action for no value (void)

1. Func for methods that return value eg. int Calculate(), performs an operation on the generic argument(s) and returns a value.
2. Action for methods that do not return value e.g void ProcessData(), performs an operation on the generic arguments.

In Linq you see a lot of Func that are being used.
Example in flitering

For Action one will see it in Linq ForEach statement

automapper

During the summer I have been busy working on my book “Instant AutoMapper“, and it did get released at the end of July of this year 2013.

Instant AutoMapper

Here is a link on amazon and packt publishing.

win8

Over the weekend I upgrade to win 8.1 and after upgrade I found my disk usage at 100% and it was getting super slow, plus I only had couple of Gig left on it.

The solution I found was to do a disk cleanup, using the cleanupmgr.exe

Here are the steps:
1. Use windows key + Q
2. Search for cleanup
3. Launch the cleanup manager

disk-cleanup

A window should launch for disk clean up click on the “clean up system files” button, the application will relaunch and will add more options, it may take some time at this point for it to relaunch.

Click on “Previous Windows installation(s)” and “Windows Update Cleanup” , and do a cleanup of the files. This fixed the issue of my drive hitting 100%.

There is also another note on microsoft community about this where 2 different steps are given. I personally havent tried those since disk cleanup worked for me, but here is the link for it:

http://answers.microsoft.com/en-us/windows/forum/windows8_1_pr-performance/100-disk-usage-ever-since-win-81-preview-update/88ce0370-8f47-4186-b902-0f6732cbbcbc

dotnet C#

Lots of developers confuse the var keyword in C# with JavaScript.
The var keyword was introduced in C# 3.0, and it is basically implicitly defined but is statically defined, var are resolved at compile time not at run-time.

The confusion is there also a var keyword in JavaScript, but in JavaScript var is dynamically defined.

Example:

In C# this would be illegal

while in Javascript this will be ok, since it is dynamically typed.

In C# one place that you are required to use var is when using Anonymous Types, that is when the compiler generates the internal type.

Example:

One can also use var when using LINQ

The above statement returns us a List of string names, the downside to this is if someone is reading this code they may think its a list of Animals that is returned, one can also define the return type in the foreach statement to make it clear or in LINQ. This may create a more readable code but I think having var also does the job well once you get comfortable using it.

Another place that var comes in handy is when defining a type to avoid repetition.

In conclusion:

  1. Javascript var != C# var
  2. Anonysmous Type requires var
  3. var can be used in LINQ return type
  4. Use var to avoid duplicating declaration names of type
UA-4524639-2