1. Give at least four class
names which are present in System.Collections.Generic namespace?
Ans:
1.
List<T>
2.
Stack<T>
3. Queue<T>
4.
SortedDictionary<T>
5.
SortedArray<T>
2. What is Generic
Delegates?
Ans: A delegate can define its own
type parameters. Code that references the generic delegate can specify the type
argument to create a closed constructed type, just like when instantiating a
generic class or calling a generic method, as shown in the following example:
public delegate void Del<T>(T item);
public static void Notify(int i) { }
Del<int> m1 = new Del<int>(Notify);
3. We can create a Generic
class, however, we cannot create a generic interface in C#.NET, Is it true?
Ans: No it false, We
can also create Generic Interfaces.
4. Generic procedures can
take maximum one generic parameter, Is it true?
Ans: No it is
false,Generic procedures must take at least one type Parameter.
We have a code snippet
like.....
public
class MyContainer<T> where T: IComparable
{
// Insert
code here
}
5. What is need of that
Icomparable in the above code?
Ans:Class
MyContainer requires that it's type argument must implement IComparable
interface.
|
6. Can we give multiple
constriant on type argument? and if yes give example or if no then why?
Ans:Yes we can give
multiple constraint on type argument.
Example:
public class MyContainer<T> where T: class, IComparable
{
//Insert
code here
}
here
Class MyContainer requires that its type argument must be a reference type and
it must implement IComparable
interface.
7. Can we initailize a Generic List with some initial capacity and how?
Ans:Yes we can initialize List
with some initial capacity by using the following constructor
8. What are the different
types of collections?
Ans: Collections are
basically group of records which can be treated as a one logical unit.
.NET Collections are divided in to four
important categories as follows.
1.
Indexed based.
2.
Key Value Pair.
3.
Prioritized Collection.
4.
Specialized Collection.
9. Explain about generics ?
Ans: Generics are not
a completely new construct; similar concepts exist with other languages.
Generics are defined with the CLR. This makes it possible to instantiate
generics with a specific type in Visual Basic even though the generic class was
defined with C#.
10.
Why hashtables are not
serialzable in .NET?
Ans: The thing about XML Serialization is that it's not just about creating a stream of bytes. It's also
about creating an XML Schema that this stream of
bytes would validate against. There's
no good way in XML Schema to represent a dictionary.
This limitation is not only Hashtable, the XmlSerializer cannot process classes
implementing the Idictionary interface due to the fact that a hashtable does not have a counterpart in the XSD type system. The only
solution is to implement a custom hashtable that
does not implement theIDictionary interface.
11.
What is a List of Generic Type?
Ans:Lists are indexed based Generic Collections.Lists are Generic
form of ArrayList.
For example,
List<int> ObjInt = new List<int>();
ObjInt.Add(123);
ObjInt.Add(100);
12.
What is Dictionary<TKey,
TValue>?
Ans: Dictionary are key based generics collection.Dictionary are
generic form of Hashtable.
For example:
Dictionary<int, int>
ObjDict = new Dictionary<int,int>();
ObjDict.Add(1,2);
Dictionary<int,string>
ObjDict1 = new Dictionary<int,string>();
ObjDict1.Add(1,"Prashant
is a Silent");
ObjDict1.Add(2,"Dhananjay
is Developer");
ObjDict1.Add(3,"Kundan is
Romantic");
13. What is Stack<T>?
Ans:Stack generic
collection allows you to get value in "LIFO"(last in first out)
manner.
For example,
Stack<string>
ObjStack = new Stack<string>();
ObjStack.Push("C#");
ObjStack.Push("ASP.NET");
ObjStack.Push("SQL SERVER");
Console.WriteLine(ObjStack.Pop());
14.
What is collectionsUtil?
Ans:It help us to Create collections that ignores the case in
String.
Example:
Hashtable ObjHash =
CollectionsUtil.CreateCaseInsensitiveHashtable();
ObjHash.Add("feroz","he
is a developer");
string str = (string)
ObjHash["FEROZ"];
MessageBox.Show(str);
15.
Which values are not used in
built in genric types?
Ans:Boolean, which is a non generic value types.
16.
How does Generics
enhance the performance?
Because it is type safe, due to which there is no need of Boxing
and Un-Boxing.
17. What is collection initializer?
Ans: Its a new feature
introduced in C#3.0 which gives us permission to initialize a collection at time of declaration.
Example:
List<int> liN = new
List<int>(){ 1 , 4 , 5 , 7 };
18.
Can we define a generic
method with multiple type?
Ans:Yes. We can define eg.
Public void
MyGenMethod<MyType>(MyType tp , int N)
{
Console.Write("{0}
\n {1}",tp,N);
}
static void Main()
{
MyGeMethod<int>(10,20);
MyGeMethod<double>(10.20,30);
MyGeMethod<string>("Nacre",40);
}
19.
Generics method or class
can take any type as type parameter, how can we restrict it to take only
specific type?
Ans:Constraints can restrict a generic to take only specific type
according to constriant imposed on Generic.
Example:
Class MyType<TP>where TP:struct
{
------
}
in this case this class can
take only value type as type parameter.
20.
What is Queue<T>?
Ans: Queue generic collection allows you to get value in
"FIFO"(first in first out) manner.
For example,
Queue<int> ObjStr = new Queue<int>();
ObjStr.Enqueue(786);
ObjStr.Enqueue(123);
Console.WriteLine(ObjStr.Dequeue());
21. What are
Constraints in Generics& what are Different Type Of Constraints?
Ans
Constraints are represented in C# using the where keyword Constraints
is something restriction the way doing that in generics
There are 3 types of Constraints
1.Constructor constraint
2.Derivation constraint
3.Reference / value type
constraints
22. What are
Generics and Casting?
The
C# compiler only lets you implicitly cast generic type parameters to Object, or
to constraint specified types.
The compiler
explicitly cast generic type parameters to only an interface but not a class.
23. How to declare
Generic Methods?
Ans Generic
method is defined by specifying the type parameter after the method name but
before the parameter list.
Example public
string Add<T>(T val1, T val2).
24. What is use of
Generic List?
Generic
List is an efficient method of storing a collection of variables
Generic
List is strongly typed and casting.
Name
space to declare generics is using System.Collections.Generic.
27. Difference
between Generics and Array List?
·Array List
is not type safe because it faces problems of boxing and UN boxing.
·List
generics are type safe and do not require boxing and UN boxing situations.
·In terms of
performance of application List generics is better than array list.
·Array List
can save different type data types.
·List
generics we can save only specific data type.
·Array List
consumes lots of memory compare to list generics/
28. What are
unbounded type parameters?
·Type
parameters that have no constraints, such as T in public class SampleClass<T>{}, are
called unbounded type parameters. Unbounded type parameters.
29.What are rules
of Unbounded type parameters?
Ans: The != and
== operators cannot be used because there is no guarantee that the concrete
type argument will support these operators.
·
They can be
converted to and from System.Object or explicitly converted to any interface
type.
You can compare to null. If an
unbounded parameter is compared to null, the comparison will
always return false if the type argument is a value type.
29.What Is IEnumerator?
Ans.IEnumerator is the
base interface for all non-generic enumerators.
30.Difference
between IEnumerator and IEnumerable?
Ans IEnumerable is an interface that defines one
method GetEnumerator which returns an IEnumeratorinterface, this in turn allows
readonly access to a collection. A collection that implements IEnumerable can
be used with a foreach statement.
31.Does Generics shift the
burden of type safety to the programmer rather than compiler?
Ans: False
Lightweight dynamic
methods can be Generic?
32.Does Generics provide type
safety without the overhead of multiple implementations?
Ans. True
33.Name Few Of the
Classes that belong to System.Collections.Generic?
Ans
SortedDictionary,stack,List,Queue,LinkedList.
34.Can We Use Generics
delegates in C#.NET?
Ans Generics delegates are
allowed in C#.NET but we can't impose constraints on it.
Ans. It Represents a
collection of objects that can be individually accessed by index.
36.What Are The
Properties Of Icollections<T>?
Ans. There Are Two Properties
Of Icollections<T>
1.ICount
2.IsReadOnly.
37.Only those
procedures labeled as Generic are generic.Is It True or False?
Ans.False
38.Differences
Between C++ Templates and C# Generics?
Ans C# generics do
not provide the same amount of flexibility as C++ templates. For example, it is
not possible to call arithmetic operators in a C# generic class, although it is
possible to call user defined operators.
39.How Information can be
obtained on the types used in a generic data type at run-time ?
Information on the types used
in a generic data type can be obtained at run-time by means of reflection.
40.What is the
most common use of generics?
Ans The most common
use of generics is to create collection classes.
41.What Is a Generic Type
Argument?
Ans.A generic type argument is the
type the client specifies to use instead of the type parameter. For example,
given this generic type definition and declaration:
public class MyClass<T>
{...}
MyClass<int> obj = new MyClass<int>();
42.What Is a Constructed Type?
Ans. A constructed type is any generic type that has at least one type
argument.
43.What Is an Open Constructed Type?
A open constructed type is any generic type that which contains at
least one type parameter used as a type argument.
44.What Can Define Generic Type
Parameters? What Types Can Be Generic?
Ans. Classes, interfaces, structures and delegates, can all be generic
types.
45.What Is a Generic Type Inference?
Generic type inference is the compiler's ability to infer which type
arguments to use with a generic method, without the developer having to specify
it explicitly.
46.Why Can I Not Use Enums, Structs, or Sealed Classes as
Generic Constraints?
You cannot constraint a generic type parameter to derive from a
non-derivable type. For example, the following does not compile:
public sealed class MySealedClass
{...}
public class MyClass<T> where T : MySealedClass //Does not compile
{...}
47.Is an Application That Uses Generics Faster than an
Application That Does Not?
Depending on the application of course, but generally speaking, in
most real-life applications, bottle necks such as I/O will mask out any
performance benefit from generics. The real benefit of generics is not
performance but rather type safety and productivity.
48.Are Generics CLS Compliant?
Yes. With the release of .NET 2.0, generics will become part of the
CLS.
49.Can I Use Generic Attributes?
You cannot define generic attributes:
No comments:
Post a Comment