简体   繁体   English

C#无法隐式将类型'Char []'转换为'Char?'

[英]C# Cannot Implicity Convert type 'Char[]' to 'Char?'

I searched and searched, but couldn't find anything remotely similar. 我进行了搜索,但找不到任何远程相似的内容。 According to everything I've come to know, it should work. 根据我所知道的一切,它应该起作用。

  string strTest, strSubTest, strDesc;
        using (GenericParser parser = new GenericParser())
        {
            parser.SetDataSource(@"D:\work.csv");

            parser.ColumnDelimiter = @",".ToCharArray();
            parser.FirstRowHasHeader = true;
            parser.MaxBufferSize = 4096;
            parser.MaxRows = 200;

            while (parser.Read())
            {
                strTest = parser["Test"];
                strSubTest = parser["Subtest"];
                strDesc = parser["Description"];
                Console.WriteLine(strTest);

The code that states @",".ToCharArray(); 声明@“,”。ToCharArray();的代码 states the error in the title. 指出标题中的错误。 I've never seen an implicit conversion like that before 'Char?'. 我从未见过像'Char?'之前那样的隐式转换。 Any idea what I did wrong? 知道我做错了什么吗?

If you need some background, I used the GenericParser found here: http://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F 如果您需要一些背景知识,我可以使用在此处找到的GenericParser: http : //www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F

(请注意单引号)

parser.ColumnDelimiter = ',';

the question mark is a modifier to value types like char, int, and double to make them nullable. 问号是对诸如char,int和double的值类型的修饰符,以使它们可以为空。 So where char x = null; 因此,其中char x = null; is a syntax error, char? x = null; 是语法错误, char? x = null; char? x = null; works. 作品。 char? actually a shorthand syntax for Nullable<char> . 实际上是Nullable<char>的简写语法。 Nullable<T> can't be used with reference types, as they are already nullable. Nullable<T>不能与引用类型一起使用,因为它们已经可以为空。 Nullable<T> objects have two properties, bool HasValue , which should be obvious, and T Value , which returns the encapsulated value if HasValue is true, or throws an exception if it is false. Nullable<T>对象具有两个属性: bool HasValue (应该很明显)和T Value (如果HasValue为true则返回封装的值),或者如果它为false则引发异常。

So, you can't set Delimeter to a character array, because it is expecting either a character or null. 因此,您不能将Delimeter设置为字符数组,因为它期望使用字符或null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM