Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How to run power shell script in cSharp?

Answer»

How to RUN power shell script in CSHARP?
Below is the code to run powershell script in c# with the HELP of path and DIFFERENT parameter.




Powershell script

Download Code

2.

What are the other equivalent alternate of Exit() for quitting csharp application?

Answer»

What are the other EQUIVALENT alternate of Exit() for quitting csharp application?
YES there are some other equivalent alternate of Exit(). For WEB application we use System.Environment.Exit(INT exitCode) for exit the application.
For Windows Forms we use Application.Exit().

3.

Create dynamic Textbox in Panel control with the help of list

Answer»

Create dynamic Textbox in Panel CONTROL with the help of list
Below is the code to create dynamic Textbox with teh help of List collection AMD ADD them in panel.

Dynamic Textbox
But my point of view is to use Control collection that is the BEST option

4.

Write down some common difference between Span and Array?

Answer»

Write down some common difference between Span T and Array? Some of the USER SAYING Span is valid REPLACEMENT for Array below are the some of the common difference between 2 of this.

5.

What is the reason or why would we use Untrusted Verification?

Answer»

What is the reason or why WOULD we USE Untrusted Verification?
We GENERALLY use untrusted verification for WEB service and for non windows applications.

6.

Mention some of ref struct restrictions in c#?

Answer»

Mention some of ref struct restrictions in csharp? Below are some restrictions which prevent a ref struct value ends up on heap at runtime:- (1)We can not use ref struct as a type argument. (2)We can not use ref struct to implement interfaces. (3)A ref struct variable can not be used in iterators. (4)We can not boxed ref struct to System.ValueType or System.Object. (5)We can not CAPTURED ref struct variable by a lambda expression or local function. (6)We can not used ref struct variable in an async methods. But we can use ref struct VARIABLES in synchronous methods. (7)We can not use ref struct as element type of an array. (8)We can not use ref struct to declare type of field of a CLASS or a struct. But a ref struct can type a field of ref struct.

7.

What are the new feature of C# 11 that is coming?

Answer»

What are the new feature of C# 11 that is coming?
C# 10 is recently released and C# 11 is in PROGRESS. There are many new features are included in this version of C#. Below are the list of new features of C#:-

(1)New List Pattern
New feature of List Pattern which allows us to do comparison between List Pattern and array or List. Here you will able to match DIFFERENT elements or even a cut pattern to match 0 or more elements. To UNDERSTAND this we we will take example as given below:-
Pattern [1, 2, .., 10] matches all below example:-



List Pattern Match

And to explore list pattern we consider below example:-



List Pattern Consider

When it is passed the following arrays the results are as indicated at end of line:-



List Pattern Consider Val

(2)Parameter Null Checking
This is basically handle null and to validate if the method argument are null. Below example will more clarify:-
(i)In old code we use below parameter


Old Check Null

(ii)And now we can use below abbreviation for treating null values



Null Check Pattern

(iii)Below example of checking indexer parameter with set and get



Get Set null

(3)Constructors
Change in null-checks using the null validation syntax (!!). And explicit validation occurs after field initializers, base CLASS constructors, and constructors called using this.


(4)Interaction with NULLABLE Reference Types
!! operator we have seen before, it will start as non-null with a nullable state. BElow example will clarify you more:-



checking null T

8.

what is multicast delegate

Answer»

The C# LANGUAGE support feature that is known as the multicast delegate. Any delegate that has a void return type is called multicast delegate. A multicast delegate can be assigned and invoke multiple methods.
if we are doing ASP.NET development and have ever looked at a page's InitializeComponent() METHOD, we may have NOTICED a STATEMENT like this:

this.EditButton.Click += new System.EventHandler(this.EditButton_Click);
If we want to add another EVENT handler then we can easily do that for example:-
this.EditButton.Click += new System.EventHandler(this.EditButton_Click);
this.SearchButton.Click += new System.EventHandler(this.newmethod);
In above example first Editbutton_Click is called after that newMethod is called

