- Posted On 29 April 2014
- By
- In Interview Questions
After Advanced Interview Questions and answers on C#.NET - Part 1 here are some more advanced level interview questions and answers on C#. I am avoiding to add questions which are already scattered all over the internet due which it is taking time to post more. All these questions are asked in most of the IT companies of pune to me or some of my friends. Hope you will like these.
What is the differnce between var and dynamic keyword?
Suppose we have code like below.
var i= 123; i="ABC"; print(i);
Above code will give compilation error "Cannot implicitly convert type 'string' to 'int'", because when we initialize the var variable first time it will decide the datatype according to value which we assign,
here first we assign i=123, so on the basis of value i is integer, intetger datatype is got assigned and therefore later we cannot assign string value to it.
And suppose with dynamic keyword we have code like below
dynamic i= 123; dynamic="ABC"; print(i);
Above code gives the output as "ABC" because as name suggest the dynamic variable can deal with any kind data type, no matter which first value is assign to this variable.
Hope by above two examples you have understood difference between "var" and "dynamic" more clearly.
In below code, What will be the output for statement 1 and statement 2?
object a = 10; string s = (string)a; //1 string s1 = a as string; //2
Before reading answer think what will be the answer so you can compare your answer with correct answer. By asking this question interviwer trying to check whether you have proper knowledge about type casting in c# or not.
To answer, first statement is kind of explicit casting but it will throw error "Unable to cast object of type 'System.Int32' to type 'System.String'." as we are forcefully trying to convert int datatype to string. and in second statement no error will come but variable s1 will have value as null.
NOTE: This question can be asked as tell us difference between casting using "as" operator and normal casting(implicit or explicit)
Can we have custom messages as a warning or error in "Error List"(without using throw keyword)?
I am not sure how many of us use this but just to answer this question. Yes it is possible to do so. We can use C#'s Diagnostic directives feature in which we can explicitly generate error and warning messages that are reported in the same way as other compile-time errors and warnings.
#warning This code needs to be commented before final check in.
Above code will generate a warning, each time you build a project.
#error This code needs to be commented before final check in.
Above code will generate compile time error.
You can read more here.
How to maintain To-do work list in C#?
Just to share,In one of the interview I had faced this question as start of my 2nd round. The answer of this question is we can maintain a to-do list in C# by using to-do comments feature of Visual Studio.
For that you need to use syntax like following.
//todo: Implement logging related code here.
You can see a list of all such to-do comments in Task List (View >> Task List) like below.
Can we overload methods in following way?
public void MyMethod(int i) { } public void MyMethod(ref int i) { }
The signature of a method consists of the name of the method, the number of type parameters and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right.
So with above code as there is use of "ref" keyword which makes it unique signature comapre to other one i.e. we can overload method in above way.
So you have answered above question but this question can be stretched like, Can we overload methods in following way?
public void TestMethod(ref int x){} public void TestMethod(out int x){}
Thinking??
Well,without stretching it more directly coming to the answer, though kind of parameter is a part of signature as like previous case but here you have used ref and out toghether which is providing same meaning to the methods therefor you cannot overload methods in same class or interface solely by ref and out so above code will throw following error.
"Cannot define overloaded method 'TestMethod' because it differs from another method only on ref and out"
Thanks for reading. Do share your opinion/thoughts in comment section below.
- Tags :
- InterviewQuestions
- CSharp
Advanced .NET Interview Questions - Garbage Collection - Part 1
This post contains some advanced .NET Interview Questions on the topic Garbage Collection.
Advanced .NET Interview Questions - Garbage Collection - Part 2
This post contains some advanced .NET Interview Questions on the topic Garbage Collection.
Advanced Interview Questions and answers on C#.NET - Part 1
In this post we will discuss some advanced interview questions on C#.NET. which will help you to understand the type of questions asked in interviews for senior software engineer post.
Asp.Net Interview Questions and Answers for Experienced Developers - Part One
Asp.net Interview Questions for experienced developers with answers you will read in this post. These questions were faced by me in my last job hunt so you will surely get benefited after reading this post.
How to Prepare for the interviews - Simple but important tips - Part 3 - Prepare for HR Round
In this third post of my interview tips series, I am going to share some tips about how to prepare for HR Round. Also about things to do and things to not to do during the same.