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

How to get Temporary Folder path in c#?

With the help of GetTempPath() method of System.IO.Path class,we can get Temp folder path.

For Example:-

string temp_folder_path = String.Empty;

temp_folder_path = Path.GetTempPath();

What will happen,when we compile below C# code? string str; MessageBox.Show(str);

NOTE: This is objective type question, Please click question title for correct answer.
How to get the Day part from below String date? string date = "24/12/2013";

Using DateTime format,we can get day part from any given string date.We have to pass format in ToString method of DateTime class as dd.

For Example:-

string date_part = Convert.ToDateTime(date).ToString("dd");


Output would be :- 24.
How to get the Month part from below String date? string date = "24/12/2013";

Using DateTime format,we can get Month part from any given string date.We have to pass format in ToString method of DateTime class as MM.

For Example:-

string date_part = Convert.ToDateTime(date).ToString("MM");


Output would be :- 12
How to get the Year part from below String date? string date = "24/12/2013";

Using DateTime format,we can get Year part from any given string date.We have to pass format in ToString method of DateTime class as yyyy.

For Example:-

string year_part = Convert.ToDateTime(date).ToString("yyyy");


Output would be :- 2013.
How to convert below date into dd-MM-yyyy format? string date = "24/12/2013";

Using DateTime format,we can convert given date to any format.We have to pass format in ToString method of DateTime class as dd-MM-yyyy.

For Example:-

string dt = Convert.ToDateTime(date).ToString("dd-MM-yyyy");


Output would be :- 24-12-2013
What is an alternate way of converting string DateTime to any format other than Convert.ToDateTime() method?

We can also use DateTime.Parse method to convert string to date and also convert given date into any date format.

For Example:-

string dt = DateTime.Parse("24/12/2013").ToString("dd-MM-yyyy");


Output would be:- 24-12-2013
How to sort a Listview Items in Descending order?

With the help of Sorting property of ListView we can sort any listview items.There is SortOrder Enum of a Sorting property,which has Ascending,Descending and None values.By which we can sort our list-view in any order.

For Example:-

lst_view_fruit_names.Items.Add("Apple");


lst_view_fruit_names.Items.Add("Orange");

lst_view_fruit_names.Items.Add("Banana");


lst_view_fruit_names.Sorting = SortOrder.Descending;


Then,list-view has Orange,Banana and Apple.
How to Sort an Arraylist?

Arraylist has a Sort() method,when applied,then Arraylist Items will be Sorted in Ascending Order.

For Example:-

arr_list_names.Sort();

Where arr_list_names is our ArrayList.which contains items like Vishal,Neeraj,Pawan

After sorting,Arraylist will show item in ascending order as
Neeraj,Pawan,Vishal
How to Dispose an Object?

We can Dispose an object by doing Null or calling its Dispose() method.

For Example:-

DataSet ds = New DataSet(); //initialise a dataset object.

ds = null; //dispose

//or

ds.Dispose();//dispose

Where is the place to assign Read-only variables?

We can assign values to Read-only variables either in Variable declaration or in Constructor.

For Example:-

public class Person

{
public readonly string status_deactive = "DD";
public readonly string status_active;

public Person()
{
status_active = "AA";
}
}

Can we initialize or assign values to Read-only variables in any method other than Constructor?

No,we can not initialize or assign values to Read-only variables because they can only be used to initialize either in Variable Declaration or in Constructor only.

If we assign values in any method,then it will give us Compile-Time error as

"A readonly field cannot be assigned to (except in a constructor or a variable initializer)"
What will happen,if we compile below code? public readonly string status_deactive; public void initialize_readonly_value() { status_active = "DD"; }

It will not compile properly,because it will give below error

"A readonly field cannot be assigned to (except in a constructor or a variable initializer)"

We can only initialize readonly variables at the time of declaration or in class constructor.
What will happen if we compile below code? public const string status_active;

NOTE: This is objective type question, Please click question title for correct answer.
What is a Method Group Conversion in C#?

Since Version 2.0,C# simplifies the syntax that assigns a method to a delegate.This is called Method Group Conversion .
It allows you to simply assign the name of the method to a delegate without using a new operator or explicitly invoking the delegate's constructor.
For E.g
Suppose we have a method name ReplaceSpace(string str)
Now i have a delegate del in the same class then we assign a method to a delegate with the simple syntax-:
del d=new del(ReplaceSpace);
string temp="";
//and now i will call the method
temp=d("Hello this is an example);

Now with method group conversion i will not use the new operator.
del d=ReplaceSpace ;
string temp="";
temp=d("Hello this is an example)

where del is my delegate defined earlier.

and in case my method is in another class then
class cs=new class();
del d=cs.ReplaceSpace//
How to set the default focus in a Textbox?

We use Focus method of Textbox control to set focus i.e. cursor inside Textbox as

In form_load event we will write as:

txt_employee_first_name.Focus();

What are the new features of Dot Net 3.5?

Main new features of Dot Net 3.5 are:-
a)VS 2008 Multi-Targeting Support.
b)In Built AJAX support for ASP.NET.
c)LINQ Support.
d)JavaScript Debugging Support.
e)Nested Master Page Support.
f)LINQ Intellisense and Javascript Intellisense support.
g)Organize Imports or Usings support.
h)Intellisense Filtering support.
i)Visual Studio 2008 Split View.
j)Debugging .NET Framework Library Source Code support.
In which situation,we need to call GC.Collect() method explicitly?

When we are testing application performance versus memory availability you can call GC.Collect() method to remove objects explicitly.
How many catch statements can be associated with single try statement?

There can be a zero or more catch statement for each try statement.
So it has no limit to the number of catch statement per try statement.
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