tags:

views:

1676

answers:

11

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. Did the behavior of strings in C# 3.0 change so that a string can be referred as a char[] array without explicitly converting it (the statement "ch = path[i - 1]")?

+1  A: 

What error are you getting?

System.String has had [] accessors since .NET v1.0

James Curran
A: 

According MSDN ( http://msdn.microsoft.com/en-us/library/362314fe(VS.71).aspx ) this was even possible in .net 1.1 you can of course have a

string myString = "Filip Ekberg";

And then access the first char by doing myString[0]

Filip Ekberg
A: 

The dash character you see in your code block is an em-dash not a minus symbol. They look similar but they're different. Wherever you cut and pasted the code from has changed it to an incorrect character.

char ch = path[i - 1];

is perfectly valid (so long as the - is a minus and not a dash)

BenAlabaster
A: 

Unexpected character '–' from line "char ch = path[i – 1];"

Oh, I guess it is the calculation of the index that is causing the error rather than the accessor.

Jim Anderson
+5  A: 

It is an invalid character '–'. Change '–' to '-'

Darin Dimitrov
Thanks darin! Didn't notice that, was completely looking at the wrong thing!
Jim Anderson
The language spec is written in MSWord, which converts minus signs to en-dashes. It looks better on print outs, but messes up compiles.
James Curran
A: 

On a sidenote, why would you split a path and a filename like that? There's a lot of very useful functions to do that for you in the Path class.

Use Path.GetFileName() for the filename, Path.GetDirectoryName() for the directory name.

Erik van Brakel
A: 

I see that now. If I calculate the index first, it works in VS2005.

Does the code work "as is" in VS2008 though? It is in the C# 3.0 spec as an example.

Jim Anderson
have you written the code in microsoft word and then copied over? otherwise, how do you get – instead of - ?
Johannes Schaub - litb
A: 

It works for me, but the code you pasted in your question has an 'm-dash' character (hex 96) instead of a minus sign (hex 2D) -- maybe it's a font issue?

Guy Starbuck
A: 

@darin

Thanks. You are correct. I copied the code from the Word document and didn't realize the character was not the minus sign. It looked fine, but that must be a different character. If I change it, it works fine. Thanks again!

Jim Anderson
A: 

@Erik van Brakel

It was just a sample to illustrate the use of out params. There is no suggestion that it is a useful function.

Jim Anderson
A: 

You might want to post the compiler error message. In any case, you've got a en-dash instead of a plain - in the path[i-1] statement.

Eamon Nerbonne