9.

What is Anonymous method in csharp?

Answer»

What is Anonymous method in csharp?
As we have the name of method Anonymous. Anonymous method doesn't have any names. Anonymous methods in c# can be DEFINED by USE of delegate keyword which is assigned to a VARIABLE of delegate type.


Anonymous Method
This methods can access variables defined in an OUTER function and Anonymous methods can also be passed to a method that accepts the delegate as a parameter. Anonymous methods can also be USED as event handlers.


Anonymous Event

10.

What are the four main limitations of Anonymous Method?

Answer»

What are the four main limitations of Anonymous METHOD?
Below are the 4 main limitations of Anonymous method as define below:-
(1)Anonymous method cannot contain jump statement like GOTO, break or continue.
(2)In Anonumous method it cannot access ref or out parameter of an OUTER method.
(3)Anonymous method cannot have or access UNSAFE code.
(4)Anonymous method cannot be used on the left side of the is OPERATOR.

11.

What are the different aspects or points of Anonymous Methods?

Answer»

What are the different aspects or points of ANONYMOUS Methods?
Below are the 5 main point of Anonymous methods:-
(1)We will use delegate keyword to define Anonymous method.
(2)A Anonymous method must be assigned to a delegate.
(3)With the help of Anonymous method we can access outer variables or functions.
(3)We can use Anonymous to PASS as a parameter.
(4)We can also use Anonymous as EVENT handlers.

12.

Definition of Tuple class with some examples?

Answer»

Definition of Tuple CLASS with some examples?
Tuple class was first introduced in .NET 4.0 Framework. Tuple is data structure which contains sequence of element which are of DIFFERENT data TYPES. We used this where we have a data structure which hold an object with properties, but you don't want to create a separate TYPE for it.

Tuple

13.

A three level Class Hierarchy example with systax?

Answer»

A three level CLASS Hierarchy example with systax?
Below is the example of three level Class Hierarchy example where we have 3 class as define below and some dervied class
<B>
public class A
{

}
public class B: A
{

}
public class C: B
{

}


Notes:-
(1)Here in above example A is a base class for B and B is base class for C.
(2)Derived class always have SOMETHING more than a base class so base is smaller than the derived class.
(3)Below Facts
A objA1=new A(); (Correct)
A objA2=new B(); (Correct)
A objA3=new C(); (Correct)
B objA4=new C(); (Correct)
B objA5=new A(); (Incorrect)
(4)As in above class initialization a base class can hold a derived class but a derived class cannot hold a base class.

14.

Write down the four main difference between Casting and Conversion?

Answer»

Write down the FOUR MAIN DIFFERENCE between Casting and CONVERSION?
Below are the four main difference between Casting and Conversion.

15.

Different way to fetch int values from enum in csharp?

Answer» DIFFERENT way to fetch int values from ENUM in csharp?
First we will define enum then will discuss the different conversion technique to convert them into int.

public enum Study
{
subject = 2,
COURSE = 3,
books = 4,
author = 5,
}


Here below we will create a object of enum and fetch the first

Enum e = Study.subject;

And below is the different conversion or parsing techniques


int i = Convert.ToInt32(e);
int i = (int)(object)e;
int i = (int)Enum.Parse(e.GetType(), e.ToString());
int i = (int)Enum.ToObject(e.GetType(), e);
16.

Accessibility Levels for class,enum,struct,interface

Answer»

Below table HELPS to UNDERSTAND what are the different member DEFAULT accessibility and what accessibility they can opt and what they cannot opt.

MemberTypeDefault accessibilityAllowed accessibility
enumPUBLICNone
classPRIVATEpublic
protected

internal
private
protected
internal
interface public None
struct private public
internal
private
17.

Can we declare Classes and Structs as static?

Answer»

Can we DECLARE CLASSES and STRUCTS as STATIC?
No we can not CREATE Structs as static we only can declare static classes.

