51+ Advanced C# Interview Questions And Answers

Spread the love

Advanced C# Interview Questions

Question 1. What Is Attribute In C#?

An attributes is a declarative tag that’s used to convey details about the behaviors of varied components (courses, strategies, assemblies, buildings, enumerators, and so forth). it’s entry at compile time or run-time. Attributes are declare with a sq. brackets [] which is locations above the weather.

[Obsolete(“Don’t use Old method, please use New method”, true)]

For instance contemplate the bellow class. If we name the outdated methodology it should by way of error message.

public class myClass

{

[Obsolete(“Don’t use Old method, please use New method”, true)]

public string Old() { return “Old”; }

public string New() { return “New”; }

}

myClass omyClass = new myClass();

omyClass.Old();

Question 2. Why Attributes Are Used?

In a program the attributes are used for including metadata, like compiler instruction or different data (feedback, description, and so forth).

Question 3. What Are The Types Of Attributes?

The Microsoft .Net Framework supplies two sorts of attributes: the pre-defined attributes and customized constructed attributes.

Pre-define attributes are three varieties:

AttributeUtilization
Conditional
Obsolete
This marks a program that some entity shouldn’t be used.

Question 4. What Is Custom Attributes?

The Microsoft .Net Framework permits creating customized attributes that can be utilized to retailer declarative data and may be retrieved at run-time.

Question 5. What Is Reflection?

Reflection is a course of by which a pc program can monitor and modify its personal construction and conduct. It is a strategy to discover the construction of assemblies at run time (courses, assets, strategies). Reflection is the potential to seek out out the details about objects, metadata, and utility particulars (assemblies) at run-time. We want to incorporate System.Reflection namespace to carry out reflections in C#. For instance contemplate the next C# codes, which can returns some meta data’s.

public class MyClass

{

public digital int Add(int numb1, int numb2)

{

return numb1 + numb2;

}

public digital int Subtract(int numb1, int numb2)

{

return numb1 – numb2;

}

}

static void Main(string[] args)

MyClass oMyClass = new MyClass();

//Type data.

Type oMyType = oMyClass.GetType();

//Method data.

MethodInfo oMyMethodInfo = oMyType.GetMethod(“Subtract”);

Console.WriteLine(“nType data:” + oMyType.FullName);

Console.WriteLine(“nMethod information:” + oMyMethodInfo.Name);

Console.Read();

Question 6. Why We Need Reflection In C#?

Reflections wanted once we need to decide / examine contents of an meeting. For instance: at Visual Studio editor intelligence, once we kind “.” (dot) earlier than any object, it offers us all of the members of the item. This is feasible for Reflection.

Beside this we’d like reflection for the next functions:

  • To view attribute data at run time
  • To view the construction of assemblies at run time (courses, assets, strategies)
  • It permits dynamic/late binding to strategies and properties
  • In serialization, it’s used to serialize and de-serialize objects
  • In internet service, it’s used to create and eat SOAP messages and likewise to generate WSDL
  • Debugging instruments can use reflection to look at the state of an object.

Question 7. What Is Dynamic Keyword?

The dynamic is a key phrase which was launched in .NET 4.0. Computer programming languages are two varieties: strongly typed and dynamically typed. In strongly varieties all kinds checks are occurred at compile time, in dynamic varieties all sorts of checks are occurred at run time.

For instance contemplate the next code

dynamic x = “c#”;

x++;

It is not going to present error at compile time however will present error at run time.

Question 8. When To Use Dynamic?

The greatest sensible use of the dynamic key phrase is once we function on MS Office.

Question 9. What Is The Difference Between Reflection And Dynamic?

Both Reflection and dynamic are used to function on an object throughout run time. But they’ve some variations:

Dynamic makes use of reflection internally
Reflection can invoke each private and non-private members of an object. But dynamic can solely invoke public members of an object

Question 10. What Is Serialization?

When we need to transport an object by way of community then we have to convert the item right into a stream of bytes. Serialization is a course of to transform a fancy objects into stream of bytes for storage (database, file, cache, and so forth) or switch. Its most important function is to save lots of the state of an object.

De-serialization is the reverse course of of making an object from a stream of bytes to their authentic type.

Question 11. What Are The Types Of Serialization?

The sorts of Serializations are given bellow:

1 Binary Serialization
In this course of all the general public, personal, learn solely members are serialized and convert into stream of bytes. This is used once we need a full conversion of our objects.
2 SOAP Serialization
In this course of solely public members are transformed into SOAP format. This is utilized in internet companies.
Three XML Serialization
In this course of solely public members are transformed into XML. This is a customized serialization. Required namespaces: System.Xml, System.Xml.Serialization.

