Skip to main content


Showing posts from October, 2013

All about Virtual,Override and New in C#

I have seen that lots of people get confused with Virtual, Override and new keyword in C#. So I thought it will be a good idea to write a blog post about it. In this blog post we will learn what is virtual, override and new keyword in C# and what’s difference between override and new in C#.

Virtual and Override in C#:
Virtual keyword allows class member to override in derived class. Let’s take simple example. I have class A which contains Print Method method as virtual method and I have another class B which is derived from class A which overrides Print method. Following is a code for that.
public class A { public virtual void Print() { System.Console.WriteLine("Virtual Print method from a"); } } public class B:A { public override void Print() { System.Console.WriteLine("Override Print method from b"); } } Now If I create a object of class A like following and run the code Print method run as normal method. Following is code…

Linq- AddRange Method in C#

In this post I’m going to explain you about Linq AddRange method. This method is quite useful when you want to add multiple elements to a end of list. Following is a method signature for this.
public void AddRange( IEnumerable<T> collection )To Understand let’s take simple example like following.

using System.Collections.Generic; using System; namespace Linq { class Program { static void Main(string[] args) { List<string> names=new List<string> {"Jalpesh"}; string[] newnames=new string[]{"Vishal","Tushar","Vikas","Himanshu"}; foreach (var newname in newnames) { names.Add(newname); } foreach (var n in names) { Console.WriteLine(n); } } } }
Here in the above code I am adding content of array to a already created list via foreach loop. You can use AddRange …

throw new exception- C#

This post will be in response to my older post about throw exception best practice. where one of user asked that I should include throw new exception.So I thought it will be good idea to write a whole blog post for it. This blog post explains what's wrong with throw new exception.

What’s wrong with throw new exception:
Throw new exception is even worse, It will create a new exception and will erase all the earlier exception data. So it will erase stack trace also.Please go through following code. It’s same earlier post the only difference is throw new exception.
using System; namespace Oops { class Program { static void Main(string[] args) { try { DevideByZero(10); } catch (Exception exception) { throw new Exception (string.Format( "Brand new Exception-Old Message:{0}", exception.Message)); …

throw vs. throw(ex) best practice and difference- c#

Recently I was looking into someone’s code found that they are using the throw(ex) to log exception so I told that person that why you are not using only throw he said there is no difference.. Wait this is not true there is a difference. So, this post is all about throw Vs. throw(ex) best practice and what is difference.

We all know that C# provides facility to handle exception with try and catch block and some times we use throw statement in catch block to throw the exception to log the exception. Here there two options either you could use throw(ex) or simple throw just like following.

try { } catch (Exception ex) { throw; }
try { } catch (Exception ex) { throw(ex); }
So which one is good and best practice let’s study that.

Similarities: Let’s first see what is similar in both.
Both are used to throw exception in catch block to log messageBoth contains same message of exception Difference: Now let’s see what is difference.
throw is used to throw current exception while th…

Cool Resharper Features Part-2

This post will be part-2 of first post Cool Resharper Features Part-1. In this post we are also going to learn some other cool features that resharper is providing. Following are some of features that resharper is providing.
1) Move to another file to match type name: This feature makes you very productive when you need to create a new class. Suppose with normal visual studio you need to create a new class with new file then you need to do via file menu –> Add new item and then select class. Here you can do very easily without having all this stuff. Just write you class after class in any .cs file like below.

In above image you can see that I have created a employee class. Now when you put a cursor on employee class name resharper will open a popup or you can press ALT + R and it will open a popup like this.

And once you click on this it will create a file name with Employee.cs and move class code to that file.

2) Extract Interface(Refactor Menu): This features comes quite handy wh…

Cool Resharper features part -1

I have been using Jetbrain’s resharper since last four years and it’s been my default Visual Studio plugin for visual studio. After this much of experience of resharper I must say if resharper is not there I would not have that much productive with visual studio. Working with it is a pleasure. I this post I am going to explain some cool resharper features.

Resharper uses two keyboard schemes Visual Studio or Intelli IDEA scheme. In this post I am going to use Intelli IDEA scheme. The only difference between two key map scheme is keyboard shortcut. Some Visual Studio short cut will be override by Resharper.
1) ALT + Enter: It’s a magic key for resharper you can have any feature with ALT+ Enter whether its refactoring, code completion or anything. You can use ALT + Enter for anything. For example you have some name space un used and you want to remove namespace just press ALT + Enter and it will have popup like following.

Once you click on this and it will remove unused namespace. Same …

Database diagram support objects cannot be installed because this database does not have a valid owner- SQL server error solution

Before some days, I have copied one database from the one of server and restored it on my machine. The server was not having ‘sa’ user login and I was having the ‘sa’ user in my local machine. The database was restored perfectly with no problem but when I tried to create a database diagram I got following error.
Database diagram support objects cannot be installed because this database does not have a valid owner. To continue, first use the Files page of the Database Properties dialog box or the ALTER AUTHORIZATION statement to set the database owner to a valid login, then add the database diagram support objects. After digging into that I found that the database does not have any valid owner and that’s why its generating errors. To resolve this error following is a step to change or add owner of database.

Right click on your database and click properties it will open a dialog like following. Goto-> Fiile Tab.

Click on .. button it will open a dialog box like fol…