Reputation:
may hope, you all are well, I've some confusion about Constraint, i still don't get this, What's the benefit over using constraint?
I did understand tomorrow about Generics
. It's an awesome features using this method, i mean that makes simplest way of using this, No need to make again and again method with different data types, simply you could call it with any types of Data type,
especially i did understand all those terms with that examples,
using System;
class Test<T>
{
private T _value;
public Test(T t)
{
this._value = t;
}
public void print()
{
Console.WriteLine("This is {0}", this._value);
}
}
class MyProgram
{
private static void Main(string[] args)
{
Test<int> test = new Test<int>(145);
test.print();
Test<string> test2 = new Test<string>("My name is ghost!");
test2.print();
Test<double> test3 = new Test<double>(458.5456);
test3.print();
}
}
But When i go deeply inside Generics constraints
, i don't get this What it is used for!
Can you tell me with simplest example, What's the advantages over using this one?
I'm confused when i see this:
public class MyGenericClass<T> where T:IComparable { }
What is where T:IComparable{}
?? what IComparable is defined?
thanks~
Upvotes: 2
Views: 472
Reputation: 460
When you use
public class MyGenericClass<T> where T:IComparable { }
then T has to implement the Interface IComparable! This can be very useful for exaple when you whant to implement a sort algortithm! Of course you can also use other Interfaces with where!
Upvotes: 0
Reputation: 64477
Without constraints, the lowest common type of T
is object
. Pretty useless if you want to do something common with a common set of types. By allowing constraints, you can "drag up" the lowest common type to that which you specified as the constraint. It also then stops you from using the generic code if you type does not meet the constraints.
IComparable
will let your code call CompareTo
, for example.
There are some other built in constraints available that are not related to your type hierarchy:
where T : class
where T : struct
where T : new()
You can find out what these mean by reviewing the documentation.
IComparable
is simply a .NET framework interface available in the BCL; documentation here:
Defines a generalized type-specific comparison method that a value type or class implements to order or sort its instances.
And as Jeppe Stig Nielsen has mentioned, you should use the generic version of IComparable
, being IComparable<T>
, on your generic constraint for various other benefits.
Upvotes: 6
Reputation: 53958
The constraints are useful, because you demand the generic type you want to use to express some specific behavior, like implemenenting an interface as for your instance. This is a prerequisite for your case, because a tyope that implements this behaviour has some specific characteristics, that will be used in the generic class you declare.
Specifically, IComparable
Defines a generalized type-specific comparison method that a value type or class implements to order or sort its instances.
All the types that implement this interface, express the method called CompareTo
, where
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
The signature of the method is:
int CompareTo(Object obj)
and returns
That being said, in your case, where the generic type you defined have to implement this behaviour, is required because in the definition of your type, you will need this method, CompareTo
, which is implemented by types that implements the interface IComparable
.
In another case, you might need your type would be only a class
. Then the definition of your generic class would be:
public class MyGenericClass<T> where T: class
On the other hand, in another case, you might need your type be only a struct
. Then the definition of your type would be:
public class MyGenericClass<T> where T: struct
Upvotes: 0
Reputation:
When use simple generics without any constraints, you can assume about generic type anything.
Consider the follwowing code:
class Test<T>
{
public void Stuff()
{
T t;
t = new T(); // COMPILE TIME ERROR - type T may not have a parameterless constructor
}
}
With constraints:
class Test<T> where T : new()
{
public void Stuff()
{
T t;
t = new T(); // IT IS OK - the type T must have parameterless constructor.
}
}
Consider helper class:
class A
{
public A(int a)
{
}
}
When you will try to to compile var test = new Test<A>();
you will get compile time error (we are talking about Test with contraints of course) because type A
doesn't contain parameterless constructor.
Upvotes: 0