Question

I think I finally get the point of constraints, but still a bit confused. Can someone tell me if the following is right?

Basically, if you inherit a class you may want to make sure that the class you inherit also inherits from some other class or some other interface.

It's confusing because presumably you would know that you would only want to inherit from a class that had the things you wanted but I guess with generics you can get a compile error at one point and not know that the problem actually stems from the fact that some class is not inherited somewhere else and you can get errors that make sense if you add constraints in the right areas.

Does that make sense? Am I on the right track?

Was it helpful?

Solution

I don't think that's the point of constraints, (but I may be wrong).

As I understand it, constraints don't have much to do with inheritance, per se. What you're really doing is putting constraints on the type that may be used if you are using (instantiating) a class that has type arguments.

Classes with type arguments are like Mad-Libs, and constraints are like the instructions that appear under the blanks:

"Bob like to ______ with his friends every night." (that's a madlib)

"Bob likes to _(verb)___ with his friends every night" (a madlib with instructions).

Check this:

//class with type arguments
public class MadLib<W> {
   public void addWord(W word) {
      System.Console.WriteLine("bob likes to " + word + " with his friends");
   }
}


//class with type arguments and contraints (note i'm not inheriting nothin)
public class MadLib<W> where W:Verb{
   public void addWord(W word) {
      System.Console.WriteLine("bob likes to " + word + " with his friends");
   }
}

OTHER TIPS

Not exactly. Constraints are used to specify what behaviors and/or types a generic parameter can exhibit.

For example, if you declare a class:

class GenericDisposableHandler<T>
{
    public void DoOperationOnT(T someVariable)
    {
       // can't really do much with T since its just an object type
    }
}

and know for a fact that the generic parameter T should always be something that implements IDisposable, you would do:

class GenericDisposableHandler<T> where T : IDiposable
{
    public void DoOperationOnT(T someVariable)
    {
        // Now you can treat T as IDisposable
        someVariable.Dispose();
    }
}

There are many other constraint types you can use.

Not really. Constraints are only applicable when you are defining a Generic class templare, (not when you inherit from a class).

When you define a generic, you are saying that this is a "template" or pattern, for some as yet undefined/unknown class with an as yet unknown Type, T, that will be created somewhere else based on this pattern and with an explicitly provided Type T. The constraint limits WHICH Types T can be used in the other place where you actually create the real type from the template/pattern

as in

 //Define the generic here, with arbitrary type T 
 public class MyClass<T> where T: SomeOtherClass
  {
    // template/pattern methods, proerpties, fields, etc.
  }

Create actual class here with explicitly provided Type "DerivedClass"

public MyClass<DerivedClass> myC = new MyClass<DerivedClass> ();

which will succeed if and only if DerivedClass inherits from SomeOtherClass

You can only inherit from one class, however you can implement several interfaces. The inheritance means that your class gets (inherits) whatever properties and methods that the base class specifies. As long as they're not marked as private, the inheriting class can call those methods as it's own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top