C# Exclusive Interview Questions and Answers (453) - Page 23

  • A joint initiative from DotNetFunda.Com and Questpond.
  • A one stop place on the internet to get trusted and proven Interview Questions (based on more than a decade of experience in this field) to ensure the success of the candidates.
  • You will find almost all Interview questions from Questpond here and this list is growing day by day with all latest and updated interview questions.

453 records found.

Get 650+ Questpond's Interview videos on discount

c# interview questions :- What is MVVM ( Model view view model) Design pattern ?

This is a good c# interview questions which is asked around design pattern.


MVVM is a UI design pattern. The main use of this pattern to remove UI cluttered code like bindings , synchronization etc.


In this pattern we create a extra class called as view model  or model which acts as a bridge between model and view. The view sends the actions and data to the model view class who in turns sends the data to model. Any changes in the model is replicated or informed to the UI using the INotifyPropertyChanged interface.



Regards,


C#/.NET interview Question - What are the different types of collections in .NET?

Collection: - Collections are basically group of records which can be treated as a one logical unit.


.NET Collections are divided in to four important categories as follows.



  1. Indexed based.
  2. Key Value Pair.
  3. Prioritized Collection.
  4. Specialized Collection.

type.png


Let’s begin with Indexed based and key value pair.


Indexed based: - It helps you to access the value of row by using the internal generated index number by the collection.


Key Value Pair: - It helps you to access value by the user defined key.


Below is the example of country collection for Indexed based and key value pair.


ex.png


When you want to access India through Index based you have to write like below code.

MessageBox.Show(myarray[0]); //for array
MessageBox.Show(mylist[0]);  //for list

Now, when we want to access the same output “India” by Key value pair.

MessageBox.Show(myhash["IND"].ToString()); //for hashtable

Prioritized Collection: -It helps us to get the element in a particular sequence.


FIF.png


Specialized Collection: -It is very specific collections which are meant for very specific purpose like hybrid dictionary that start as list and become hashtable.


For more detail on collections watch the below video.



Regards,




C#/.NET interview Question - What is the difference between Array and ArrayList?

Array
ArrayList
They are fixed length.
They are resizable and variable length.
They are compiled strong type collection.
They are flexible and can accommodate any data types.
Because arrays are of fixed size and strong type collection performance is faster.
In arraylist lots of boxing and unboxing are done there for its performance is slower.

Let’s see an example to prove the above differences.

The below is how we declare Array.
String [ ] str = new String [5]; // here you see that the length is fixed as [5] and the data type is also defined as string.

Now, see how you can declare ArrayList.
ArrayList   str = new ArrayList (); // here you see that the length is not fixed and is not tied up with a data type.
If you go to add data to an ArrayList you can see it takes object which is a very generic type. Due to this boxing, unboxing or casting are done. In other words data moves from stack to heap or heap to stack. If you look at Array as they are strong types boxing and unboxing are completely avoided. Therefore, Array performance is faster as compared to ArrayList. 

array.png

The below diagram gives a better idea of the differences between Array and ArrayList.

arr.png

Regards,



C#/.NET interview Question - Why Arrays are faster than ArrayList?

Let’s demonstrate an example to prove that how arrays are faster than arraylist for that go to visual studio and create a simple window application with a button and two labels like below diagram.

111

Below is the code snippet.

using System.Collections;// import this namespace to access arraylist.
using System.Diagnostics;// import this namespace to access Stopwatch.
namespace ArrayandArrayList
{
public partial class Form1 : Form
{
int[] Array = new int[1000]; // here array is declared.
ArrayList objArraylist = new ArrayList();// here array list is declared.
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Stopwatch objWatch = new Stopwatch();
Stopwatch objWatch1 = new Stopwatch();
objWatch.Start();
for(int i=0;i<1000;i++)
{
Array[i] = DateTime.Now.Second;
}
foreach (int i in Array)
{

}
objWatch.Stop();
label1.Text = objWatch.ElapsedTicks.ToString();
objWatch1.Start();
for (int i = 0; i < 1000; i++)
{
objArraylist.Add(DateTime.Now.Second.ToString());
}
foreach (object i in objArraylist)
{
}
objWatch1.Stop();
label2.Text = objWatch1.ElapsedTicks.ToString();
}
}
}
In the above code, I have used Stopwatch to record the time taken by the array and  arraylist to performance actions.

The Output look like below diagram.


qwqwqw.png

Conclusion: -As in ArrayList lots of Boxing and UnBoxing are done therefore its performance is slower than Array because in Array there is no type casting.

Regards,


Please click here to see more C#/.NET interview questions
C# and .NET interview questions - How to create setup and deployment project for window application?

This is a typical .Net interview question which is asked to understand if you know how to create deployment packages. When you are done with your application you would like to give your end user a setup package rather than the source code.


The short answer which the interviewer is expecting is that we can use the Visual studio deployment package project. Now the long answer just in case the interviewer gets in to details. So lets understand step by step of how to create a deployment package.


Following are the simple steps for creating setup and deployment for window application:


Step 1: Go to file and click add new project > select other project type > setup and deployment >visual studio installer then select the setup project, below are the sample diagrams which you need to follow.


s1.png


s2.png


The default File System editor will appear in the window as shown in the below diagram.


s3.png


Step 2:Select Application Folder and right click Add>Project Output>select Primary output.


s4.png


s5.png


Step 3: If you want to shortcut on desktop then Select User’s Desktop right click on the second window>click create new shortcut>select output file from Application folder>change the file name from primary output which you want.


s6.png


If you want to shortcut on program menu  then Select User’s Programs Menu right click on the second window>click create new shortcut>select output file from Application folder>change the file name from primary output which you want.


If you want to change the Manufactures name and other properties just right click the project goes to properties.


s7.png


Step 4: Finally Build the Setup1 project.


s8.png


Exe or installer will be available on the physical path of the project.


s9.png


In the following video, view samples of various questions asked in C# and .NET interview.


C# and .NET interview Question - Have you used crystal report in your project? If yes, how will you implement the same?

Any project you take in .NET or any language reporting is an integral part of the application. You will rarely find application without reports. Due to heavy usage of reporting in application this topic becomes a hot favorite of interviewers.  There are two prominent technologies by which you create reports in .NET one is crystal reports and the other is SQL reporting services.  In this question we will try to answer from the crystal report point of view.

So again a very typical .Net interview question which is asked to understand if you know how to create crystal report.
Let’s demonstrate a simple example to see that, how we can create crystal report in our project.

Creation of crystal reports its basically 7 step process as following.

Step 1:-This is very simple and basic step for adding crystal report in the project for that just click on the "Project" menu and select "Add New Item" --> "Reporting"--> "Crystal Report".





Step 2:-When you click on add button the following window will appear and you can select your choices according to your project requirement and then click OK.



Step 3:-As soon as you click on OK button a new window will appear from that expand Create New Connection ->OLEDB(ADO)  as you can see in below diagram.




Step 4:-Now, Expand the OLEDB(ADO) folder and a new window will appear with all the connection providers from that you can select your own connection provider name and click on “Next”.



Step 5:-When you click on Next a new window will pop-up where you can define your server name and your database name then click on Next another window will pop-up then click on “Finish” later you can see a new window like below diagram with your selected database then you can select the table on which you want to generate report and click on “Next”.



Step 6:-Choose the field name, which you want to display on your report and click on “Finish”.



Step 7:-Once you have completed the above step then create a new form
and place a CrystalReportViewer from the toolbox and define the report
source like below diagram.




Once you select the report source click on “OK”, now you can run your project and see the report.



In the following video, view samples of various questions asked in C# and .NET interview.


C# and .NET interview Question - What is Generic?

This is one of the most important typical question, which is asked in most of the interviews to check whether you know about generic.

Generic: Generic help us to create flexible strong type collection.

Generic basically seperate the logic from the datatype in order maintain better reusability, better maintainability etc.

Lets see an simple example to understand how exactly generic seperate logic from datatype.

