Some Important Differences In C# Concepts

Today, we will discuss a few important differences that every C# developer must know. We will cover the differences between dictionary and hashtable, x.Equals(y) and x == y difference, the difference between class and interface, and the difference between error and exception, and more.

There are these terms in C# programming we encounter often but most of the time, we do not know the actual difference between them.
 
Hence today, I thought I should try to write the concrete difference between these terms, as much as I know.
 

Difference between Dictionary and Hashtable 

  • Dictionary is a generic type, while hashtable is a non-generic type. This means Dictionary is a tightly-coupled type data structure while hashtable is a weakly-coupled type data structure.

  • When it comes to the declaration, we need to specify the type of data to be stored in Dictionary at that time; however, it is not necessary in the case of Hashtable because a Hashtable can store any data type in it.
    1. Hashtable values = new Hashtable();    
    2. Dictionary<intstring> dictionaryname = new Dictionary<intstring >();    
  • As Hashtable comes with the advantage of storing any data type values,  it is slower in the performance because of boxing and unboxing. As Dictionary is a type-safe data structure and boxing/unboxing is not needed in this case, Dictionary performs better than a Hashtable.

  • There is one advantage that Hashtable provides us, i.e., it is thread-safe. So, for read-write operations on Hashtable, you can have n number of reader threads but only a single writer thread. But in the case of Dictionary, there is nothing like that. If there is some need of synchronization in Dictionary, then we have to implement that.
For a better understanding of the concepts, you can learn about Dictionary and Hashtable in C# from the following links.

Difference between x.Equals(y) and x == y

 
Both of these operators are used for comparison purposes and return the same true/false value after comparison. Then, what is the difference? The difference takes place when we use reference type variables for comparison. In such case, Equals() compares the object value while == just compares the objects.
 
For example, 
  1. StringBuilder sb1 = new StringBuilder("CsharpCorner");  
  2. StringBuilder sb2 = new StringBuilder("CsharpCorner");  
So in this case, sb1 == sb2 returns false and sb1.Equals(sb2) returns true. In the case of value type comparisons, both of these operators give the same result.
 
You can learn more about it from this article >> Difference Between Equality Operator ( ==) and Equals() Method in C#
 

The difference between class and interface

 
A class is the blueprint of an object. It tells us how the object will actually look. On the other hand, interface is contract management.
 
For example, if you write an interface and then implement it in some class, this, then it makes sure all the methods written in interface apply to the implementing class.
These two are called the worst villains of a C# interview. You can learn here why,

Difference between error and exception

 
You might have heard people using these terms interchangeably many times but they are not that equal. An exception is derived from the System.Exception class. We can handle it on runtime using the try...catch block. But errors can come anytime and we might not be able to handle certain errors. So an exception is usually what an application wants to catch and handle but an error can be random and the application may not be able to catch or handle it. So, we must use these words properly to give a clear picture. 
 
You can learn more here about Exception Handling In C#.
 

Summary

 
In this article, we have understood some important differences between a few C# concepts. We covered the difference between Dictionary and Hashtable, the difference between Equals() and ==, the difference between classes and interface in C#, and the difference between error and exception in C# etc. I will write another article to shine light on more such differences.