Home C# Interview Question and Answer

C# Interview Question and Answer

What’s the advantage of using System.Text.StringBuilder over System.String? StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it’s being operated on, a new instance is created.
Can you store multiple data types in System.Array? No
What’s the difference between the System.Array.CopyTo() and System.Array.Clone()? The first one performs a deep copy of the array, the second one is shallow
How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods.
What’s the .NET datatype that allows the retrieval of data by a unique key? HashTable
What’s class SortedList underneath? A sorted HashTable.
Will finally block get executed if the exception had not occurred? Yes
What’s the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception? A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.
Can multiple catch blocks be executed? No, once the proper catch code fires off, the control is transferred to the finally block (if there are any), and then whatever follows the finally block
Why is it a bad idea to throw your own exceptions? if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project.
What’s a delegate? A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.
What’s a multicast delegate? It’s a delegate that points to and eventually fires off several methods
How’s the DLL Hell problem solved in .NET? Assembly versioning allows the application to specify not only the library it needs to run (which was available under Win32), but also the version of the assembly.
What are the ways to deploy an assembly? An MSI installer, a CAB archive, and XCOPY command
What’s a satellite assembly? When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.
What namespaces are necessary to create a localized application System.Globalization, System.Resources.
What’s the difference between // comments, /* */ comments and /// comments? Single-line, multi-line and XML documentation comments.
How do you debug an ASP.NET Web application? Attach the aspnet_wp.exe process to the DbgClr debugger.
Can you change the value of a variable while debugging a C# application? Yes, if you are debugging via Visual Studio.NET, just go to Immediate window.
What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET? SQLServer.NET data provider is high-speed and robust, but requires SQL Server license purchased from Microsoft. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access and Informix, but it’s a .NET layer on top of OLE layer, so not the fastest thing in the world. ODBC.NET is a deprecated layer provided for backward compatibility to ODBC engines.
What’s the role of the DataReader class in ADO.NET connections? It returns a read-only dataset from the data source when the command is executed.
What does Dispose method do with the connection object? Deletes it from the memory.
What is a pre-requisite for connection pooling? Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.
Does C# support multiple-inheritance? No. but can implement uinsg inheritance
Who is a protected class-level variable available to? It is available to any sub-class (a class inheriting this class).
Are private class-level variables inherited? Yes, but they are not accessible. Although they are not visible or accessible via the class interface, they are inherited.
Describe the accessibility modifier “protected internal”. It is available to classes that are within the same assembly and derived from the specified base class.
What’s the top .NET class that everything is derived from? System.Object.
Can you store multiple data types in System.Array?
No
Explain serialization?
Serialization is a process of converting an object into a stream of bytes. .Net has 2 serializers namely XMLSerializer and SOAP/BINARY Serializer. Serialization is maily used in the concept of .Net Remoting
Is it possible to force garbage collector to run? Yes, we can force garbage collector to run using System.GC.Collect().
Define Delegate. Delegates are kind of similar to the function pointers. But they are secure and type-safe.
A delegate instance encapsulates a static or an instance method.
Declaring a delegate defines a reference type which can be used to encapsulate a method having a specific signature.
What is Assembly manifest? The manifest of an assembly contains assembly’s data like version, scope, security information (strong name),etc.
It also contains a reference to the resource and classes.
It is stored in either an .exe or a .dll with Microsoft intermediate language (MSIL) code.
Can you prevent your class from being inherited by another class? Yes. The keyword “sealed” will prevent the class from being inherited.
Can you allow a class to be inherited, but prevent the method from being over-ridden? Yes. Just leave the class public and make the method sealed.
When do you absolutely have to declare a class as abstract?
1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.2. When at least one of the methods in the class is abstract.
What is an interface class? Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes.
Why can’t you specify the accessibility modifier for methods inside the interface?
They all must be public, and are therefore public by default.
What happens if you inherit multiple interfaces and they have conflicting method names? It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay.
What’s the difference between an interface and abstract class?
In an interface class, all methods are abstract – there is no implementation. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed. An abstract class may have accessibility modifiers.
What is the difference between a Struct and a Class?