String as char [] array in C # 3.0?
The C # 3.0 spec has the following code example in section 10.6.1.3 Output Parameters:
using System;
class Test
{
static void SplitPath(string path, out string dir, out string name) {
int i = path.Length;
while (i > 0) {
char ch = path[i – 1];
if (ch == '\\' || ch == '/' || ch == ':') break;
i--;
}
dir = path.Substring(0, i);
name = path.Substring(i);
}
static void Main() {
string dir, name;
SplitPath("c:\\Windows\\System\\hello.txt", out dir, out name);
Console.WriteLine(dir);
Console.WriteLine(name);
}
}
I cannot get this code to compile in VS2005 / C # 2.0. Has the behavior of strings been changed in C # 3.0 so that a string can be cast to a char [] array without explicitly converting it (the "ch = path [i-1]" statement)?
+1
source to share
6 answers
According to MSDN ( http://msdn.microsoft.com/en-us/library/362314fe(VS.71).aspx ) this was possible even in .net 1.1, you can of course have
string myString = "Filip Ekberg";
And then enter the first char by doing myString [0]
0
source to share