18.

What do you think csharp have Its Own Class Library?

Answer»

What do you think csharp have Its Own CLASS LIBRARY?
Not EXACTLY we can say this ACTUALLY c# use class libaray of .NET FRAMEWORK. C# does not have its own class library.

19.

How to get the name of all folder and bind that list to dropdownlist

Answer»

Below CODE will HELPS us to get LIST of all the folder name in SPECIFIED folder and bind that list to dropdownlist
DirectoryInfo dd= new DirectoryInfo("C: ");
DirectoryInfo[] dd= dd.GetDirectories();
foreach (DirectoryInfo dinfo in dd)
{
DropDownList1.Items.Add(dinfo .Name);
}

20.

Is it possible to place Virtual Fields inside a Class?

Answer»

Is it possible to place VIRTUAL FIELDS inside a Class?
No it is not possible in a class we can not CREATE a virtual fields. Only methods, properties, events and indexers can be virtual.

21.

Syntax to create datatable in c#

Answer»

Below CLASS is USED to create a table and how to use that table
using System;
using System.Data;

class CreateTableProgram
{
static void Main()
{
//
//Create a table object and GET the DATATABLE.
//
DataTable table = GetTable();
}
/// Below is syntax to generates a DataTable.
static DataTable GetTable()
{
//
// Here we create a DataTable with four columns.
//
DataTable table = new DataTable();
table.Columns.Add("EmployeeId", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Address", typeof(string));
//
// Here we add some DataRows.
//
table.Rows.Add(1, "SURESH", "Delhi");
table.Rows.Add(2, "Mahesh", "Mumbai");
table.Rows.Add(3, "Gitesh", "Haryana");
table.Rows.Add(4, "Vitesh", "Goa");
return table;
}
}

22.

Swap two int with temp variable and without temp variable

Answer»

Below SOURCE code will help you to swap values between two INTEGER with help of temp variable and without temp variable
//define two variable
int i=2;
int j=6;

//with temp varible
int temp //as a temp varible
//swapping
int temp=i; //swap first variable to temp variable
int i=j; //swap value of SECOND variable to first
j=temp //swap temp to second varible

//without temp varible
i=i+j; //ADD both number
j=i-j; //subtract second from first
i=i-j; //subtract second from first

23.

StreamReader and StreamWriter with example

Answer»

StreamWriter and StreamReader taking twice the memory footprint in some cases over the basic FileStream. StreamReader is designed for character input in a PARTICULAR encoding, whereas the Stream class is designed for byte input and output.StreamReader is nothing but file pointer.
StreamReader is USED to read data from the file.
StreamWriter is used to write data into the file.

StreamReader methods:-Below are the some MAIN methods in streamreader
1)Read() : Reads single character.
2)ReadLine() : Reads single line.
3)ReadToEnd() : Reads full file.

StreamWriter methods:-Below are the some main methods in streamWriter
1)Write()
2)WriteLine()

Example:-Below is example to streamreader text file
using System;
using System.IO;
class Teststreamreader
{
public static void Main()
{
try
{
// create instance of streamreader.
using (StreamReader stread = new StreamReader("FileName.txt"))
{
string strline;
// Read and display lines of file
while ((strline = stread.ReadLine()) != null)
{
Console.WriteLine(strline);
}
}
}
CATCH (Exception e)
{
Console.WriteLine("Problem with file could not be read:");
Console.WriteLine(e.Message);
}
}
}

Example:-Below is example to streamwriter on text file
using System;
using System.IO;
public class testStearmwriter
{
public static void Main(String[] args)
{
FileStream sb = new FileStream("FileName.txt", FileMode.OpenOrCreate);
char[] b = {'b','b','c','d','e','f','g','h','i','j','k','L','m'};
StreamWriter sw = new StreamWriter(sb);
sw.Write(b, 3, 8);
sw.Close();
}
}

