2

I'm using an overload of String.Split that the docs says is a valid overload (string, then StringSplitOptions).

string separatingString = "<<";
string text = "one<<two......three<four";
string[] words = text.Split(separatingString, System.StringSplitOptions.None);

foreach (var word in words)
{
    System.Console.WriteLine(word);
}

When I compile this directly with csc.exe (Visual C# Compiler version 4.2.0-4.22252.24) I get errors:

split_strings_issue_2.cs(3,29): error CS1503: Argument 1: cannot convert from 'string' to 'char'

split_strings_issue_2.cs(3,47): error CS1503: Argument 2: cannot convert from 'System.StringSplitOptions' to 'char'

Why won't the compiler accept a string in String.Split()? Why do the docs state that it should?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
user32882
  • 5,094
  • 5
  • 43
  • 82
  • 2
    What version of .NET are you using? – Kirk Woll May 13 '22 at 14:33
  • @KirkWoll how can I determine that? – user32882 May 13 '22 at 14:37
  • 5
    Are you on .Net Framework? Because this is only available on: _"Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 Preview 3 "_ and _"Standard 2.1"_ => [Split(String, StringSplitOptions)](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=net-6.0#system-string-split(system-string-system-stringsplitoptions)) – Fildor May 13 '22 at 14:37
  • 3
    @user32882 you can look at your project properties or the `.csproj` file directly. But it's something you should always know about your project -- it's pretty important! :) – Kirk Woll May 13 '22 at 14:38
  • @KirkWoll I'm compiling with `csc`, but based on this https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#use-powershell-to-check-for-a-minimum-version I'm using a version more recent than 4.6.2 – user32882 May 13 '22 at 14:39
  • Even compiling with csc, you still should have a .csproj, no? – Fildor May 13 '22 at 14:40
  • @Fildor, not if I'm directly doing `csc test.cs` from MSVC Developer prompt – user32882 May 13 '22 at 14:41
  • 1
    What is the first line outputted from csc if you just type it without any arguments? Something like `Microsoft (R) Visual C# Compiler version 4.8.4084.0` perhaps? – Kirk Woll May 13 '22 at 14:41
  • Erm ok. Just curious: Why? – Fildor May 13 '22 at 14:41
  • @KirkWoll it shows `Visual C# Compiler version 4.2.0-4.22252.24` – user32882 May 13 '22 at 14:43
  • 2
    Whelp, that's your problem then. That's a very old version of the .NET Framework, which doesn't support this overload. – Kirk Woll May 13 '22 at 14:43
  • @Kirk Woll That's strange, I installed Visual Studio 2022 community edition., so I was expecting that to provide me with the latest and greatest... – user32882 May 13 '22 at 14:45
  • Did you have anything dotnettish installed previously? Like VS 2017 or something like that? – Fildor May 13 '22 at 14:46
  • 3
    Shouldn't csc not be used anymore anyways? https://stackoverflow.com/a/31698761/1043380 – gunr2171 May 13 '22 at 14:47
  • _"Shouldn't csc not be used anymore"_ true, but this still doesn't explain the ancient version of it. – Fildor May 13 '22 at 14:49
  • @gunr2171 I was under the impression that `MSBuild` and `dotnet` both use `csc` under the hood anyway.... – user32882 May 13 '22 at 14:49
  • 1
    It probably just comes with VS (even newer versions) for backwards compatibility sake. I have csc 4.1.0 on my computer with VS 2022. MSBuild and dotnet probably _choose_ between csc and Roslyn depending on what's needed. – gunr2171 May 13 '22 at 14:51
  • Is there anything slightly lower level than `MSBuild` and `dotnet` that I could use? I just want to compile simple on-off executables from powershell without have the extra complexity needed for full-blown VS projects/solutions... – user32882 May 13 '22 at 14:54
  • and `dotnet build` doesn't do the trick for you? Ah, it needs at least a csproj, I guess. Checked: yep, does. – Fildor May 13 '22 at 14:54
  • @Fildor it adds a lot more than `csproj`, there's also `bin` and `obj` directories which hide parts of the compilation process... but maybe I'll give it a try anyway... it seems `csc` is outdated – user32882 May 13 '22 at 14:57

0 Answers0