
here will discus top most important c# interview question with examples types
Can you write an example for string and StringBuilder?
Strings in .net are called as Immutable (not modifiable) objects. They are Reference Types and hence are allocated memory on the heap.
Example:
string str = "Microsoft.net";
str.Replace("c", "K"); //Incorrect-str doesn’t change.
Console.WriteLine(str); //Output = Microsoft.net
str = str.Replace("c", "K"); //Correct Usage
Console.WriteLine(str);// Output = "Microsoft.net"
in .Net stringbuilder are mutable it’s means modifabul
class sampleprogram
{
static void Main(string[] args)
{
string obj;
System.Text.StringBuilder sbobj= new System.Text.StringBuilder(20);
Console.WriteLine(sbobj.Capacity); //output=20
for (int i = 0; i < 100; i++)
sbobj.Append("A");
Console.WriteLine(sbobj.Capacity); //output=128
Console.WriteLine(sbobj.Length); //output=100
obj= sbobj.ToString();
}
}
write an example for boxing and unboxing?
Boxing: in .net boxing means converting values types to object(reference types).
Unboxing: in. net unboxing means to convert the object(reference types) to values type
class sampleprogram
{
static void Main(string[] args)
{
int n = 30;
object ob = n; //boxing
int m = (int)ob; //unboxing
}
}
write an example for a single-dimensional array?
using System;
class Sampleprogram
{
static void Main(string[] args)
{
int[] sda= new int[] { 3, 6, 7, 8 };
Console.WriteLine(sda.Length);
Console.WriteLine (sda.Rank); //Number of Dimensions.
foreach (int x in sda)
Console.WriteLine(x);
}
}
Explain the method overloading concept with example?
class Program
{
public static void Main()
{
string str;
str = Add("Tutorials", "Helper");
Console.WriteLine(str);
res = Add(20, 2, 4); // Arguments
res = Add(20, 2, d: 4);
res = Add(b: 4, a: 20, d: 4);
}
static int Add(int a, int b)
{
return Add(a, b, 0);
}
static int Add(int a, int b, int c = 0, int d = 0)
{
return a + b + c + d;
}
static string Add(string s1, string s2)
{
return s1 + s2;
}
}
write an example for an inheritance?
see the Inheritance in c# with real time example
public class Parent
{
public int public_A;
private int private_A;
protected int protected_A;
public Parent()
{ }
public Parent(int a, int b, int c)
: this()
{
this.private_A = a;
this.protected_A = b;
this.public_A = c;
}
}
public class Child : Parent
{
public int pubB;
public Child() //First calls parent class constructor.
{ }
public Child(int a, int b, int c, int d)
: base(a, b, c)
{
this.pubB = d;
}
public void Foo()
{
Parent p = new Parent();
Child c = new Child();
protected_A = 10; //Allowed
c.protected_A = 10; //Allowed
//p.ProA = 10; //Compilation Error “p” is of type parent.
}
}
class Program
{
public static void Main()
{
Child obj;
obj = new Child(1, 2, 3, 4);
obj.Foo();
}
}
Write an example for the Abstract?
for more details about abstract class and abstract method in c#
abstract class Figure
{
public int Dimension;
public abstract double Area();
public abstract double Perimeter();
}
class Square : Figure
{
public override double Area()
{
return Dimension * Dimension;
}
public override double Perimeter()
{
return 4 * Dimension;
}
}
class Circle : Figure
{
public override double Area()
{
return Math.PI * Dimension * Dimension;
}
public override double Perimeter()
{
return 2 * Math.PI * Dimension;
}
}
class Program
{
public static void Main()
{
Figure fig = new Square(); // or Circle();
fig.Dimension = 10;
Console.WriteLine(fig.Area());
Console.WriteLine(fig.Perimeter());
}
}
resource: