【问题标题】:Trouble understanding calling function in C#无法理解 C# 中的调用函数
【发布时间】:2021-11-22 19:18:07
【问题描述】:

谁能向我解释为什么调用这个函数不起作用?它表明我在 main 中遇到了 ContainsDuplicate 的错误(ContainsDuplicate 在当前上下文中不存在)。

    static void Main(string[] args)
    {
        var result = ContainsDuplicate(new int[] { 1, 2, 3, 4, 5, 1 });
        Console.WriteLine(result);
    }


    public class solution {
        public bool ContainsDuplicate(int[] nums)
        {
            var hash = new HashSet<int>();
            foreach (var i in nums)
            {
                if (hash.Add(i)) return true;

            }
            return false;
        }

    

【问题讨论】:

  • 我建议研究面向对象的“组合”——长话短说,您需要创建类的一个实例并调用该对象的方法。

标签: c# function boolean calling-convention


【解决方案1】:

您已经创建了包含函数的类。由于类的工作方式,在这种情况下,您必须像这样创建 solution 类的新对象:

var instance = new solution() ;

然后您可以使用. 通过该对象访问此方法,如下所示:

var result = instance.ContainsDuplicate(new [] {1, 2, 3, 4, 5, 1});

这种方法为每个对象创建一个方法,因此每次要使用函数时都必须有对象。

或者,您可以将函数设为静态,这样可以在代码中的所有位置访问它。您可以将函数签名更改为:

public static bool ContainsDuplicates(int[] nums)
{
....
}

然后你可以这样使用它:

var result = solution.ContainsDuplicates(new [] {1, 2, 3, 4, 5, 1});

此外,您可以从该方法周围删除类,将其设为公开,您可以按照最初的方式访问它,这有利于快速测试,但不建议在实际应用中使用。

【讨论】:

    【解决方案2】:

    这应该可以工作

    static void Main(string[] args)
    {
        var solu = new solution();
        var result = solu.ContainsDuplicate(new int[] { 1, 2, 3, 4, 5, 1 });
        Console.WriteLine(result);
    }
    

    【讨论】:

      【解决方案3】:

      您应该将您的方法标记为static,以便在不创建类实例的情况下进行调用。 然后你应该使用类名从你的类中调用方法。

      变化:

      var result = solution.ContainsDuplicate(new int[] { 1, 2, 3, 4, 5, 1 });

      public static bool ContainsDuplicate(int[] nums)

      static void Main(string[] args)
          {
              var result = solution.ContainsDuplicate(new int[] { 1, 2, 3, 4, 5, 1 });
              Console.WriteLine(result);
          }
      
      
          public class solution {
              public static bool ContainsDuplicate(int[] nums)
              {
                  var hash = new HashSet<int>();
                  foreach (var i in nums)
                  {
                      if (hash.Add(i)) return true;
      
                  }
                  return false;
              }
      

      【讨论】:

        【解决方案4】:

        您正在调用静态类中的非静态函数。您可以使用:

        var result =solution.ContainsDuplicate(new int[] { 1, 2, 3, 4, 5, 1 });
                    Console.WriteLine(result);
        
        public static class solution
                {
                    public static bool ContainsDuplicate(int[] nums)
                    {
                        var hash = new HashSet<int>();
                        foreach (var i in nums)
                        {
                            if (hash.Add(i)) return true;
        
                        }
                        return false;
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 2022-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-23
          • 2014-10-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多