This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Wednesday, March 30, 2022

c# interview questions and answers for freshers

c# interview questions and answers for freshers

In this Article Today Learn c# interview questions and answers for freshers follow the c# interview questions and answers for freshers.

1. How is C# different from C?

You would always know C is the procedural language while C# is a more object-oriented language. The biggest difference is that C# supports automatic garbage collection by Common Language Runtime (CLR) while C does not. C# primarily needs a .NET framework to execute while C is a platform-agnostic language. 


2. What is Common Language Runtime (CLR)?

CLR handles program execution for various languages including C#. The architecture of CLR handles memory management, garbage collection, security handling, and looks like: 


3. What is garbage collection in C#?

Garbage collection is the process of freeing up memory that is captured by unwanted objects. When you create a class object, automatically some memory space is allocated to the object in the heap memory. Now, after you perform all the actions on the object, the memory space occupied by the object becomes waste. It is necessary to free up memory. Garbage collection happens in three cases:


If the occupied memory by the objects exceeds the pre-set threshold value.

If the garbage collection method is called

If your system has low physical memory


4. What are the types of classes in C#?

Class is an entity that encapsulates all the properties of its objects and instances as a single unit. C# has four types of such classes:


Static class: Static class, defined by the keyword ‘static’ does not allow inheritance. Therefore, you cannot create an object for a static class.

Sample code:


static class classname  

{  

  //static data members  

  //static methods  

}


Partial class: Partial class, defined by the keyword ‘partial’ allows its members to partially divide or share source (.cs) files.

Abstract class: Abstract classes are classes that cannot be instantiated where you cannot create objects. Abstract classes work on the OOPS concept of abstraction. Abstraction helps to extract essential details and hide the unessential ones.

Sealed class: Sealed classes are classes that cannot be inherited. Use the keyword sealed to restrict access to users to inherit that class. 

 

sealed class InterviewBit

{

   // data members

   // methods

   .

   .

   .

}

5. What is a managed and unmanaged code?

Managed code lets you run the code on a managed CLR runtime environment in the .NET framework. 

Managed code runs on the managed runtime environment than the operating system itself. 

Benefits: Provides various services like a garbage collector, exception handling, etc. 


Unmanaged code is when the code doesn’t run on CLR, it is an unmanaged code that works outside the .NET framework. 

They don’t provide services of the high-level languages and therefore, run without them. Such an example is C++. 


6. What is the difference between an abstract class and an interface?

Let’s dig into the differences between an abstract class and an interface:


Abstract classes are classes that cannot be instantiated ie. that cannot create an object. The interface is like an abstract class because all the methods inside the interface are abstract methods.

Surprisingly, abstract classes can have both abstract and non-abstract methods but all the methods of an interface are abstract methods.

Since abstract classes can have both abstract and non-abstract methods, we need to use the Abstract keyword to declare abstract methods. But in the interface, there is no such need.

An abstract class has constructors while an interface encompasses none. 


Ex.


Abstract class:


public abstract class Shape{

public abstract void draw();

}

Interface:


public interface Paintable{

void paint();

}

7. What are the differences between ref and out keywords?

C# ref keywords pass arguments by reference and not value. To use the ‘ref’ keyword, you need to explicitly mention ‘ref’. 


void Method(ref int refArgument)

{

   refArgument = refArgument + 10;

}

int number = 1;

Method(ref number);

Console.WriteLine(number);

// Output: 11


C# out keywords pass arguments within methods and functions. 

‘out’ keyword is used to pass arguments in a method as a reference to return multiple values. Although it is the same as the ref keyword, the ref keyword needs to be initialised before it is passed. Here, The out and ref keywords are useful when we want to return a value in the same variables that are passed as an argument. 


public static string GetNextFeature(ref int id)  

{  

   string returnText = "Next-" + id.ToString();  

   id += 1;  

   return returnText;  

}  

public static string GetNextFeature(out int id)  

{  

   id = 1;  

   string returnText = "Next-" + id.ToString();  

   return returnText;  

}   

8. What are extension methods in C#?

Extension methods help to add new methods to the existing ones. The methods that are added are static. At times, when you want to add methods to an existing class but don’t perceive the right to modify that class or don’t hold the rights, you can create a new static class containing the new methods. Once the extended methods are declared, bind this class with the existing one and see the methods will be added to the existing one.


// C# program to illustrate the concept

// of the extension methods

using System;

 

namespace ExtensionMethod {

static class NewMethodClass {

 

   // Method 4

   public static void M4(this Scaler s)

   {

       Console.WriteLine("Method Name: M4");

   }

 

   // Method 5

   public static void M5(this Scaler s, string str)

   {

       Console.WriteLine(str);

   }

}

 

// Now we create a new class in which

// Scaler class access all the five methods

public class IB {

 

   // Main Method

   public static void Main(string[] args)

   {

       Scaler s = new Scaler();

       s.M1();

       s.M2();

       s.M3();

       s.M4();

       s.M5("Method Name: M5");

   }

}

}

Output:


Method Name: M1


Method Name: M2


Method Name: M3


Method Name: M4


Method Name: M5


9. What are Generics in C#?

In C# collections, defining any kind of object is termed okay which compromises C#’s basic rule of type-safety. Therefore, generics were included to type-safe the code by allowing re-use of the data processing algorithms. Generics in C# mean not linked to any specific data type. Generics reduce the load of using boxing, unboxing, and typecasting objects. Generics are always defined inside angular brackets <>. To create a generic class, this syntax is used:


GenericList<float> list1 = new GenericList<float>();

GenericList<Features> list2 = new GenericList<Features>();

GenericList<Struct> list3 = new GenericList<Struct>();

Here, GenericList<float> is a generic class. In each of these instances of GenericList<T>, every occurrence of T in the class is substituted at run time with the type argument. By substituting the T, we have created three different type-safe using the same class.

No comments:

Post a Comment