In order to use Generic in your project you will first have to ensure that you have imported "System.Collections.Generic" namespace.
    class Check<UNKNOWNDATATYPE>
{
public bool Compare(UNKNOWNDATATYPE i, UNKNOWNDATATYPE j)
{
if (i.Equals(j))
{
return true;
}
else
{
return false;
}
}
}
In above code snippet, i have created a class called as "Check" with UNKNOWNDATATYPE so that, i can define different data types at run time. Now, in below code you can see that i have created two object of Check class with two different datatypes(int,string).
    class Program
{
static void Main(string[] args)
{
Check ObjCheck = new Check(); //here i have defined int datatype
bool b1 = ObjCheck.Compare(1, 1);
Check Obj1 = new Check(); //here i have defined string datatype
bool b2 = Obj1.Compare("feroz", "kalim");
Console.WriteLine("Numeric Comparison Result:" + b1);
Console.WriteLine("String Comparison Result:"+ b2);
Console.ReadLine();
}
}
Below is the full code snippet for the same so that you can try it by yourself and see the resultant output.
    class Program
{
static void Main(string[] args)
{
Check ObjCheck = new Check();
bool b1 = ObjCheck.Compare(1, 1);
Check Obj1 = new Check();
bool b2 = Obj1.Compare("feroz", "kalim");
Console.WriteLine("Numeric Comparison Result:" + b1);
Console.WriteLine("String Comparison Result:"+ b2);
Console.ReadLine();
}
}

class Check<UNKNOWNDATATYPE>
{
public bool Compare(UNKNOWNDATATYPE i, UNKNOWNDATATYPE j)
{
if (i.Equals(j))
{
return true;
}
else
{
return false;
}
}
}
For more information about generic, please watch the below video.


C# and .NET interview Question - What are Special Collections?

Special Collection:-Special Collection are nothing but tailored .net collection meant for specific purpose.

There are many types of special collection in .net but let only talk about four important special collection which are mostly used.

The following are the four important special collections

1. CollectionsUtil:-
It help us to Create collections that ignores the case in String.

Example:-
Hashtable ObjHash = CollectionsUtil.CreateCaseInsensitiveHashtable();
ObjHash.Add("feroz","he is a developer");
string str = (string) ObjHash["FEROZ"];
MessageBox.Show(str);
2. ListCollection:-Good for Collections that typically contain less number of elements.

Example:-
ListDictionary ObjDic = new ListDictionary();
ObjDic.Add("feroz", "he is a developer");
ObjDic.Add("moosa", "he is a developer");
3. HybridCollection:- It is combination of ListDictionary and HashTable.When the collection items are less then it will act as ListDictionary and when number of items increases above 10 or above certain number then it will act as a Hashtable Collection.

Example:-
 HybridDictionary ObjHybrid = new HybridDictionary();
ObjHybrid.Add("feroz", "he is a developer");
ObjDic.Add("Wasim", "he is a network administrator");
ObjDic.Add("moosa", "he is a hardware engineer");
4. StringCollection:- It is a very special Collection, which is designed for storing only string items.

str.png


Also have a look on this video, which is asked in most of the interviews.


C# and .NET interview questions - What are the different types of generic collections?

There are basically four different types of generic collections which are as follows:-


1. List:-Lists are indexed based Generic Collections. Lists are Generic form of ArrayList.


List helps us to create flexible strong type collection as you can see in below code snippet i have defined List as "int" and "string".

//index based Generic collection            

List<int> ObjInt = new List<int>();        
ObjInt.Add(123);           
ObjInt.Add(456);           
Console.WriteLine(ObjInt[0]); //accessing the List by internal index based value.           
List<string> ObjString = new List<string>();           
ObjString.Add("feroz");

2. Dictionary:-Dictionary are key based generics collection.
                     Dictionary are generic form of Hashtable.

 //key based Generic collection            

Dictionary<int, int> ObjDict = new Dictionary<int,int>();           
ObjDict.Add(1,2);           
Dictionary<int, string> ObjDict1 = new Dictionary<int,string>();           
ObjDict1.Add(3, "feroz is a developer");           
ObjDict1.Add(4, "wasim is a developer");           
Console.WriteLine(ObjDict1[3]); //accessing the dictionary by defined key.

3. Stack:-Stack generic collection allows you to get value in "LIFO"(last in first out) manner.

// Stack             

Stack<string> ObjStack = new Stack<string>();           
ObjStack.Push("feroz");           
ObjStack.Push("moosa");           
Console.WriteLine(ObjStack.Pop());

