0

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#?

Jens
  • 20,533
  • 11
  • 60
  • 86
van con Nguyen
  • 101
  • 1
  • 5
  • 1
    There are no templates in C#, but only generics. They are TOTALLY different from C++ templates. Complete different beast from another universe. They just happen to look quite similar (actually not). – Diligent Key Presser Apr 16 '17 at 06:57

3 Answers3

1

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)

Community
  • 1
  • 1
1

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]);
mehdi farhadi
  • 1,415
  • 1
  • 10
  • 16
1

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]);