24.

What is static modifier and its example

Answer»

AS the name suggest static Static modifier are used to declare a static member and belongs to type itself rather than to specific object. We can use static modifier with fields, operators, properties, events, constructors and METHODS but we cannot used with indexers, destructors, or types.Once we declare static varible it will automatically initialized in memory by there default value or value defined by user once we will initialized static VARIABLE it will never reinitialized in memory on other hand local variables local variable reinitialized in memory every time they call as well as all the objects of the CLASS SHARE the same address of static variable.
In C# the static keyword indicates a class variable. In VB the EQUIVALENT keyword is Shared. Its scoped to the class in which it occurs.
Below is the example to understand what static is and where to use static
class TestStatic
{
static int a = b;
static int b = 1;
static void Main()
{
Console.WriteLine(TestStatic.a);
Console.WriteLine(TestStatic.b);
TestStatic.a = 2;
Console.WriteLine(TestStatic.a);
}
}

-----Output-------
0 ///by default it will take 0 value of a
1
2

25.

Simplest definition of partial classes and its rules?

Answer»

Simplest definition of partial classes and its rules?
Partial class is usually used to split definition of class in 2 or more classes which should be either in one source code file or in more than one source file. Here we can create class definition in multiple files but these are compiled as only one class at RUN time. And when we create a instance of this partial class then we can access all methods from source files with same object. So we used "Partial" keyword with all the class names that we want to bind together with the same name of a class in the same namespace. Below image will clarify you the definition of partial class:-


Partial Class
Rules:-
(1)All definition of partial class must be in same seembly and namespace.
(2)All parts have the same accessibility like PUBLIC or private, etc.
(3)If we declared and part as abstract, sealed or base type then the whole class is declared of the same type.
(4)partial Nested types are allowed.
(5)Different parts can have different base types and so the final class will inherit all the base types.
(6)Partial modifier can only appear immediately before the keywords class, struct, or interface.
(7)Partial methods can be generic or static methods.
(8)Partial methods can have in or REF but not out parameters.
(9)Partial methods are implicitly private methods, so cannot be virtual.
(10)Partial methods must use the partial keyword and must return void.
(11)Partial methods can have in or ref but not out parameters.

26.

Implement two interface with same function define in them

Answer»

Below is the example how to HANDLE two INTERFACE same implementation in the class
(1)Fist Interface declartion
interface Intface1
{
void INTFUNCTION();
}

(2)Second Interface Declartion
interface Intface2
{
void intFunciton();
}

(3)Class that CALL both interface in it
class myClass: Intface1, Intface2
{
public void Intface1.intFunction()
{
// code here functionality for intFunciton of interface Intface1
}
public void Intface2.intFunction()
{
// code here functionality for intFunciton of interface Intface2
}
&nbsp}

27.

Define Race Condition in cSharp?

Answer»

Define Race Condition in cSharp?
When ONE resource is simultaneously access by two threads at same time then Race Conditions OCCURS. And here THREAD which will able to access first cannot be predicted. Now we will take a simple example we have two threads Thread1 and Thread2 and they are trying to access a shared resource called resourcex. And if both are trying to update some values in resourcex then the last value written to resourcex will be SAVED.

28.

Features of c# over Java

Answer»

