Reputation: 1917
Please note that I am not talking about Generic Type parameters.
For example in Ada I can write a package that needs to be initialized using a value instead of a type. e.g.
generic
Size : Positive;
package Sudokus is
subtype Values is Positive range 1..Size*Size;
type Choice is private;
type Sudoku is private;
procedure Fill(S : out Sudoku);
procedure Init(S : out Sudoku);
procedure Solve(S : in out Sudoku);
procedure Print(S : Sudoku);
Unsolvable_Grid_Error : exception;
And this is how to use it :
package My_Sudoku is new Sudokus(3); -- 3 for 9x9 solver, 4 for 16x16 solver, etc.
I guess there is no equivalent but I find it quite useful. Is there a reason for this absence ?
Upvotes: 2
Views: 855
Reputation: 29530
Why don't you simply pass the size to the constructor and initialize the array with it?
class Sudokus
{
char[] field;
public Sudokus (int size){ field = new char[size*size]; }
}
The only advantage I could see with a "generic parameter" is if you had a fixed size array, which could allow for faster access, but that isn't supported in C# anyway (at least not as non-local variable).
I know this kind of templating is supported in C++, which templating is far superior to templating in C# or Java.
Upvotes: 1
Reputation: 1503290
The general answer to "why does C# not have feature X" revolves around the benefits of such a feature versus the costs. Benefits are usually obvious, but costs include:
Basically, it shouldn't be a case of asking why a particular feature isn't present: it should be a matter of arguing that the benefits of the feature are enormous compared with the costs. Features have to earn their place in the language, and the language designers have set the bar pretty high. (As Anders has put it in the past, every feature starts out with a score of -100, and has to work its way up.)
Upvotes: 3