khaledwawa
0
Q:

difference between dynamic and var

// C# program to illustrate how to get the 
// actual type of the dynamic type variable 
using System; 
  
class GFG { 
  
    // Main Method 
    static public void Main() 
    { 
  
        // Dynamic variables 
        dynamic val1 = "GeeksforGeeks"; 
        dynamic val2 = 3234; 
        dynamic val3 = 32.55; 
        dynamic val4 = true; 
  
        // Get the actual type of 
        // dynamic variables 
        // Using GetType() method 
        Console.WriteLine("Get the actual type of val1: {0}", 
                                  val1.GetType().ToString()); 
  
        Console.WriteLine("Get the actual type of val2: {0}", 
                                  val2.GetType().ToString()); 
  
        Console.WriteLine("Get the actual type of val3: {0}", 
                                  val3.GetType().ToString()); 
  
        Console.WriteLine("Get the actual type of val4: {0}", 
                                  val4.GetType().ToString()); 
    } 
} 
0
// C# program to illustrate the concept 
// of the implicitly typed variable 
using System; 
  
class GFG { 
  
    // Main method 
    static public void Main() 
    { 
  
        // Creating and initializing 
        // implicitly typed variables 
        // Using var keyword 
        var a = 'f'; 
        var b = "GeeksforGeeks"; 
        var c = 30.67d; 
        var d = false; 
        var e = 54544; 
  
        // Display the type 
        Console.WriteLine("Type of 'a' is : {0} ", a.GetType()); 
  
        Console.WriteLine("Type of 'b' is : {0} ", b.GetType()); 
  
        Console.WriteLine("Type of 'c' is : {0} ", c.GetType()); 
  
        Console.WriteLine("Type of 'd' is : {0} ", d.GetType()); 
  
        Console.WriteLine("Type of 'e' is : {0} ", e.GetType()); 
    } 
} 
0
Get the actual type of val1: System.String
Get the actual type of val2: System.Int32
Get the actual type of val3: System.Double
Get the actual type of val4: System.Boolean
0
Type of 'a' is : System.Char 
Type of 'b' is : System.String 
Type of 'c' is : System.Double 
Type of 'd' is : System.Boolean 
Type of 'e' is : System.Int32 
0

Tags

New to Communities?

Join the community