Question

In .NET 3.5 or 4.0 I can use this (just an example):

var d = ("D").ToArray();

But the same doesn't work in 2.0 because there is no ToArray() method. How can I "convert" this code to .NET 2.0? I am using WinForms.

Was it helpful?

Solution

List<T>.ToArray() is a .NET 2.0 method.

OTHER TIPS

In your example you have a string so in order to get its characters as an array you could use the ToCharArray method:

char[] d = ("D").ToCharArray();

and the parenthesis are not necessary:

char[] d = "D".ToCharArray();

and if you have an array of strings, well you already have an array, so no ToArray is necessary.

And if you have a List<T> where T can be anything you still have the ToArray method which will return a T[].

That depends, you've got a couple of options here. See in .Net 4. The compiler and the precompiler(The part of the IDE which finds syntax error and other possible errors) do a lot of work to try and figure out what type that actual "var" is underneath the scenes, and just abstracts it away from the developer. However because in .NET 2.0 that functionality didn't exist yet, you've got to put a little more thought into what the type is actually going to be. To that end you've got a couple of options.

 char[] myArray = "s".ToCharArray();
 String [] arry = "s".Split(' ');

So you can have either an array of char's or an array of strings. But you have to put some thought into it before hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top