October 20, 2018

Srikaanth

Marketo Most Frequently Asked Latest C# Interview Questions Answers

How Do You Create Empty Strings In C#?

Using string.empty as shown in the example below.
string EmptyString = string.empty;

What Is The Difference Between System.text.stringbuilder And System.string?

1. Objects of type StringBuilder are mutable where as objects of type System.String are immutable.
2. As StringBuilder objects are mutable, they offer better performance than string objects of type System.String
3. StringBuilder class is present in System.Text namespace where String class is present in System namespace.

How Do You Determine Whether A String Represents A Numeric Value?

To determine whether a String represents a numeric value use TryParse method as shown in the example below. If the string contains nonnumeric characters or the numeric value is too large or too small for the particular type you have specified, TryParse returns false and sets the out parameter to zero. Otherwise, it returns true and sets the out parameter to the numeric value of the string.

string str = "One";
int i = 0;
if(int.TryParse(str,out i))
{
Console.WriteLine("Yes string contains Integer and it is " + i);
}
else
{
Console.WriteLine("string does not contain Integer");
}

Give Some Examples For Built In Datatypes In C#?

1. int
2. float
3. bool

How Do You Create User Defined Data Types In C#?

You use the struct, class, interface, and enum constructs to create your own custom types. The .NET Framework class library itself is a collection of custom types provided by Microsoft that you can use in your own applications.

What Are The 2 Types Of Data Types Available In C#?

1. Value Types
2. Reference Types

If You Define A User Defined Data Type By Using The Struct Keyword, Is It A Value Type Or Reference Type?

Value Type.

If You Define A User Defined Data Type By Using The Class Keyword, Is It A Value Type Or Reference Type?

Reference type

Are Value Types Sealed?

Yes, Value types are sealed.

What Is The Base Class From Which All Value Types Are Derived?

System.ValueType.

Give Examples For Value Types?

Enum
Struct

Give Examples For Reference Types?

Class
Delegate
Array
Interface.
Marketo Most Frequently Asked Latest C# Interview Questions Answers
Marketo Most Frequently Asked Latest C# Interview Questions Answers

What Are The Differences Between Value Types And Reference Types?

1. Value types are stored on the stack where as reference types are stored on the managed heap.
2. Value type variables directly contain their values where as reference variables holds only a reference to the location of the object that is created on the managed heap.
3. There is no heap allocation or garbage collection overhead for value-type variables. As reference types are stored on the managed heap, they have the over head of object allocation and garbage collection.
4. Value Types cannot inherit from another class or struct. Value types can only inherit from interfaces. Reference types can inherit from another class or interface.

What Do You Mean By Casting A Data Type?

Converting a variable of one data type to another data type is called casting. This is also called as data type conversion.

What Are The 2 Kinds Of Data Type Conversions In C#?

Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

Explicit conversions: Explicit conversions require a cast operator. The source and destination variables are compatible, but there is a risk of data loss because the type of the destination variable is a smaller size than (or is a base class of) the source variable.

What Is The Difference Between An Implicit Conversion And An Explicit Conversion?

1. Explicit conversions require a cast operator where as an implicit converstion is done automatically.
2. Explicit conversion can lead to data loss where as with implicit conversions there is no data loss.

What Type Of Data Type Conversion Happens When The Compiler Encounters The Following Code?

ChildClass CC = new ChildClass();
ParentClass PC = new ParentClass();

Implicit Conversion. For reference types, an implicit conversion always exists from a class to any one of its direct or indirect base classes or interfaces. No special syntax is necessary because a derived class always contains all the members of a base class.

If You Want To Convert A Base Type To A Derived Type, What Type Of Conversion Do You Use?

Explicit conversion as shown below.
//Create a new derived type.
Car C1 = new Car();
// Implicit conversion to base type is safe.
Vehicle V = C1;

// Explicit conversion is required to cast back to derived type. The code below will compile but throw an exception at run time if the right-side object is not a Car object.
Car C2 = (Car) V;

What Operators Can Be Used To Cast From One Reference Type To Another Without The Risk Of Throwing An Exception?

The is and as operators can be used to cast from one reference type to another without the risk of throwing an exception.

If Casting Fails What Type Of Exception Is Thrown?

InvalidCastException.

What Is Boxing And Unboxing?

Boxing - Converting a value type to reference type is called boxing. An example is shown below.
int i = 101;
object obj = (object)i; // Boxing

Unboxing - Converting a reference type to a value typpe is called unboxing. An example is shown below.
obj = 101;
i = (int)obj; // Unboxing

Is Boxing An Implicit Conversion?

Yes, boxing happens implicitly.

Is Unboxing An Implicit Conversion?

No, unboxing is an explicit conversion.

What Happens During The Process Of Boxing?

Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object. Due to this boxing and unboxing can have performance impact.


What Is The Difference Between Int.parse And Int.tryparse Methods?

Parse method throws an exception if the string you are trying to parse is not a valid number where as TryParse returns false and does not throw an exception if parsing fails. Hence TryParse is more efficient than Parse.

Why Should You Override The Tostring() Method?

All types in .Net inherit from system.object directly or indirectly. Because of this inheritance, every type in .Net inherit the ToString() method from System.Object class. Consider the example below.

using System;
public class MainClass
{
public static void Main()
{
int Number = 10;
Console.WriteLine(Number.ToString());
}
}

In the above example Number.ToString() method will correctly give the string representaion of int 10, when you call the ToString() method.

If you have a Customer class as shown in the below example and when you call the ToString() method the output doesnot make any sense. Hence you have to override the ToString() method, that is inherited from the System.Object class.

using System;
public class Customer
{
public string FirstName;
public string LastName;
}
public class MainClass
{
public static void Main()
{
Customer C = new Customer();
C.FirstName = "David";
C.LastName = "Boon";
Console.WriteLine(C.ToString());
}
}

The code sample below shows how to override the ToString() method in a class, that would give the output you want.

using System;
public class Customer
{
public string FirstName;
public string LastName;
public override string ToString()
{
return LastName + ", " + FirstName;
}
}
public class MainClass
{
public static void Main()
{
Customer C = new Customer();
C.FirstName = "David";
C.LastName = "Boon";
Console.WriteLine(C.ToString());
}
}

Conclusion : If you have a class or a struct, make sure you override the inherited ToString() method.

Explain Polymorphism In C# With A Simple Example?

Polymorphism allows you to invoke derived class methods through a base class reference during run-time. An example is shown below.
using System;
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("I am a drawing object.");
}
}
public class Triangle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I am a Triangle.");
}
}
public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I am a Circle.");
}
}
public class Rectangle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I am a Rectangle.");
}
}
public class DrawDemo
{
public static void Main()
{
DrawingObject[] DrawObj = new DrawingObject[4];
DrawObj[0] = new Triangle();
DrawObj[1] = new Circle();
DrawObj[2] = new Rectangle();
DrawObj[3] = new DrawingObject();
foreach (DrawingObject drawObj in DrawObj)
{
drawObj.Draw();
}
}
}

When Can A Derived Class Override A Base Class Member?

A derived class can override a base class member only if the base class member is declared as virtual or abstract.

What Is The Difference Between A Virtual Method And An Abstract Method?

A virtual method must have a body where as an abstract method should not have a body.

Can Fields Inside A Class Be Virtual?

No, Fields inside a class cannot be virtua. Only methods, properties, events and indexers can be virtual.

Subscribe to get more Posts :