Top 20 beginner level C# interview questions
- What is C#?
C# is a simple, modern, type-safe, managed and object oriented language, which is compiled by .Net framework for generating intermediate language (IL).
It is designed to be a platform-independent language although it is implemented primarily on Windows.
Its syntax is similar to C and C++ syntax.
C# does not support multiple inheritance.
You can user c# to develop any type of application by using .NET we require one .NET language to write the business logic of that application.
Several characteristics of C# are:
- Simple
- Type safe
- Flexible
- Object oriented
- Compatible
- Consistent
- Interoperable
- Modern
You can use Visual Studio Express (VCE), Visual Studio (VS), Visual Web Developer IDE’s.
- What are primitive data types in C#?
In C#, According to the type of the data and size of the data, data types are classified into five types. They are:
- Numerical Data types
- Signed numerical data types: sbyte, short, int, long
- Unsigned Numerical data types: byte, ushort, uint, ulong
- Floating float, double, decimal
- Character related Data types
- Char
- Logical Data Types
- Bool
- General data types
- String
- Object
The most famous primitive data types are: int, string, object, short, char, float, double, char, bool. They are called primitive because they are the main built-in types, and could be used to build other data types.
- What are the two types of data types in C#?
There are two types of data type on C#: Value types and Reference types
Value Type:
A data type is a value type if it holds a data value within its own memory space. It means variables of these data types directly contain their values. They are derived from the class System.ValueType.
Example of value types: bool, byte, chae, decimal, double, enum, float, int, long, sbyte, short, strut, uint, ulong, ushort.
When you declare an int type, the system allocates memory to store the value.
In terms of memory allocation, Value types are stored in the Stack.
Reference type
A reference type doesn’t store the actual data directly. Instead, it stores the address where the value is being stored. In other words, a reference type contains a pointer to another memory location that holds the data. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.
Example of built-in reference types: object, string and dynamic.
In terms of memory allocation, Reference type are stored in the Heap.
- Can we assign null value into value type variable?
No, but we can assign null values into reference type variable.
- How does C# differ from C++?
C# is a high level language that is component oriented while C++ is a low level and indeed platform neutral programming language.
- C# does not support #include statement. It uses only using statement.
- In C#, class definition does not use a semicolon at the end.
- C# does not support multiple inheritance.
- Casting in C# is much safer than in C++.
- In C# switch can also be used on string values.
- In C#, memory management is automatically handled by garbage collector. But in C++, the memory that is allocated in the heap dynamically has to be explicitly deleted.
- In C#, pointers can be used only in unsafe mode.
- Define namespace?
What are the namespaces in C#.NET?
Namespace is a logical grouping of classes. The namespace is a container which will be used to organize the hierarchal set of .Net classes.
Namespace is designed for providing a way to keep one set of class names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.
Example:
Using System;
Using System.Collection.Generic;
Using System.Windows.Forms;
- What is the use of using statement in C#?
In C#, we use “using” keyword for two different purpose.
- “Using” keyword is used to include a namespace in the program. A program generally has multiple using statements.
- The using statement is used to obtain a resource, execute a statement, and then dispose of that resource.
Example:
using (SqlConnection dbConn = new SqlConnection(conn)){ // Your code} // automatically does the .Dispose call as if it was in a finally block
- What is mean by operators in C#?
Operators, in C#, are symbols used within an expression or statement to specify the operations to be performed during evaluation of the expression. Operators are program elements that can be applied to one or more operands in an expression to perform computations. The operands used with the operator can be literals, fields, local variables and expressions.
There are three kinds of operators:
Unary operators,
Binary operators,
And conversion operators.
All operators must be declared as public and static.
- What is Jagged Array in C#?
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is also called an “array of arrays.”
You can initialize a jagged array as –
int [ ][ ] myArray = new int [2][ ] {new int [ ]{10,11,12}, new int [ ] {8,9,10,11}};
Where, myArray is an array of two arrays of integers – myArray [0] is an array of 3 integers and myArray[1] is an array of four integers.
- In how many ways you can pass parameters to a method?
There are three ways we can pass parameters to a method:
Value parameters – This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no side effect on the argument.
Reference parameters – This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.
Output parameters – This method helps in returning more than one value.
- Can you return multiple values from a function in C#?
Yes, by using output parameters, we can return multiple values from a function in C#. A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function.
- What is the difference between ref and out parameters?
Reference parameter copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.
Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it.
- Which class act as a base class for all the data types in .net?
The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. The object types can be assigned values of any other types, value types, reference types. Predefined or user-defined types. However, before assigning values, it needs type conversion.
- What is boxing and Unboxing in C#?
Boxing: is the process of converting from VALUE type to REFERENCE type.
Ex: converting from int to object
Unboxing: is the process of converting from REFERENCE type to VALUE type.
Ex: converting from object to int
- What are dynamic type variables in C#?
You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at runtime.
Syntax:
dynamic <variable_name> = value;
Example:
dynamic d = 20;
- What is the difference between dynamic type variables and object type variables?
Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas for the dynamic variables type checking take place at run time.
- What are nullable types in C#?
C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.
- What is the use of NULL Coalescing Operator (??) in C#?
The Null coalescing operator is used with the nullable value types and reference types. It is a binary operator that simplifies checking for null values.
If the value of the first operand is null, then the operator returns the value of second operand, otherwise it returns the value of the first operand.
Example: x ?? y
if x is null, it will return y else x
- What is the purpose of “is” operator in C#?
“is” operator determines whether an object is of certain type.
Ex-
If (David is Employee) // checks if David is an object of the Employee class.
- What is the purpose of “as” operator in C#?
“as” operator casts without raising an exception if the cast fails.
Example:
Object message = new StringReader (“desibanjara”);
StringReader newMessage = message as StringReader;
[amazon_link asins=’9351190900,1787287041,007070368X,8120352068,9352136640,8120349172,9386873583,9351343189,B016Z18MLG|1840787198,B016Z18MLG,0985580135,1119458684,1484230175,1119428114,1491987650,1491988533,1119449278′ template=’ProductGrid’ store=’desibanjara22-21|desibanjaraco-21′ marketplace=’IN|UK’ link_id=’c651b5ad-93d9-11e8-b332-e523a69d9d98′]