Question 12. Why Serialization And Deserialization?

For instance contemplate, we now have a really complicated object and we’d like XML format to indicate it on HTML web page. Then we are able to create a XML file within the disk, writes all the mandatory knowledge on the XML file, and use it for the HTML web page. But this isn’t good strategy for giant variety of customers. Extra area is required; anybody can see the XML file which creates safety subject. We can overcome it by utilizing XML serialization.

Question 13. When To Use Serialization?

Serialization is used within the following functions:

To cross an object from on utility to a different
In SOAP based mostly internet companies
To switch knowledge by way of cross platforms, cross gadgets

Question 14. Give Examples Where Serialization Is Used?

Serialization is used to save lots of session state in ASP.NET purposes, to repeat objects to the clipboard in Windows Forms. It can be used to cross objects from one utility area to a different. Web companies makes use of serialization.

Question 15. What Is Generics?

Generics are essentially the most highly effective options launched in C# 2.0. It is a type-safe knowledge construction that enables us to write down codes that works for any knowledge varieties.

Question 16. What Is A Generic Class?

A generic class is a particular form of class that may deal with any sorts of knowledge. We specify the info varieties throughout the object creations of that class. It is said with the bracket <>. For instance contemplate the next Comparer class, which has a way that examine two worth and returns as Boolean output.

public class Comparer

{

public bool Compare(Unknown t1, Unknown t2)

{

if (t1.Equals(t2))

{

return true;

}

else

{

return false;

}

}

}

Comparer oComparerInt = new Comparer();

Console.WriteLine(oComparerInt.Compare(10, 10));

Comparer oComparerStr = new Comparer();

Console.WriteLine(oComparerStr.Compare(“jdhsjhds”, “10”));

Question 17. Why We Should Use Generics?

Generic supplies lot of benefits throughout programming. We ought to use generics for the next causes:

It permits creating class, strategies that are type-safe
It is quicker. Because it scale back boxing/un-boxing
It enhance the code efficiency
It helps to maximise code reuse, and kind security

Question 18. What Is Collections In C#?

Sometimes we have to work with associated objects for knowledge storage and retrieval. There are two methods to work with associated objects. One is array and one other one is collections. Arrays are most helpful for creating and dealing with a set variety of strongly-typed objects. Collections are enhancement of array which supplies a extra versatile strategy to work with teams of objects.

The Microsoft .NET framework supplies specialised courses for knowledge storage and retrieval. Collections are certainly one of them. Collection is an information construction that holds knowledge in several methods. Collections are two varieties. One is normal collections, which is discovered beneath System.Collections namespace and one other one is generic collections, which is discovered beneath System.Collections.Generic namespace.The generic collections are extra versatile and preferable to work with knowledge.

Some generally used collections beneath System.Collections namespace are given bellow:

ArrayChecklist
SortedList
Hashtable
Stack
Queue
BitArray

Question 19. What Is Unsafe Code?

In order to take care of safety and kind security, C# doesn’t help pointer typically. But by utilizing unsafe key phrase we are able to outline an unsafe context through which pointer can be utilized. The unsafe code or unmanaged code is a code block that makes use of a pointer variable. In the CLR, unsafe code is known as unverifiable code. In C#, the unsafe code isn’t essentially harmful. The CLR doesn’t confirm its security. The CLR will solely execute the unsafe code whether it is inside a completely trusted meeting. If we use unsafe code, it’s our personal accountability to make sure that the code doesn’t introduce safety dangers or pointer errors.

Question 20. What Are The Properties Of Unsafe Code?

Some properties of unsafe codes are given bellow:

We can outline Methods, varieties, and code blocks as unsafe
In some instances, unsafe code could enhance the appliance’s efficiency by eradicating array bounds checks
Unsafe code is required with a purpose to name native features that require pointers
Using unsafe code brings safety and stability dangers
In order to compile unsafe code, the appliance should be compiled with /unsafe

Question 21. Can Unsafe Code Be Executed In Untrusted Environment?

Unsafe code can’t be executed in an un-trusted setting. For instance, we can not run unsafe code immediately from the Internet.

Question 22. How To Compile Unsafe Code?

For compiling unsafe code, we now have to specify the /unsafe command-line change with command-line compiler.

For instance: to compile a program named “myClass.cs” containing unsafe code the command line command is:

csc /unsafe myClass.cs

In Visual Studio IDE at first we have to allow use of unsafe code within the undertaking properties.

The steps are given bellow:

Open undertaking properties
Click on the Build tab
Select the choice “Allow unsafe code”

Question 23. What Is Pointer?

Pointer is a variable that shops the reminiscence tackle of one other variable. Pointers in C# have the identical capabilities as in C or C++.

Some examples are given bellow:

int *i // pointer of an integer
float *f // pointer to a float
double *d // pointer to a double
char *ch // pointer to a personality

Question 24. Should I Use Unsafe Code In C#?

In C#, pointer is absolutely used and Microsoft disengaged to make use of it. But there are some conditions that require pointer. We can use pointer if required at our personal danger. Some sonorous are given bellow:

To cope with present buildings on disk
Some superior COM or Platform Invoke eventualities that contain pointer
To efficiency important codes

Question 25. How Can We Sort The Elements Of The Array In Descending Order ?

For This,First we name the Sort () methodology after which name Reverse() Methods.

Question 26. Can We Store Multiple Data Types In System.array ?

No.

Question 27. What Is The Difference Between The System.array.copyto() And System.array.clone() ?

System.Array.CopyTo()–>It require a vacation spot array to be existed earlier than and it should be succesful to carry all the weather within the supply array from the index that’s specified to repeat from the supply array.

System.Array.Clone()–>It doesn’t require the vacation spot array to be existed because it creates a brand new one from scratch.

Note-These each are used as a shallow copy.

Question 28. What Is Difference Between String And Stringbuilder ?

StringBuilder is extra environment friendly than string.

String :- It is Immutable and resides inside System Namespace.

StringBuilder:-It is mutable and resides System.Text Namespace.

Question 29. What Is Class Sortedlist Underneath?

It is a Hash Table.

Question 30. What Is The .internet Data Type That Allow The Retrieval Of Data By A Unique Key ?

Hash Table

Question 31. Is Finally Block Get Executed If The Exception Is Not Occured ?

Yes.

Question 32. Can Multiple Catch () Block Get Executed If The Exception Is Not Occured ?

No,Once the correct catch code fires off ,the management is transferred to the lastly block(if any),and the no matter follows the lastly block.

Question 33. What Is Multicast Delegate ?

The Multicast delegate is a delegate that factors to and ultimately fires off a number of strategies.

Question 34. What Are The Ways To Deploy An Assembly ?

An MSI Installer
A CAB archive
XCopy command

Question 35. What Is The Dll Hell Problem Solved In .internet ?

In .NET, Assembly versioning permits the appliance to specify not solely the library it must run ,but additionally the model of the meeting.

Question 36. What Is A Satellite Assembly ?

When we write the code, a multicultural or multilingual utility in .NET and need to distribute the core utility individually from the localized modules,the localized assemblies that modify the core utility ,that is called Satellite meeting.

Question 37. How Do We Inherit From A Class In C# ?

In c#, we use a colon (:) after which the identify of the bottom class.

Question 38. Does C# Support Multiple Inheritance ?

No, we use interface for this function.

Question 39. Are Private Class -label Variables Inherited ?

Yes, however it isn’t accessible.we typically know that they’re inherited however not accessible.

Question 40. What Is The Implicit Name Of The Parameter That Gets Passed Into Class “set” Method ?

Value and it is datatype(it relies upon no matter variable we’re altering).

Question 41. What Is The Top .internet Class ?

System.Object

Question 42. How Does Method Overloading Different From Overriding ?

A way overloading merely includes having a way with the identical identify inside the class. whereas in methodology overriding we are able to change methodology behaviour for a derived class.

Question 43. Can We Override Private Virtual Method ?

No.

Question 44. Can We Declare The Override Method Static While The Original Method Is Non Static ?

No.

Question 45. Can We Prevent My Class From Being Inherited And Becoming A Base Class From The Other Classes ?

Yes.

Question 46. What Is An Interface Class ?

This is an summary class with public summary strategies , all of which should be carried out within the inherited courses.

Question 47. Can We Inherit Multiple Interfaces ?

Yes.

Question 48. Can We Allow Class To Be Inherited ,however Prevent The Method From Being Overridden ?

Yes, first create class as public and make it is methodology sealed.

Question 49. What Is Signature Used For Overloaded A Method ?

  • Use completely different knowledge varieties
  • Use completely different variety of parameters
  • Use completely different order of parameters

Question 50. What Is The Difference Between An Interface And Abstract Class ?

In an interface, all strategies should be summary however in summary class some strategies may be concrete.In interface No accessibility modifiers are alloweded however in summary class a accessibility modifier are alloweded.


Spread the love