-2

So I was wondering what would be the best way to create a class that can hold primitive data types? I want a class that basically can hold any data types, like when I use a constructor to create the class I can make it a float, double, or integer, unsigned, or signed. Also how would you add or subtract doubles and floats?

Also I am looking for broad answers, as in not specific to one programming language, except for c#.

edit: It is very hard to describe what I mean here. Basically what I want is some way that I can create my own type of primitive data type, given certain information. For example I could create a 7 byte unsigned int if I wanted and then add it to a unsigned float I created. Also I want these all to be the same class so that when the two classes are added I do not need to have a add method for every single type of class.

me me
  • 778
  • 2
  • 7
  • 18
  • What's wrong with `object`? – mellamokb Dec 31 '12 at 16:28
  • 2
    It's not at all clear what you mean, and there's not going to be any language-agnostic answer here - it will be *very* language-specific. – Jon Skeet Dec 31 '12 at 16:28
  • 1
    Any class that you create already has the ability to hold primitive data types so perhaps you can explain further what it is that you are trying to do. – PhoenixReborn Dec 31 '12 at 16:29
  • 1
    possible duplicate of [How to define generic type limit to primitive types?](http://stackoverflow.com/questions/805264/how-to-define-generic-type-limit-to-primitive-types) – Jon B Dec 31 '12 at 16:29
  • It sounds like you want to use generics limited to primitives. – Jon B Dec 31 '12 at 16:29
  • Despite the edit, I still don't understand what you want to do. Why can't you just deal with the primitives instead of having a class to wrap them? – Bobson Dec 31 '12 at 17:45

1 Answers1

2

I'm not 100% sure what you're asking, but I think what you're looking for is Generics

From that link:

// Declare the generic class.
public class GenericList<T>
{
    void Add(T input) { }
}
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int.
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string.
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass.
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}
Jordan Kaye
  • 2,837
  • 15
  • 15