1

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.

Brian Mains
  • 49,697
  • 35
  • 139
  • 249
rayanisran
  • 2,536
  • 12
  • 43
  • 59
  • The ToArray() method you are using is a Linq method. Linq is not available in .Net 2.0. Why would you want to convert to .Net 2.0? – Nick Zimmerman Nov 26 '11 at 18:45
  • My entire program is coded in .NET 2.0 because apparently some people are too lazy to upgrade to .NET 4.0. :( – rayanisran Nov 26 '11 at 18:51

3 Answers3

14

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[].

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
6

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

Oded
  • 463,167
  • 92
  • 837
  • 979
1

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.

CHowell
  • 75
  • 2
  • 11