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

How to get values from hashtable?

We can access value with the help of Hashtable'Key.
We can differentiate Hashtable with keys and Values.

Suppose,i have one Hashtable named ht_courses as below:-

Hashtable ht_courses = new Hashtable();

ht_courses.add("course1","Dot Net");
ht_courses.add("course2","C#");
ht_courses.add("course3","VB.NET");
ht_courses.add("course4","ASP.NET");


Here course1,course2,course3 and course4 are the Keys and Dot Net,C#,VB.NET and ASP.NET are the Values.

//Now to access Value from Hashtable,we will write below code:

 if (ht_courses.Count > 0)

{
string course1_value = (string)ht_courses["course1"];
string course2_value = (string)ht_courses["course2"];
string course3_value = (string)ht_courses["course3"];
string course4_value = (string)ht_courses["course4"];
}


//Output will be for:-
//course1_value = Dot Net
//course2_value = C#
//course3_value = VB.NET
//course4_value = ASP.NET
How to check whether Dictionary contains key or not?

With the help of ContainsKey method,we can check whether dictionary has a key or not?

If(dict_names.ContainsKey["name"])

return true;
else
return false;

How to check whether Dictionary contains any value or not?

With the help of ContainsValue method,we can check whether dictionary has a value or not?


Dictionary<string, string> dict_names = new Dictionary<string, string>();
dict_names.Add("name", "rajesh");

If(dict_names.ContainsValue["rajesh"])
return true;
else
return false;


It will return true because rajesh is present inside dictionary.
How to add items into Dictionary?

Add method is used to add a item into dictionary.

For example:-

Dictionary<int,string> dict_names = new Dictionary<int,string>();

dict_names.Add("name","rajesh");
dict_names.Add("surname","sathua");

How to clear dictionary items?

Clear() method is used for removing all items from dictionary.

Dictionary<int,string> dict_names = new Dictionary<int,string>();

dict_names.Add("n]ame","rajesh");
dict_names.Add("surname","sathua");
//count = 2

dict_names.Clear();
//count = 0

How to check whether dictionary contains any items or not?

With the help of count method,we can check

dict_names.Count();

How to add items into List collection?

With the help of Add method,we can add items into List collections:-
We have to import using System.Collections.Generic Namespace.

For Example:-

List<string> lst_courses = new List<string>();

lst_courses.Add("C#");
lst_courses.Add("VB.Net");

What is the alternative of For Loop?

Foreach loop is an alternative of For Loop.
We get the same output with Foreach loop as with For Loop

For Example:-

foreach(string values in arr_list)

{
MessageBox.Show(values);
}


Where arr_list is an ArrayList.
What are the List collections methods?

NOTE: This is objective type question, Please click question title for correct answer.
What is the use of Exists method of Generic method in C#?

Exist returns true if a List item is found otherwise returns false.We have to match with predicate parameters.

For Example:-

List<int> lst = new List<int>();

lst.Add(1);
lst.Add(2);
bool exists = lst.Exists(value => value > 1);


It returns true because we have checked here value > 1,and List also contains 2.
What will be the output of below code? List<int> lst = new List<int>(); lst.Add(1); lst.Add(2); bool exists = lst.Exists(value => value > 2);

It will return false.Because as we can see list have 2 items as 1 and 2.And we are checking value > 2.So it will return false.Because there are no greater value other then 2.So the condition will always be false.
What will be the output of below code? List<int> lst = new List<int>(); lst.Add(1); lst.Add(2); bool exists = lst.Exists(value => value >= 2);

It will return True.Because as we can see list have 2 items as 1 and 2.And we are checking value >= 2(value is greater than or equal to 2).So the condition will always be True.It's same as we check with IF condition.As IF condition returns True or False.Exists also returns True or False.
What is the use of IndexOf method in List collection?

IndexOf method is same as String IndexOf method in DotNet.As we know that,IndexOf method is used for searching particular value or text from within string.So it performs same thing with the generic List.It searches List collections and determines the item index of a certain value in the List collection.By using the IndexOf method,we can search for the first position of the value specified in a List collection.
It returns 1 if it finds particular elements in a List otherwise -1 will be returned.

Syntax:-

lst.IndexOf("search any text");

What is the output of below code? List<int> lst = new List<int>(); lst.Add(1); lst.Add(2); int index = lst.IndexOf(1);

Output will be 0.As we know that IndexOf method returns 0 if it finds the suitable match or particular value.And here List have 2 items i.e. 1 and 2.So IndexOf will search 1 in list.Therefore it will return 0 because 1 is there in List.
What is the output of below code? List<int> lst = new List<int>(); lst.Add(1); lst.Add(2); int index = lst.IndexOf(5);

Output will be -1 .As we know that IndexOf method returns 0 if it finds the suitable match or particular value otherwise returns -1.And here List collections have 2 items i.e. 1 and 2.But 5 is not there in List.So IndexOf will search 5 inside List.Therefore it will return -1 because it will not find 5 inside List.
What are the classes available in System.Collections.Generic Namespace?

We have List,Dictionary,SortedList,SortedDictionary,
Stack,Queue and so many classes which are available in System.Collections.Generic Namespace.
How to extract numeric value from String?

We use Regex and its Match method to extract Number from String value.We have to create Regular Exp class object.and we have to pass string value to its Match method.We use \d used for extracting numeric value.

Regex regex = new Regex(@"\d+");

Match match = regex.Match("Rajesh Kumar Sathua Age 29");

if (match.Success)
{
MessageBox.Show(match.Value);
}

What is the Namespace used to deal with Regular Expression?

We have to import System.Text.RegularExpressions Namespace to work with Regular Expression.
What is the Namespace used in StringBuilder?

We use System.Text Namespace to work with StringBuilder.

StringBuilder sb = new StringBuilder();

sb.Append("Rajesh Kumar");
sb.Append(" Sathua");
string st = sb.ToString();


Output would be:Rajesh Kumar Sathua
How to extract only date part from date and time.

Suppose i have Date value as

19/01/2014 12:15:36

And i want only 19/01/2014. So i will use ToString method of DateTime object to convert date into specific format as

dt.ToString("dd/MM/yyyy")


Output would be:19/01/2014
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