0

In Lua and Javascript you can put different datatypes in an array. Bools; Strings; Ints and such. But I see that in C#, the arrays look something like

 string[] keysPressed ={};

So... Can I not put different datatypes in an array? Yes I know its obvious that you cant in that line. But is there like some other way I can create an array that supports different things?

4
  • 3
    object[] anythingYouWant = new object[] { "Hello", 42, new Dictionary<string,int>() }; - which is what LUA (or JavaScript, etc.) is effectively doing, by not having static typing. However, this throws away static type information and usually makes it more difficult to consume the data. To solve this in the C# (or, statically typed way) requires going back and re-examining the goal: and then solving it as suited for that language, not another one. (There is also dynamic in C#, but I hate to "suggest" that as a core approach/concept.) Commented Dec 30, 2015 at 5:14
  • 2
    Using object[] 'works' because every value in C# "is an inhabitant of the Object type" or "can be treated-as an Object". Commented Dec 30, 2015 at 5:21
  • 2
    I agree with user2864740 (props for having that username and making it to 32k). Doing this in C# is like speaking English with German word order. It would like this sound, and it would no sense make because all the words in a different order be would. You should use static typing as much as possible when using C#. It's not dynamically typed like Javascript. Commented Dec 30, 2015 at 5:24
  • take a look at my answer. dynamic solves all of the scenarios from JS and lua. I have also tested it with ruby and python objects.msdn.microsoft.com/en-us/library/dd264736.aspx Commented Dec 30, 2015 at 5:31

5 Answers 5

4

You are looking for an array or collection of dynamic. For more MSDN Dynamic data type

Dynamic can be used as we do in lua and JS. These are dynamically typed languages.

dynamic d1 = 7;
dynamic d2 = "a string";
dynamic d3 = System.DateTime.Today;
dynamic d4 = System.Diagnostics.Process.GetProcesses();

Here is an example of using them in array

dynamic[] myObjects = new dynamic[3];
myObjects[0] = 1;
myObjects[1] = "2";
myObjects[3] = "another string";
Sign up to request clarification or add additional context in comments.

2 Comments

This answer does not show anything "dynamic"-worthy. To show dynamic would require something like dynamicArr[3].Trim(), but that could be done even with an object[], as: ((dynamic)objectArr[3]).Trim() - and I find it unlikely that either should be used here. While dynamic is useful in a few scenarios, this should be far from normal usage in C#. Also, the first part would be much better written with var, which maintains static typing. Thus, I feel this answer is misleading at best..
you are absolutely right. In Js, lua, ruby or python etc. we can assign object of one type to another type at run time. The use of object is preferred if we are dealing with compile time behaviors but dynamic handles broader scenarios of with its DLR. It is obviously an overkill for types such as int, string, float etc. but i have used dynamic here to abstract the boxing/unboxing of complex types and classes.
0

You can also use an object array.

object[] keysPressed ={};

Comments

0

You can use object[] or any other collection of objects:

List<object> objs = new List<object>();

objs.Add(1);
objs.Add("Str");
objs.Add(DateTime.Now);

Remember that every item in this collection is an object, and you will need to cast them in order to use:

objs[1].Trim() // won't compile. object has no Trim method
((string)objs[1]).Trim() // OK
((string)objs[0]).Trim() // runtime exception, unable to cast

Another important thing that you need to keep in mind is that adding primitives to this collection results in boxing.

Comments

0
  1. object array, however,it need to be remember which type you store and cast it to the right type when you use the element. It's possible to throw an Exception in run time.
  2. Tuple
  3. generic collection

Comments

0

Why you don't use dynamic feature.

 List<dynamic> dynamicList = new List<dynamic>();
 string stringValue = "Akshay";
 int intValue = 1;
 dynamicList.Add(stringValue);
 dynamicList.Add(intValue);

You can either use the collection

 ArrayList array = new ArrayList();
 string stringValue = "Akshay";
 int intValue = 1;
 array.Add(stringValue);
 array.Add(intValue);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.