C# Interview Question


Q1) What is the difference between int and Uint or sbyte vs byte, long ulong , short or ushort?

Ans: sbyte mean signed 8-bit integer i.e -128 to 127, whereas byte mean unsigned 8-bit integer i.e 0 to 255, same for other follow the image.

Q2) How to print double quotations in a string value. i.e display the string value which contains " (double quotation) i.e Codepedia.info" blog.

Ans: Here we use an escape sequence to display double quotation. i.e backlash \. 

Sample Code:

string strValue= "Codepedia.info\".info";

Q3)How to display string variable value into a new line for every new word. i.e Welcome to Codepedia is the single-line string that would be displayed as different 3 lines.
Ans: Here we user \n which make newline. We replace each whitespace with \n.
Sample Code :
string strValue="Welcome to Codepedia";
strValue=strValue.Replace(" ","\n");

Q4) What is verbatim literal in C#?
Ans: Verbatim literal, is a string with an @ symbol prefix, as in @"Hello World"
Verbatim literal makes escape sequences translate as normal printable characters to enhance readability.
Sample Code:

-- WithOut verbatim literal
"C:\\folder\\filename" -- less readable

--With verbatim literal
@"C:\folder\filename" -- better readable

Q5) Difference between Static method Vs Instance Methods?
Ans: When a method declaration includes a static modifier, that method is said to be a static method.
When no static modifier is present, the method is said to be an instance method.

A static method is invoked using the class name, whereas an instance method is invoked using an instance of the class.

The difference between the instance method and static method is that multiple instances of a class can be created and each instance has its own separate method. However when a method is static, there is no instance of the method, and you can invoke only that one definition of the static method.

Q: What is abstraction?
A: Abstraction means show only what is necessary. Example color is abstracted to RGB. By just making the combination of these three colors we can achieve any color in the world.

Q: What is Encapsulation?
A: It is a process of hiding all the complex processing from the outside world and makes your object simple.

Q: What is Inheritance?
A: This concept helps to define the parent-child relationship between classes.

Q: What is polymorphism?
A: It's a property of an object to act differently under different conditions. For instance, a simple user object depending on condition can act as an admin or as a data entry object.

Q: What are the different types of polymorphism?
A: 2 types Static and Dynamic. or you can call them runtime or compile-time polymorphism. Static Poly is implemented by overloading and dynamic poly is implemented by overriding or virtual keyword.

Q: What is an abstract class?
A: A abstract class is a special type of class that cannot be instantiated, this means it cannot be used to create objects (to access it, it must be inherited from another class). An abstract class acts as a base class for other class. The full implementation of abstract class is defined by the derived / child classes.
For example below code, snippet shows a simple abstract class/ half defined class called "DatabaseCommon" and later the concrete classes .i.e. "SQLServer" or "Oracle" inherit and define a complete implementation.

Public abstract class DatabaseCommon{


Public class SQLSERVER : DatabaseCommon{

Public class ORACLE : DatabaseCommon{

Q: What is Static Constructor, and where do we use static constructor?
A: Static constructors are used to initializing static filed in a class.
You declare a static constructor by using the keyword static in front of the constructor name.
A static constructor is called only once, no matter how many instances you create.
Static constructors are called before instance Constructor.

Q: Can we prevent a class from overriding?
A: Yes, If we define a class as Sealed then we cannot inherit the class any further.

Q. What are the Differences between Abstract Class and Interface?
A: Abstract Class
1: It can have implemented Methods
2: A class can inherit only one abstract class
3: We go for Abstract classes on such situations where we need
to give common functionality for a group of related classes
4: If we add a new method, then we can provide a default
implementation and so no need to make any change to existing work
5: Static and Instance constants are possible

1: It cannot have implemented Methods.
2: A Class can implement any number of Interfaces
3: We go for Interface on such situations where we need to give
common functionality for a group of un-related classes
4: If we add a new method, then we need to change all the existing work
5: Only Static constants are possible.

Q: What are the differences between Structure and Class?
A: Structure are value types and classes are reference type, So Structure use Stack and classes use heap.
Structure members cannot be declared as protected, but class members can be. We cannot do inheritance in structure.
Structures do not require constructors, while classes require

Q: What is a constructor in C#?
A: Constructor is a class method that is executed when an object of a class is created. Constructor has the same name as the class and is usually used to initialize the data members of the new object.

Q: What is a Destructor?
A: A Destructor has the same name as the class with a tilde character and is used to destroy an instance of a class.

Q: Can a class have more than 1 destructor? 
A: No, a class can have only 1 destructor.

Q: Can structs in C# have destructors?
A: No, structs can have constructors but not destructors, only classes can have destructors.

Q: Can you pass parameters to destructors? 
A: No, you cannot pass parameters to destructors. Hence, you cannot overload destructors.

Q: What is the difference between ref and out parameters?
A: Ref parameter
1: An argument passed to a ref parameter must be first initialized

The ref keyword on a method parameter causes a method to refer to the same variable that was passed as an input parameter for the same method. If you do any changes to the variable, they will be reflected in the variable.
You can even use ref for more than one method parameter.
namespace RefPerameterClass {
  using System;
  public class RefClass {
    public static void CalculateRefPerameter(ref int intRefA) {
      intRefA += 2;
    public static void Main() {
      int i; // variable need to be initialized 
      i = 3;
      CalculateRefPerameter(ref i);
Out parameter
1: An argument passed to an output parameter does not need to be explicitly initialized.

The out parameter can be used to return the values in the same variable passed as a parameter of the method. Any changes made to the parameter will be reflected in the variable.

 public class OutPerameterClass {
  public static int CalculateOutPerameter(out int intOutA, out int intOutB) {
    intOutA = 10;
    intOutB = 20;
    return 0;
  public static void Main() {
    int i, j; // variable need not be initialized
    Console.WriteLine(CalculateOutPerameter(out i, out j));

Q: Difference between EXE and DLL?
A: 1. .EXE is an executable file and can run by itself as an application, whereas.DLL is usually consumed by a .EXE or by another .DLL, and we cannot run or execute .DLL directly.
   2. For example, In .NET, compiling a Console Application or a Windows Application generates .EXE, whereas compiling a Class Library Project or an ASP.NET web application generates .DLL. In .NET framework, both .EXE and .DLL are called assemblies.

Q: I have a page that contains 1 textbox and a button, on submit I want the textbox value to be populated on another page textbox.
A: using QueryString, using Session, using LocalStorage (HTML % new features), Save in Database, and populate on page load.

Q: What are httphandlers and HttpModules?
A: Handlers and modules help to inject pre-processing logic that is invoked depending on file extensions. Httpmodule is an event-based processor.

Q: What is the difference between server-side and client-side code?
A: Server-side code is executed on the server-side on IIS in Asp.net, while client-side is executed on the browser.

Q: How to print 1 to 100 in SQL?
; with CTE as  
 select 1 Number  
 union all  
 select Number +1 from CTE where Number<100  
select *from CTE  
DECLARE @site_value INT;
SET @site_value = 0;

WHILE @site_value <= 10
   PRINT 'Printing '+ site_value;
   SET @site_value = @site_value + 1;


Q: What is the use of =,==,=== operators?
A: = is used to assign one value or variable to another variable. == is used for comparing two strings or numbers. === is used to compare only string with the string and number with numbers.

Q: What is the difference between varchar and nvarchar types?
A: Varchar and nvarchar are the same but the only difference is that nvarhcar can be used to store Unicode characters for multiple languages and it also takes more space when compared with varchar.

Q: What is the Difference between Table Aliases and Column Aliases? Do they Affect Performance?
A: Usually, when the name of the table or column is very long or complicated to write, aliases are used to refer to them.
SELECT VeryLongColumnName col1
		FROM VeryLongTableName tab1
In the above example, col1 and tab1 are the column alias and table alias, respectively. They do not affect the performance at all.

Q: What is the difference between Delete and truncate?
A: Delete table can have criteria while truncate cannot.
The truncate table does not invoke trigger.
Delete table syntax logs the deletes thus the delete operation is slow. The truncate table does not log any information, but it logs information about the deallocation of the data page of the table so truncate is faster as compared to delete the table.

Q: Different between  UNION and UNION ALL?
A: UNION is used to select information from two tables but it selects only distinct records from both tables, while union all select all records from both the tables.

Q: Difference between primary key and unique key in SQL Server?
A: A table can have only one primary key. On the other hand, a table can have more than one unique key.
   The primary key column does not accept any null values, whereas a unique key column accepts one null value.

Q: What is the difference between the Having and Where clause?
A: A WHERE clause is used in the select statement to filter the rows as they are retrieved from the database table. HAVING clause is used in the select statement in conjunction with the Group By clause, to filter the query results after they have been grouped.
You can use the HAVING clause only when you use Group By clause.

What are the 2 types of Indexes in SQL Server?
A: 1. Clustered Index 
     2. Non-Clustered Index.

Q:  What is a stored procedure?
A: A Stored Procedure is a collection or a group of T-SQL statements. Stored Procedures are a precompiled set of one or more statements that are stored together in the database. They reduce the network load because of the precompilation. We can create a Stored Procedure using the "Create proc" statement.

Q: What is a trigger in SQL Server?
A: A Trigger is a Database object just like a stored procedure or we can say it is a special kind of Stored Procedure which fires when an event occurs in a database."
What are the two types of Triggers in SQL Server?
1. After Triggers: Fired after Insert, Update and Delete operations on a table.
2. Instead of Triggers: Fired instead of Insert, Update and Delete operations on a table.

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.

Post Comment

Your email address will not be published. Required fields are marked *