Trying to call a namespace in another class in C#

Viewed 19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
    public class Namespaces
    { 
        public Namespaces()
        {

        }
    }

    namespace NameSpace1
    {
        public class MyClass
        {
            public static int add(int a, int b)
            {
                return a + b;
            }
        }
        namespace NameSpace2 
        {
            public class MyClass
            { public static int add (int a, int b, int c)
                    { return a + b + c; }
            }
        }
    }
}`
```
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        { 
            int sum1 = NameSpace1.MyClass.add(1, 2);
            int sum2 = NameSpace2.MyClass.add(1, 2, 3); //Attempting to call Namespace 2 from here


        }
    }

Previously, I tried rebuilding, checking for spelling mistakes or differences between my namespaces(didn't find any), checking for formatting mistakes or differences between the two original Namespace methods(the only one I could find really didn't make any difference).

1 Answers

NameSpace2 is within NameSpace1. Change it to int sum2 = NameSpace1.NameSpace2.MyClass.add(1, 2, 3); and it should work:

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main()
        {
            int sum1 = NameSpace1.MyClass.add(1, 2);
            int sum2 = NameSpace1.NameSpace2.MyClass.add(1, 2, 3);
        }
    }
}

(Fiddle).

Related