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

I remember watching a talk on infoq by Michael Feathers. He said something about growing your software teams organically like a garden. An example he gave is, you don’t yell at the lettuce for not growing, you have to nurture it and help it, try different things so that it grows better.

If you have a co-worker in your team not doing very well, you need to help them to become stronger, guide them, try different things, show them different ideas, pair with them.

The reality is every software project will have weeds (bad code), you need to pull the weeds out as the garden grows, clean it up, so that overall the software is growing more organically. By having a stronger team you will have less weeds.

For your process rather than just focusing on task base feature, one should also focus on code quality features. Don’t just focus on hammering the new feature out, have the time to go over items that need care. Refactor the code that you see lots of bug and write test in those areas.

From my experience, I have seen when a coworker tries to explain their problem to you they already have a solution in their mind and when they try to verbally explain to you they have in the back of their mind solved the issue already. And if its not the case give them ideas of how you perceive the problem and show them your take on it.

But by yelling and not helping the lettuce, the lettuce is not going to grow any better.

linq

Recently I was asked a simple code to improve the performance, and at first I couldn’t see it but as you know your brain starts working when you aren’t thinking of the problem anymore. (It happen to me when I was driving)

Anyway the problem on hand was there is a for loop and when there are 10000 clients it performs bad.
Something along the line of.

So I went and wrote some code to see the perf using Linq and at the end using a Dictionary rather, since a dictionary is way faster in lookup, O(1) in this case.

Here is the code using a List and Linq together

and if we changed the datatype to Dictionary this was the code used

Here is the end result of using it on an i7 8G of ram machine.

console

And the results show that using an index as a dictionary is definitely faster, but one also has to look at the number of data.
Where there are only few data entry using linq is quite insignificant in performance but where there are millions of data, it really shows that it slows down.

Here is the rest of the code.

UA-4524639-2