Is there any type of template that can be used like the statement below:
BitSet<10> bitSet; //we can create bitset with n bits, here is 10
bool b = bitSet.get<3>(); //get value of bit 3rd.
And how can I define a class like this in C#?
Is there any type of template that can be used like the statement below:
BitSet<10> bitSet; //we can create bitset with n bits, here is 10
bool b = bitSet.get<3>(); //get value of bit 3rd.
And how can I define a class like this in C#?
You should use a simple class, with a Constructor parameter (10) and then just call any method that recall any bit. Templates are for datatypes afaik. Anyway, look at This answer to verify if thats what you are looking for (Constructor parameter with a template with a given type)
c# code is :
bool[] array = new bool[]{true,false,false,true,false,false};
System.Collections.BitArray bitArray = new System.Collections.BitArray(array);
Console.WriteLine(bitArray[3]);
Not exactly like what you want but you can do that kind of stuff by using Indexers
.
class BitSet
{
private bool[] _bits;
public BitSet(int length)
{
_bits = new bool[length];
}
public bool this[int index]
{
get
{
return _bits[index];
}
set
{
_bits[index] = value;
}
}
}
To use that class
BitSet bitset = new BitSet(10);
bitset[0] = true;
Console.WriteLine(bitset[0]);