4. Queue:-Queue generic collection allows you to get value in "FIFO"(first in first out) manner.

//Queue            

Queue<int> ObjStr = new Queue<int>();           
ObjStr.Enqueue(789);           
ObjStr.Enqueue(456);           
Console.WriteLine(ObjStr.Dequeue());

Genericollection.jpg


 


For more information about generic, please watch the below video.



.NET and ASP.NET interview Question - What is Threading and how to implement it?

Threading is a parallel processing unit and helps you to access multiple tasks at a one moment of time. When we want to access parallel processing in our application we use Threading.


In order to use threading we first have to import System.Threading namespace in our application like below code.
using System.Threading;


Below is the simple class in which I have defined two functions as Fun1 and Fun2.i have also created a DoTest function with an instance of thread as t1 and t2.finally I have invoked DoTest function in Main Method.

class Program   

{
 public void Fun1()      
{
  for (int i = 0; i < 10; i++)         
{              
Console.WriteLine("i"+"="+i.ToString());              
Thread.Sleep(500);         
}       
}
 public void Fun2()       
{
  for (int j = 0; j < 10; j++)           
{               
Console.WriteLine("j" + "=" +j.ToString());             
Thread.Sleep(500);      
}        
}
  static void Main(string[]args)       
{
   Program p = new Program();        
   p.DoTest();
   Console.ReadLine();       
}
  public void DoTest()       
{
  Thread t1 = new Thread(new ThreadStart(Fun1));     
  Thread t2 = new Thread(new
  ThreadStart(Fun2));
  t1.Start();
  t2.Start();       
}
}

Once you have completed the above steps just run your application and you will see the output like below diagram.
Output:


aa.png


You can see that in the above diagram both the function Fun1 and Fun2 run at single moment of  time with one after another and display their respective outputs. 


C# and .NET interview questions - What are Extension Methods in C#.NET ?

This is a semi-asked .NET interview question and below goes the explanation for the same.Lets say we have a simple class called as Maths and this class we have got from a 3rd party. In other words we do not have control of changing the code of this class.

public class Maths   
{
 public int Number1;
 public int Number2;
    public int Add()       
   {
     return Number1 + Number2;        
   }
}


Now let’s say we get a requirement where we need to add one more method called as subtract to the above class. As said previously you just have the DLL and you cannot go and add subtract method to the above class. One thought you can give is how about creating a new class which inherits the above and aggregates it. But let’s assume we are not given that option either. There's where we have extension methods. Extension methods enables you to "add" methods to existing classes without creating a new derived classes, recompiling, or otherwise modifying the original class. So what you can do is create static function as shown below in the below code snippet. Please note the parameter "this Maths x".

public static class MyMathExtension    

{
  Public static int Subtract(this Maths x)       
   {
      return x.Number1 - x.Number2;       
   }
}

Now when you create an object of maths class and type "." in the visual studio intellisense you should see the subtract extended method. Below is the diagram for the same. Please see the color is blue for extension methods with a down arrow symbol.



Sometimes back i was asked a very basic c# and .NET interview question What is the difference between convert.tostring and tostring (), below is a simple video which explains the concept in detail.



What are the different coding standards used in c# projects ?



There are 3 types of naming convention pascal,hungarian and Camel.



Pascal notation starts with all first letter of the word capital. For example

"Customercode" where you can see "C" letter capital in both the words.



Camel notation starts with all first letter capital except the first letter of
the first word. For example "customerCode", you can see the first letter "c"
small and the other "C" of the letter code capital.



Hungarian notation starts with first word as the datatype prefix. For instance "intCount","boolIsValie"
etc. The first word describes the datatype lik int, bool etc.





One of the most asked .NET interview question is what are generics ? , below is
a nice video which explains generic concept.







 


C# interview questions: - What is the difference between “int” and “Int32” in C#?

"int” and “Int32” are both one and same . If you move your mouse on the integer data type you can see “int” is actually just “Int32”. In other words it will store max value of 2 ^ 32 and minimum value of -2 ^ 32.




Below is a nice video which talks about one more important .NET interview question: - What is the use of param key word ?







More C# Interview Questions & Answers here

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Exclusive Interview Questions and Answers Categories