Using Script# code with string operations in standart C# .NET project

后端 未结 2 1111
一个人的身影
一个人的身影 2021-01-20 10:34

I added reference of Script# to my standart console application. After that I was trying to invoke some method from there but was getting the following error:

2条回答
  •  深忆病人
    2021-01-20 11:19

    EDIT: The only solution to me seems to be the extension methods. You can define extension method Split on top of Script#'s String type similar to the standard .NET String.Split, i.e. accepting params char[]. And only then you can use the same code.

    string[] lines = s.Split(new char[] { ';' });
    

    Alternatively, define extension on top of standard String type, in case Script# is strict on extensions. But I think should not be a problem since extension method is just a static method.

    OLD ANSWER: After realizing that Script# does not accept params char[], below answer became incorrect:

    You are invoking s.Split by passing char to it. However, this method accepts params char[]. Although C# compiler allows you to pass char to Split, it later compiles it into passing char[]. So, if you want to have the same code working for both Script# and C#, change your code to the following way:

    string[] lines = s.Split(new char[] { ';' });
    

    This will work for both Script# and C#.

提交回复
热议问题