C# Interview Questions and Answers (958) - Page 16

How can we create an array with non-default value in C#?

Use the Repeat function of Enumerable class.

Enumerable class belongs to System.Linq namespace

int[] b = Enumerable.Repeat(45, 5).ToArray();

45: initial value

5: number of times the initial value should repeat.
foreach (var z in b)
{
Console.WriteLine(z);
}
What is Value Types versus Reference Types?

The Value Types which includes most built-in types such as numeric types, char types and bool types as well as the custom struct and enum types. The content of a Value Type variable is simply a value.

But in case of Reference Types, it includes all classes, array, interface, and delegate types. This is quite different from value type. It contains two parts that is an object and the reference to that object. The content of this type is a reference to an object that contains a value.

The basic difference between them is, how they are handled inside the memory.
Difference between var and dynamic?

var:
1)It is a keyword that is used for implicit variable declaration
ex: var a=100;
var b="welcome";
The compiler determines the datatype of the variable declared with var keyword
on the basis of assigned value. we have to assign value with variable declaration
ex: var a;
a="welcome"; This is wrong


2)var cannot be used to declare variables at class level.
3)var cannot be used as the return type of methods.
4)Intoduced in c# 3.0

dynamic:
1)It is a keyword used for variable declaration

ex: dynamic a=100;

dynamic b;
b="good";

The datatype of the variable is determined at runtime

2)dynamic can also be used to declare class level variables

ex: class aa
{
dynamic d; //correct

}

3)dynamic can also be used as return type of the methods

example:

class dd
{
static dynamic pp()
{
dyanmic d;
d=<some value>;
return d;
}
}

4)Introduced in c# 4.0
why can you prove that string is immutable datatype while StringBuilder is Mutable Datatype ?

Hi All

We can prove this by using Object.GetHashCode () Method provided in super class of all .net langues..

The hash code is a numeric value that is used to identify an object..

Please refer the below line of code..

using System;

using System.Text;

class Program
{
static void Main(string[] args)
{
string strtemp ="Prad";
Console.WriteLine(strtemp +" "+ strtemp.GetHashCode());

strtemp = strtemp + "eep";
Console.WriteLine(strtemp +" "+strtemp.GetHashCode());

StringBuilder sb = new StringBuilder("Prad");
Console.WriteLine(sb + " " + sb.GetHashCode());

sb.Append("eep");
Console.WriteLine(sb + " " + sb.GetHashCode());
}
}

/*output
Prad 432992021
Pradeep -673750172
Prad 46104728
Pradeep 46104728 */


As we can see for any operation performed on the string the new memory is created that y it is called immutable..

while for stringbuilder for all the operation the same memory is being modified.. as we can see the hashcode both time is same.

Happy coding :-)
An instance of which of the following must be created to read files across a network?

NOTE: This is objective type question, Please click question title for correct answer.
In what format does C# expect data to be streamed? 1. Simple binary 2. SOAP 3. Unserialized

NOTE: This is objective type question, Please click question title for correct answer.
How can you rename a file in C#?

Example:

if there is a filename known as aa.txt in C drive and we want to change its name

to bb.txt.

Use this code snippet:

File.Move("c:\\aa.txt","c:\\bb.txt");


You must include the namespace System.IO
Can we change the dimensions of the array like arr[4,5] dynamically ?

Yes, we can change both the dimensions of this array dynamically.
Code:
        int[,] arr = new int[4, 5];

Console.WriteLine(arr.GetLength(0) + "---" + arr.GetLength(1));
int b = 6;
arr = new int[b, b];
Console.WriteLine(arr.GetLength(0) + "---" + arr.GetLength(1));


When you run the program, the Getlengthmethod will show different number of dimensions.
How many catch Statement can be associated with single try statement?

It can be 0 to as many.
try must be followed by catch or finally.

ex:
1)0 catch statement
try
{

}
finally
{

}


2) any number
try
{

}

catch
{

}
catch
{

}
finally
{

}
Difference between typeof and Type.GetType

typeof and Type.GetType both help us to get Type Information using Reflection.

Differences:

typeof:

1)typeof expects only an existent typename.

2)Namespace reference is not needed.

3)typename is verified at the compile time itself

ex: Type t=typeof(Program);

Type.GetType

1))we have to specify a typename, which will be checked at runtime only.
if it does not exist, an exception will be generated.

2)Namespace is needed before the type name

ex: Type t=Type.GetType("System.String");
Will this code compile? try { } catch { string s="error"; throw s; }

No, it won't compile.

For throw, the type must be derived from the Exception class.
throw/catch keywords always expect an Exception instance.
example:
This would be Ok.
    

try
{

}
catch
{
Exception e=new Exception();
throw e;
}


Why Object Pool is required?

Basically this is a container of objects which reduces the overhead of creating objects each time. This can perform a certain task by allocating an object in the memory pool and can re-use it from memory pool according to the requirement.

Whenever the user will request to create an object, the pool manager will perform task for allocating an object from the pool which helps to minimize the memory consumption. This is used to enhance the performance and reduce the load of creation of new objects.
What is the correct syntax of region directive? a)#region #end region b) #region #endregion c)region endregion

NOTE: This is objective type question, Please click question title for correct answer.
Which of these statements will allow us to determine if Ctrl(Control) key was pressed? (e is a variable of KeyEventArgs class)

NOTE: This is objective type question, Please click question title for correct answer.
What is the escape sequence of null character?

NOTE: This is objective type question, Please click question title for correct answer.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories