Data Types Interview Questions C#.NET

Data Types Interview Questions and Answers C#.NET

In this article, I am going to discuss most frequently asked Data Types Interview Questions and Answers on C#.NET.

Is C# a strongly-typed language? 


What are the 2 broad classifications of data types available in C#?

  1. Built-in data types.
  2. User-defined data types.

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. 

Difference between int and Int32 in c#

This is a very basic and a common c# interview question

Int32 and int are synonymous, both of them allow us to create a 32-bit integer. int is shorthand notation (alias) for Int32. When declaring an integer in a c# program most of us prefer using int over Int32.  

Whether we use int or Int32 to create an integer, the behavior is identical. 

int i = 10;
Int32 j = 10;

Console.WriteLine("int i = " + i);
Console.WriteLine("Int32 j = " + j);
Console.WriteLine("int i + Int32 j =  " + (i + j));

I think the only place where Int32 is not allowed is when creating an enum. The following code will raise a compiler error stating – Type byte, sbyte, short, ushort, int, uint, long, or ulong expected.

enum Test : Int32
    abc = 1

The following code will compile just fine

enum Test: int
    abc = 1

I can think of only the following minor differences between int and Int32

  1. One of the differences is in readability. When we use Int32, we are being explicit about the size of the variable.
  2. To use Int32, either we need to use using System declaration or specify the fully qualified name (System.Int32) whereas with int it is not required.

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? 


Give examples for value types?

  1. Enum
  2. Struct

Give examples for reference types? 

  1. Class
  2. Delegate
  3. Array
  4. Interface
What are the differences between value types and reference types?

Value types are stored on the stack whereas reference types are stored on the managed heap.

Value type variables directly contain their values whereas reference variables hold only a reference to the location of the object that is created on the managed heap.

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 overhead of object allocation and garbage collection.

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.

My understanding is that just because structs inherit from System.ValueType, they cannot inherit from another class, because we cannot do multiple class inheritance.

Structs can inherit from System.ValueType class but cannot be inherited by any other types like Structs or Class. In other words, Structs are like Sealed Class that cannot be inherited.

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? 

Explicit conversions require a cast operator whereas an implicit conversion is done automatically.

Explicit conversion can lead to data loss whereas 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.

Will the following code compile? 

double d = 9999.11;

int i = d;

No, the above code will not compile. Double is a larger data type than the integer. An implicit conversion is not done automatically because there is a data loss. Hence we have to use explicit conversion as shown below.

double d = 9999.11;

int i = (int)d; //Cast double to int.

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 the 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 runtime 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?


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 whereas TryParse returns false and does not throw an exception if parsing fails. Hence TryParse is more efficient than Parse.

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 type 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. The 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 the performance impact.


In this article, I try to explain most frequently asked Data Types Interview Questions and Answers on C#.NET. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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