YouTube Icon

Interview Questions.

Top 50 Advanced C# Interview Questions - Jul 25, 2022

fluid

Top 50 Advanced C# Interview Questions

Q1. What Is Unsafe Code?

In order to maintain protection and type safety, C# does now not help pointer normally. But by way of the use of risky keyword we will define an risky context in which pointer can be used. The hazardous code or unmanaged code is a code block that makes use of a pointer variable. In the CLR, dangerous code is called unverifiable code. In C#, the hazardous code isn't necessarily dangerous. The CLR does now not verify its protection. The CLR will only execute the unsafe code if it's miles inside a fully relied on meeting. If we use unsafe code, it's far our personal duty to ensure that the code does now not introduce safety dangers or pointer errors.

Q2. 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 have to be capable to keep all of the elements in the supply array from the index this is unique to replicate from the source array.

System.Array.Clone()-->It does no longer require the vacation spot array to be existed as it creates a new one from scratch.

Note-These each are used as a shallow replica.

Q3. What Is Reflection?

Reflection is a technique by using which a pc application can display and regulate its very own structure and conduct. It is a manner to explore the shape of assemblies at run time (instructions, sources, strategies). Reflection is the capability to find out the information approximately gadgets, metadata, and alertness information (assemblies) at run-time. We need to encompass System.Reflection namespace to carry out reflections in C#. For example recollect the following C# codes, to be able to returns a few meta statistics’s.

Public class MyClass

    public digital int Add(int numb1, int numb2)

                    

        return numb1 + numb2;

    

    public virtual int Subtract(int numb1, int numb2)

    

        go back numb1 - numb2;

    

static void Main(string[] args)

    MyClass oMyClass = new MyClass();

    //Type information.

    Type oMyType = oMyClass.GetType();

    //Method statistics.

MethodInfo oMyMethodInfo = oMyType.GetMethod("Subtract");

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

 Console.WriteLine("nMethod info:" + oMyMethodInfo.Name);            

Console.Read();

Q4. Why We Should Use Generics?

Generic affords lot of advantages during programming. We must use generics for the following motives:

It allows growing magnificence, methods which can be type-secure

It is quicker. Because it reduce boxing/un-boxing

It growth the code overall performance

It facilitates to maximize code reuse, and kind safety

Q5. When To Use Serialization?

Serialization is used in the following purposes:

To bypass an item from on utility to some other

In SOAP based internet services

To trfer information via cross platforms, pass devices

Q6. What Is Serialization?

When we need to trport an object through community then we need to convert the item right into a move of bytes. Serialization is a system to convert a complex items into movement of bytes for garage (database, record, cache, and so on) or trfer. Its important reason is to shop the country of an item.

De-serialization is the reverse technique of making an item from a movement of bytes to their original shape.

Q7. What Is The Dll Hell Problem Solved In .Internet ?

In .NET, Assembly versioning lets in the utility to specify not best the library it wishes to run ,however also the model of the meeting.

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

For This,First we call the Sort () approach and then name Reverse() Methods.

Q9. What Is Dynamic Keyword?

The dynamic is a key-word which become added in .NET 4.@Computer programming languages are  sorts: strongly typed and dynamically typed. In strongly types all kinds assessments are befell at assemble time, in dynamic sorts all sorts of assessments are came about at run time.

For example don't forget the following code

dynamic x = "c#";

x++;

It will not offer blunders at bring together time but will provide errors at run time.

Q10. What Is Collections In C#?

Sometimes we need to work with related items for facts storage and retrieval. There are  approaches to work with associated items. One is array and any other one is collections. Arrays are maximum beneficial for growing and running with a fixed variety of strongly-typed objects. Collections are enhancement of array which offers a extra flexible way to work with organizations of gadgets.

The Microsoft .NET framework provides specialised training for data garage and retrieval. Collections are considered one of them. Collection is a records structure that holds records in specific ways. Collections are  sorts. One is popular collections, that is located underneath System.Collections namespace and another one is conventional collections, that's found under System.Collections.Generic namespace.The familiar collections are extra flexible and premiere to work with statistics.

