Home > database >  Trouble understanding calling function in c#
Trouble understanding calling function in c#

Time:10-01

Could anyone explain to me why calling this function does not work? It shows I have an error from ContainsDuplicate in main (ContainsDuplicate does not exist in current context).

    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;
        }

    

CodePudding user response:

You have created class which contains a function. Due to how classes work, in this scenario, you have to create new object of class solution like that:

var instance = new solution() ;

Then you can access this method through that object using . , like so:

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

This approach creates one method per object, so you have to have object around every time you want to use function.

Alternatively, you can make function static, which will make it accessible in all places in your code. You would change function signature to this:

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

And then you can use it like that:

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

Additionally you can remove class from around this method, making it public, the you can access it like you originally wanted, which is good for quick tests, but not recommended for use in actual applications.

CodePudding user response:

This should work

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

CodePudding user response:

You should mark your method static to call without creating an instance of the class. Then you should call the method from your class using the class name.

Changes:

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;
        }

CodePudding user response:

You are calling a non static function in static class. You can use:

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;
            }
        }
  • Related