“string”不包含“isNullorWhiteSpace”的定义

ted*_*one -2 c# methods

我不确定如何使用 isNullorWhiteSpace 将我自己的方法合并到代码中,我的框架不是 4.0。我之前得到过一些帮助,他们建议使用isnullorwhitespace,这不是最优选的显示方法吗:

2014 年 2 月 20 日上午 7:33:10,测量速度:0.2225

我似乎找不到可以工作的等效代码。

 using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Collections;

    namespace conApp
    {
        class Program
        {
            public static class StringExtensions
            {
                public static bool IsNullOrWhiteSpace(string value)
                {
                    if (value != null)
                    {
                        for (int i = 0; i < value.Length; i++)
                        {
                            if (!char.IsWhiteSpace(value[i]))
                            {
                                return false;
                            }
                        }
                    }
                    return true;
                }
            }

            static void Main(string[] args)
            {
                String line;
                try
                {
                    using (StreamWriter sw = new StreamWriter("C:\\writetest\\writetest.txt"))
                    {
                        string mydirpath = "C:\\chat\\";

                        string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

                        foreach (string txtName in txtFileList)
                        {
                            System.IO.StreamReader sr = new System.IO.StreamReader(txtName);

                            while ((line = sr.ReadLine()) != null)
                            {
                                String spart = ".prt";
                                String sam = " AM";
                                String spm = " PM";
                                String sresult = "TEST RESULT: ";
                                String svelocity = "MEASURED VELOCITY: ";
                                String part = string.Empty;
                                String date = string.Empty;
                                String result = string.Empty;
                                String velocity = string.Empty;
                                // sw.WriteLine(line);

                                    if (line.Contains(sam) || line.Contains(spm))
                                    {
                                        date = line;
                                    }

                                    if (line.Contains(spart))
                                      {
                                           part = line;
                                      }

                                    if (line.Contains(sresult))
                                      {
                                          result = line;
                                      }

                                    if (line.Contains(svelocity))
                                      {
                                          velocity = line;
                                      }

                                  if (!String.IsNullOrWhiteSpace(date) && !String.IsNullOrWhiteSpace(velocity))
                                  {
                               bool isNullOrWhiteSpace = "foo bar".IsNullOrWhiteSpace(); //doesnt work here

                                      int I = 2;
                                      string[] x = new string[I];
                                      x[0] = date;
                                      x[1] = velocity;
                                      sw.WriteLine(x[0] + "," + x[1]);
                                  }



                            }

                        }
                    }
                }
                catch
                {

                }
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)

Den*_*s_E 5

首先,StringExtensions需要是顶级类,因此它不能位于另一个类中。其次,您需要通过在第一个参数中
添加关键字来将该方法变成扩展方法:this

public static bool IsNullOrWhiteSpace(this string value)
Run Code Online (Sandbox Code Playgroud)

所以就变成了:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value != null) {
            for (int i = 0; i < value.Length; i++) {
                if (!char.IsWhiteSpace(value[i])) {
                    return false;
                }
            }
        }
        return true;
    }
}

class Program
{
    ...
}
Run Code Online (Sandbox Code Playgroud)