Some typically used collections underneath System.Collections namespace are given bellow:

ArrayList

SortedList

Hashtable

Stack

Queue

BitArray

Q11. What Is The Top .Net Class ?

System.Object

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

Yes.

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

Yes.

Q14. Can Unsafe Code Be Executed In Untrusted Environment?

Unsafe code can not be done in an un-depended on surroundings. For example, we cannot run hazardous code immediately from the Internet.

Q15. Does C# Support Multiple Inheritance ?

No, we use interface for this purpose.

Q16. Can We Store Multiple Data Types In System.Array ?

No.

Q17. What Is Custom Attributes?

The Microsoft .Net Framework permits creating custom attributes that can be used to keep declarative records and can be retrieved at run-time.

Q18. How To Compile Unsafe Code?

For compiling unsafe code, we ought to specify the /dangerous command-line transfer with command-line compiler.

For instance: to collect a software named “myClass.Cs” containing unsafe code the command line command is:

csc /risky myClass.Cs

In Visual Studio IDE in the beginning we want to permit use of risky code inside the project properties.

The steps are given bellow:

Open task homes

Click on the Build tab

Select the choice “Allow risky code”

Q19. Give Examples Where Serialization Is Used?

Serialization is used to save consultation nation in ASP.NET packages, to copy items to the clipboard in Windows Forms. It is also used to bypass objects from one software area to every other. Web offerings uses serialization.

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

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

Q21. What Is The Difference Between Reflection And Dynamic?

Both Reflection and dynamic are used to function on an object during run time. But they have some variations:

Dynamic uses mirrored image internally

Reflection can invoke both public and private contributors of an item. But dynamic can only invoke public individuals of an item

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

Yes, first create magnificence as public and make it is technique sealed.

Q23. When To Use Dynamic?

The biggest sensible use of the dynamic keyword is whilst we operate on MS Office.

Q24. What Is The Implicit Name Of The Parameter That Gets Passed Into Class "set" Method ?

Value and it's datatype(it relies upon something variable we're converting).

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

In an interface, all techniques have to be summary however in abstract class a few strategies may be concrete.In interface No accessibility modifiers are alloweded but in summary magnificence a accessibility modifier are alloweded.

Q26. How Does Method Overloading Different From Overriding ?

A method overloading absolutely involves having a technique with the equal name inside the elegance. Whereas in method overriding we will alternate method behaviour for a derived magnificence.

Q27. What Is Attribute In C#?

An attributes is a declarative tag this is used to deliver information about the behaviors of various factors (lessons, strategies, assemblies, structures, enumerators, etc). It's miles get right of entry to at compile time or run-time. Attributes are declare with a square brackets [] which is places above the factors.

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

For example recollect the bellow class. If we name the vintage method it will via mistakes message.

Public magnificence myClass

    [Obsolete("Don't use Old method, please use New method", true)]

    public string Old()  return "Old"; 

    public string New()  go back "New"; 

myClass omyClass = new myClass();

omyClass.Old();

Q28. Why Attributes Are Used?

In a software the attributes are used for including metadata, like compiler education or other facts (remarks, description, and so on).

Q29. Are Private Class -label Variables Inherited ?

Yes, but it is not reachable.We normally realize that they are inherited but not on hand.

Q30. What Is The .Internet Data Type That Allow The Retrieval Of Data By A Unique Key ?

Hash Table

Q31. What Is Pointer?

Pointer is a variable that stores the reminiscence cope with of every other variable. Pointers in C# have the identical talents as in C or C++.

Some examples are given bellow:

int    *i    // pointer of an integer

flow  *f    // pointer to a drift

double *d    // pointer to a double

char   *ch   // pointer to a character

Q32. What Is An Interface Class ?

This is an summary class with public summary strategies , all of which need to be implemented within the inherited lessons.

Q33. What Is A Satellite Assembly ?

When we write the code, a  multicultural or multilingual utility in .NET and need to distribute the core utility separately from the localized modules,the localized assemblies that adjust the center utility ,this is regarded  as Satellite assembly.

Q34. What Is A Generic Class?

A normal class is a special sort of class which could handle any types of records. We specify the statistics kinds throughout the item creations of that elegance. It is asserted with the bracket <>. For instance don't forget the subsequent Comparer class, which has a method that evaluate  price and returns as Boolean output.

Public class Comparer

    public bool Compare(Unknown t1, Unknown t2)

    

         if (t1.Equals(t2))

      

            go back actual;

        

        else

        

            go back false;

        

    

Comparer oComparerInt = new Comparer();

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

Comparer oComparerStr = new Comparer();

Console.WriteLine(oComparerStr.Compare("jdhsjhds", "10"));

Q35. Why We Need Reflection In C#?

Reflections wished when we want to decide / look into contents of an meeting. For instance: at Visual Studio editor intelligence, when we kind “.” (dot) earlier than any object, it gives us all of the members of the item. This is viable for Reflection.

Beside this we need mirrored image for the following purposes:

To view attribute data at run time

To view the shape of assemblies at run time (training, assets, methods)

It allows dynamic/past due binding to strategies and residences

In serialization, it's far used to serialize and de-serialize items

In net carrier, it's miles used to create and consume SOAP messages and additionally to generate WSDL

Debugging equipment can use mirrored image to examine the nation of an object.

Q36. What Is Multicast Delegate ?

The Multicast delegate is a delegate that points to and ultimately fires off several methods.

Q37. What Are The Types Of Serialization?

The styles of Serializations are given bellow:

1  Binary Serialization

            In this manner all the public, personal, study simplest participants are serialized and convert into flow of bytes. This is used whilst we need a entire conversion of our items.

2  SOAP Serialization

           In this manner simplest public members are converted into SOAP layout. This is utilized in net services.

Three  XML Serialization

            In this method most effective public contributors are transformed into XML. This is a custom serialization. Required namespaces: System.Xml, System.Xml.Serialization.

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

No,Once the right capture code fires off ,the manage is trferred to the sooner or later block(if any),and the some thing follows the ultimately block.

Q39. Should I Use Unsafe Code In C#?

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

To deal with present structures on disk

Some superior COM or Platform Invoke eventualities that involve pointer

To overall performance essential codes

Q40. What Is Signature Used For Overloaded A Method ?

Use different statistics sorts

Use distinctive range of parameters

Use one-of-a-kind order of parameters

Q41. Why Serialization And Deserialization?

For example don't forget, we've a totally complex object and we need XML format to reveal it on HTML page. Then we will create a XML file within the disk, writes all the vital records at the XML file, and use it for the HTML web page. But this isn't always suitable method for huge range of users. Extra area is required; everybody can see the XML record which creates protection issue. We can triumph over it by the use of XML serialization.

Q42. What Are The Types Of Attributes?

The Microsoft .Net Framework gives two types of attributes: the pre-defined attributes and custom constructed attributes.

Pre-outline attributes are three types:

AttributeUsage

Conditional

Obsolete

This marks a software that a few entity have to not be used.

Q43. What Are The Ways To Deploy An Assembly ?

An MSI Installer

A CAB archive

XCopy command

Q44. Can We Inherit Multiple Interfaces ?

Yes.

Q45. Can We Override Private Virtual Method ?

No.

Q46. What Is Difference Between String And Stringbuilder ?

StringBuilder is extra efficient than string.

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

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

Q47. What Is Generics?

Generics are the most effective features brought in C# 2.@It is a type-secure facts shape that allows us to put in writing codes that works for any information kinds.

Q48. What Is Class Sortedlist Underneath?

It is a Hash Table.

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

No.

Q50. What Are The Properties Of Unsafe Code?

Some homes of hazardous codes are given bellow:

We can define Methods, sorts, and code blocks as risky

In some instances, dangerous code can also boom the application’s performance by means of removing array bounds assessments

Unsafe code is needed so as to name local features that require guidelines

Using unsafe code brings protection and balance risks

In order to collect unsafe code, the utility must be compiled with /unsafe




CFG