Posted By

sureshdasari on 10/11/18


Tagged

csharp programming collections dotnet visualStudio tutlane


Versions (?)

C# Generic Constraints


 / Published in: C#
 

URL: https://www.tutlane.com/tutorial/csharp/csharp-generic-constraints

In case, if we want to restrict a generic class to accept only the particular type of placeholder, then we need to use Constraints.

  1. public class GenericClass<T> where T: class
  2.  
  3. {
  4.  
  5. public T msg;
  6.  
  7. public void genericMethod(T name, T location)
  8.  
  9. {
  10.  
  11. Console.WriteLine("{0}", msg);
  12.  
  13. Console.WriteLine("Name: {0}", name);
  14.  
  15. Console.WriteLine("Location: {0}", location);
  16.  
  17. }
  18.  
  19. }

Report this snippet  

You need to login to post a comment.