C# 变量赋值问题

发布于 2024-12-01 19:12:12 字数 600 浏览 1 评论 0原文

当我返回字符串 timeTaken 时,它是 null,并且它在 IDE 上这么说,尽管它已在 main 方法中定义 (TimeSpan timeTaken = timeTaken = timeTaken;)

class Program
{    
    public static string timeTaken;    

    static void Main(string[] args)
    {                
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(firstline);    
        Stopwatch timer = new Stopwatch();
        timer.Start();               
        using (var response = request.GetResponse());
        timer.Stop();    
        TimeSpan timeTaken = timer.Elapsed;
        ...
    }
}            

如何输出 timeTaken?

When I return the string timeTaken, it is null, and it says this on the IDE to, although it has been defined in the main method (TimeSpan timeTaken = timer.Elapsed;)

class Program
{    
    public static string timeTaken;    

    static void Main(string[] args)
    {                
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(firstline);    
        Stopwatch timer = new Stopwatch();
        timer.Start();               
        using (var response = request.GetResponse());
        timer.Stop();    
        TimeSpan timeTaken = timer.Elapsed;
        ...
    }
}            

How can I output timeTaken?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空城缀染半城烟沙 2024-12-08 19:12:12

您定义一个具有相同名称的局部变量

TimeSpan timeTaken

,该变量隐藏您的静态类字段。

要输出timer.Elapsed的值,你可以这样写:

Console.WriteLine("{0}", timer.Elapsed);

You define a local variable with the same name

TimeSpan timeTaken

which hides your static class field.

To output the value of timer.Elapsed you could write something like this:

Console.WriteLine("{0}", timer.Elapsed);
彩虹直至黑白 2024-12-08 19:12:12

有两个 timeTaken 变量,一个是 Main 函数的本地变量,另一个是类的静态成员。要显式引用该字符串,请使用 Program.timeTaken。无论如何,如果您将代码重构为具有不同的名称,那就更好了。

There is two timeTaken variable, one local to rthe Main function, the other one is a static member of the class. To explicitly refer to the string one use Program.timeTaken. Anyway is better if you refactor the code to have different names.

梦里梦着梦中梦 2024-12-08 19:12:12

您可能想要更像这样的东西:-

class Program
{
  public static string timeTaken;
  static void Main(string[] args)
  {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(firstline);
    System.Diagnostics.Stopwatch timer = new Stopwatch();
    timer.Start();
    using (var response = request.GetResponse())
    timer.Stop();
    timeTaken = timer.Elapsed.ToString();
  }
}

You probably want something more like this:-

class Program
{
  public static string timeTaken;
  static void Main(string[] args)
  {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(firstline);
    System.Diagnostics.Stopwatch timer = new Stopwatch();
    timer.Start();
    using (var response = request.GetResponse())
    timer.Stop();
    timeTaken = timer.Elapsed.ToString();
  }
}
如此安好 2024-12-08 19:12:12

您犯了一个简单的错误:您在 static void Main(...) 行中再次定义了变量“timeTaken”,

TimeSpan timeTaken = timer.Elapsed;

这将隐藏静态定义。要回到静态类字段,请

Program.timeTaken = ...

考虑命名(例如将静态字段命名为 _timeTaken
或者只是使用

timeTaken = timer.Elapsed;

而不是

TimeSpan timeTaken = timer.Elapsed;

You made a simple mistake: you define the variable "timeTaken" once again inside static void Main(...) on the line

TimeSpan timeTaken = timer.Elapsed;

This will shadow the static definition. To get back to the static class field use

Program.timeTaken = ...

think about naming (for example name your static field _timeTaken
or just use

timeTaken = timer.Elapsed;

instead of

TimeSpan timeTaken = timer.Elapsed;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文