Q:
What is the difference between var and dynamic in C#
belongs to collection: C# Interview Questions and Answers,You Need To Know
C# Interview Questions and Answers,You Need To Know
- What is C#?
- What is an object in c#?
- What are C# attributes and their significance?
- What is the meaning of instantiation in c#?
- How will you differentiate between a Class and a Struct in c#?
- What is the difference between public, static, and void in c#?
- What’s a multicast delegate in c#?
- How do I calculate someone’s age in C#?
- What is the difference between public static, public and static methods in c#?
- What is a Virtual Method in C#?
- List down the fundamental OOP concepts in c#?
- Compare Virtual methods and Abstract methods in c#
- What are Namespaces in C#?
- Is every abstract function virtual in C#, in general?
- What are I/O classes in C#? Define some of the most commonly used ones
- What is the difference between SessionState and ViewState in c#?
- What’s the difference between a method and a function in c#?
- What is the difference between an abstract function and a virtual function in c#?
- What is an interface class? Give one example of it
- What is the advantage of interface class?
- Explain the process of inheriting a class into another class?
- What is the difference between an interface and abstract class?
- What are circular references?
- What is the advantage of abstract class?
- What happens if the inherited interfaces have conflicting method names?
- What is Constructor in C#?
- Explain some points related to the constructor?
- What is difference between “is” and “as” operators in c#?
- Why can’t you specify the accessibility modifier for methods inside the interface?
- What are Value types and Reference types in C#?
- What are Jagged Arrays in c#?
- Is elements of jagged array must be initialized before its use
- Why Doesn’t C# Allow Static Methods to Implement an Interface?
- What do you understand by regular expressions in C#? Write a program that searches a string using regular expressions
- What is the difference between ref & out parameters in c#?
- What is the difference between var and dynamic in C#
- What is the use of ‘using’ statement in C#?
- What is the major use of ‘using’ keyword in c#?
- What is the difference between String and string in C#?
- What is function overloading in c#?
- Explain some ways of doing overloading function in C#
- What is serialization?
- Explain Inheritance in C# with an example?
- What is the best way to give a C# auto-property an initial value?
- List down the reason behind the usage of C# language
- What are Custom Exceptions?
- What is Managed or Unmanaged Code in c#?
- Explain the features of C#?
- What is the difference between constant and readonly in C#?
- Can we use “this” command within a static method?
- Explain Deadlock?
- illustrate Race Condition?
- What is Thread Pooling?
- Distinguish between finally and finalize blocks?
- What is Boxing and Unboxing in C#?
- What is enum in C#?
- Describe Accessibility Modifiers in C#
- What is the difference between ‘protected’ and ‘protected internal’?
- How do short-circuited operators work?
- What is the “volatile” keyword used for?
- Why do we use Async and Await in C#?
- Explain different states of a thread in C#?
- What are delegates?
- What is the difference between “continue” and “break” statements in C#?
- What can you tell us about the XSD file in C#?
- What are Custom Control and User Control?
- What are sealed classes in C#?
- What is the difference between Array and Arraylist?
- Can a private virtual method can be overridden?
- What are Properties in C#?
- What are accessors?
- What are the differences between System.String and System.Text.StringBuilder classes?
- Why Properties are introduced in C#?
- What is the difference between the dispose and finalize methods in C#?
- What are partial classes?
- What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ?
- What are the advantages of partial classes?
- What is the difference between late binding and early binding in C#?
- What are the differences between IEnumerable and IQueryable?
- What is Reflection in C#?
- Give an example of removing an element from the queue?
- What is the difference between directcast and ctype?
- How to implement a singleton design pattern in C#?
- What is the difference between the “throw” and “throw ex” in .NET?
- List down the commonly used types of exceptions in .net?
- How can we sort the elements of the Array in descending order?
- What is a Hashtable in C#?
- What is Multithreading with .NET?
var keyword:
The var keyword was introduced in C# 3.0 and variables declared with var are statically typed. Here, the type of variable declared is decided at the compile time. Variables that are declared as var should be initialized at the time of declaration. By looking at the assigned value, the compiler will decide the variable type. As the compiler knows the data type of the variable at compile-time, errors will be caught at that time only. And Visual Studio 2008 and later versions will show the IntelliSense for var type.
Example,
In the above sentence, obj will be treated as a string
In the above line, the compiler will throw an error, as the compiler already decided the type of obj as String and assigned an integer value to string variable violating safety rule type.
dynamic keyword:
The dynamic keyword was introduced in C# 4.0 and variables declared with dynamic were dynamically typed. Here, the type of variable declared is decided at runtime. Variables that are declared as dynamic don’t need to initialize the time of declaration. The compiler won’t know the variable time at the time of compiling, hence errors can’t be caught by the compiler while compiling. IntelliSense is not available since the type of variable will be decided at runtime.
Example,
In the above code, obj will be treated as a string.
The compiler will not throw any error, though obj is assigned to the integer value. The compiler will create the type of obj as String and then it recreates the type of obj as an integer when we assign an integer value to obj.
need an explanation for this answer? contact us directly to get an explanation for this answer