Dart Generics : Generics of specified SubType

Dart Generics : Generics of specified SubType

·

1 min read

Generics of specified Subtype :

  • generics allow you to write reusable code that can work with different types while still maintaining type safety.

  • If you want to restrict the generic type to a specific subtype, you can use the extends keyword.

class Box<T extends num> {
  T value;

  Box(this.value);

  void printValue() {
    print('The value is: $value');
  }
}

void main() {

  var intBox = Box<int>(20);
  intBox.printValue(); 

}
The value is: 20

Exited.