1

Possible Duplicate:
What does “T” mean in C#?
What does <T> denote in C#

I have searched about the question, but because I do not have any idea about that syntax, I could not search well enough. Well, after the short explanation my question is :

While I was looking to a particle effect library I meet this :

[Serializable]
public class DPSF<Particle, Vertex> : IDPSFParticleSystem
  where Particle : global::DPSF.DPSFParticle, new()
  where Vertex : struct, global::DPSF.IDPSFParticleVertex
{
  // constructors, methods, parameters here ...
}

What does this notation mean in blankets (<...>) and how it is used ?

Community
  • 1
  • 1
icaptan
  • 1,495
  • 1
  • 16
  • 36
  • 3
    http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx – BlueRaja - Danny Pflughoeft Apr 20 '12 at 14:24
  • possible duplicate of [What does "T" mean in C#?](http://stackoverflow.com/questions/400314/what-does-t-mean-in-c) and [What does denote in C#](http://stackoverflow.com/questions/9857180/what-does-t-denote-in-c-sharp) – BlueRaja - Danny Pflughoeft Apr 20 '12 at 14:25
  • thanks ! I'm reading the post and i'm curious why there is "where Particle : ... " is it a restriction ? I know what T means, but I have never seen it using with a class ! I know using T at function, but still I want to learn its concept using at classes – icaptan Apr 20 '12 at 14:26
  • 1
    @icaptan yes, they're formally called constraints. There is a section describing them in the article linked to by BlueRaja – heavyd Apr 20 '12 at 14:28
  • [The section](http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx#csharp_generics_topic4) mentioned by @heavyd – BlueRaja - Danny Pflughoeft Apr 20 '12 at 14:29

2 Answers2

2

They are generic types that you can pass into a class definition:

A simple example:

public class Test<T>
{
   public T yourVariable;  
}

Test<int> intClass = new Test();
Test<string> stringClass = new Test();

intClass.yourVariable // would be of type int
stringClass.yourVariable // would be of type string

Where T is the type you want (i.e. another class, a string, integer, anything generic)

where Particle : global::DPSF.DPSFParticle - means that the Particle object must inherit from DPSF.DPSFParticle

Darren
  • 68,902
  • 24
  • 138
  • 144
1

It means that DPSF class is a generic class. Particle and Vertex between <> are type parameters. It means that DPSF gets two types as parameters and serves as a generic class.

Take a look at here: MSDN: Generics

EDIT Where keyword allows you to restrict type parameters. where:class means parameter must be a class in order this generic class to work.

Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42