Below are the some common differences between c# and Java
(1)We can do kind of Delegates in Java, but its not as clean as in c#.
(2)Lambdas- Which are way better than anonymous inner classes. (C# has anonymous inner classes, too.)
(3)LINQ (Language Integrated and Query ) implemented with c#
(4)Operator overloading- This FEATURE can be abused, but it is still occasionally useful, especially in libraries and DSLs.
(5)Properties- No need to write getters and setters. Everything looks like a direct field access, even if it isn t.
(6)Yield CO-Routine capability- This is a powerful and HIGHLY useful capability, especially for LAZY iterators.
(7)Extension METHODS- They permit you to extend existing classes.
(8)Null coalescing operator that provides a simple SYNTAX for dereferencing a reference and supplying a default if the reference is null.

29.

Use of virtual, sealed, override, and abstract

Answer»

(1)The USE of VIRTUAL keyword is to enable or to allow a class to be overridden in the derived class.
(2)The use of sealed keyword is to prevent the class from overridden i.e. you can t inherit sealed classes.
(3)The use of override keyword is to override the virtual method in the derived class.
(4)The use of ABSTRACT keyword is to modify the class, method, and property declaration. You cannot directly make calls to an abstract method and you cannot instantiate an abstract class.

30.

Code to get total time taken by webpage

Answer»

Below CODE is USED to get time taken by webpage to download :-
VOID Application_BeginRequest(OBJECT sender, EventArgs e)
{
Context.Items.Add("startime", DateTime.Now);
}
void Application_EndRequest(object sender, EventArgs e)
{
//Get the start time
DateTime dt=(DateTime)Context.Items["startime"];
//calculate the time difference between start and end of request
TIMESPAN ts = DateTime.Now-dt;
Response.Write("number of milleseconds for execution"+"--"+ts.TotalMilliseconds);
}

31.

How to use Extension Methods in C#

Answer»

Extension methods is a new feature of C# 3.0 that allows you to enhance an existing FRAMEWORK CLASS by adding a new method to it without modifying the ACTUAL code of that class.Suppose you want to calculate the 4th power of an integer. For that you will have to square it two times. So instead of that you can create an Extension Method on integer
public static class ExtensionExample
{
public static int powerfour(this int myNumber)
{
return myNumber ;
}
}
Extension Methods are created in a static class and the Extension Method must be defined as static as shown above. The this keyword PRECEDING the first PARAMETER creates an extension method that applies to the type of that parameter. So here an Extension Method called Cube to the int type is created.

32.

Is it possible to pass default value in parameter in c#

Answer» YES in CSHARP vesion 4.0 we have facilty to pass parametrs with default value to get this done we take an example as below:-
class ProgramDefaultVal
{
public static void mydefmethod(STRING dfval="testvalue")
{
Console.WriteLine(dfval);
}
static void MAIN()
{
mydefmethod();
}
}
33.

C#.Net Reserve Words

Answer»

virtual, class, abstract , as , base , byte , char , decimal ,int , sbyte , uint , ulong , UNCHECKED , unsafe , ushort , bool , BREAK , case , catch , finally , checked , const , continue , default , delegate , do , double , else , enum , event , explicit , EXTERN , false , fixed , float , for , foreach , goto , if , implicit , in , interface , internal , is , LOCK , long , namespace , new , null , object , operator , out , override , params , private , protected , public , readonly , ref , return , sealed , short , SIZEOF , stackalloc , static , string , struct , switch , while , this , throw , true , try , typeof , using , void , volatile

34.

can we inherit private class level variables in c#

Answer»

Yes we can do this but we cannot ACCESS them. Although they are not VISIBLE or ACCESSIBLE. But with help of class INTERFACE they are INHERITED.

35.

How to write alphabets from A to Z from for loop

Answer»

Below code is USED to type ALPHABETS from a to Z
for (CHAR CH = 'A'; ch < 'Z'; ch++)
{
Response.Write(ch + "
");
}

36.

Code to get number is perfect or not

Answer»

Below code will help you to get wheather number is a perfect or not if it is perfect number then it will return TRUE

public boolean isPerfect(int number)
{
int i = PerfectFactors(number);
if (i == number)
{
return true;
}
ELSE
{
return FALSE;
}
}

public int PerfectFactors(int number)
{
int factor = 0, sum = 0;
for (int i = 1; i < number; i++)
{
if (!(number % i))
{
factor = i;
sum += factor;
}
}
return sum;
}

37.

Syntax to get number of days in a month

Answer»

Response.Write(DateTime.DaysInMonth(2011, 2));