Wednesday, May 10, 2023

c# : Differences between Object, Var and Dynamic type

 Difference between Object, Var and Dynamic type

Object, var, and dynamic are all keywords in C# that are used to declare variables, but they have different meanings and uses.

  1. Object type: The object type is a reference type that can hold any type of data. It is the base class for all C# classes and is used when the data type of a variable is unknown or can change at runtime. For example:
csharp
object myObject = "Hello World";

Here, the variable myObject is declared as an object type and is assigned a string value "Hello World". Later, the same variable can be assigned to a different type of value, such as an integer or a custom object.

  1. Var type: The var type is used to implicitly declare a variable with a data type that is inferred from the value assigned to it. It is a shorthand way of declaring a variable without explicitly specifying its type. For example:
csharp
var myString = "Hello World";

Here, the variable myString is declared as a var type, and the compiler infers its data type as string from the assigned value "Hello World".

  1. Dynamic type: The dynamic type is a type that is resolved at runtime instead of compile time. It allows a variable to hold any type of data and perform any operation on that data without type checking. This can be useful when working with dynamic data sources, such as databases or web services. For example:
csharp
dynamic myDynamicObject = "Hello World";

Here, the variable myDynamicObject is declared as a dynamic type and is assigned a string value "Hello World". Later, the same variable can be assigned to a different type of value, such as an integer or a custom object, and can be used to call any method or property on that value without explicit type casting.

It's important to note that while var and dynamic can make coding more convenient, they can also make it more difficult to debug errors, as the data type of the variable may not be immediately obvious. Therefore, it's generally recommended to use these types sparingly and only when they provide a clear benefit over explicitly declaring the data type.


Object
Var
Dynamic
The object was introduced with C# 1.0
Var was introduced with C# 3.0
Dynamic was introduced with C# 4.0
It can store any kind of value because the object is the base class of all type in .NET framework.
It can store any type of value but It is mandatory to initialize var types at the time of declaration.
It can store any type of the variable, similar to the old VB language variable.
The compiler has little information about the type.
It is type-safe i.e. Compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
It is not type safe i.e. Compiler doesn't have any information about the type of variable.
The object type can be passed as a method argument and method also can return object type.
Var type cannot be passed as a method argument and method cannot return object type. Var type work in the scope where it defined.
Dynamic type can be passed as a method argument and method also can return dynamic type.
Need to cast object variable to original type to use it and performing desired operations.
No need to cast because the compiler has all information to perform operations.
Casting is not required but you need to know the properties and methods related to stored type.
Cause the problem at run time if the stored value is not getting converted to an underlying data type.
Doesn't cause a problem because the compiler has all information about stored value.
Cause problem if the wrong properties or methods are accessed because all the information about stored value is getting resolve only at run time.
Useful when we don’t have more information about the data type.
Useful when we don’t know the actual type i.e. type is anonymous.
Useful when we need to code using reflection or dynamic languages or with the COM objects because you need to write less code.

Friday, April 14, 2023

Code practise : Good and Bad

  1. Follow naming conventions:
  2. arduino
    // Bad: int a; int x1; // Good: int age; int numberOfCustomers;
    1. Use comments:
    arduino
    // Bad: int result = 10 / 0; // Divide by zero // Good: // Divide the dividend by the divisor and return the result. // Throws DivideByZeroException if divisor is zero. int Divide(int dividend, int divisor) { if (divisor == 0) { throw new DivideByZeroException("Divisor cannot be zero."); } return dividend / divisor; }
    1. Keep your code clean and organized:
    c
    // Bad: int a=10,b=20,c=30,d=40;string name="John"; // Good: int a = 10; int b = 20; int c = 30; int d = 40; string name = "John";
    1. Use exception handling:
    arduino
    // Bad: int result = 10 / 0; // throws DivideByZeroException and crashes the program // Good: try { int result = 10 / 0; } catch (DivideByZeroException ex) { Console.WriteLine("Divide by zero error: " + ex.Message); }
    1. Use object-oriented programming principles:
    csharp
    // Bad: public int Add(int a, int b) { return a + b; } // Good: public interface IAddable { int Add(int a, int b); } public class Calculator : IAddable { public int Add(int a, int b) { return a + b; } }
    1. Write efficient code:
    scss
    // Bad: for (int i = 0; i < myList.Count; i++) { Console.WriteLine(myList[i]); } // Good: foreach (var item in myList) { Console.WriteLine(item); }
    1. Test your code thoroughly:
    csharp
    // Bad: public int Add(int a, int b) { return a + b; } // Good: [TestClass] public class CalculatorTests { [TestMethod] public void TestAdd() { Calculator calc = new Calculator(); int result = calc.Add(2, 3); Assert.AreEqual(5, result); } }

    By following these coding practices, you can write clean, maintainable, and efficient C# code that is easy to read and understand, and that works as expected.

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...