How to include several classes in one project with one namespace. How to use them?

I'm new to C #, so I'm struggling with basic concepts.

I created several classes in one project under one namespace, in which one class has Main () and other classes do not. Now I want to use other classes inside the same class.

I don’t understand how I can import all classes inside one class and call their functions, creating objects inside the Main () class.

As in Java, we used import packagename.ClassName;

In C # how to do the same?

Suppose I have one project named UseOfMultipleClasses. It has one class file called Program.cs, which has Main (). Now I have created 2 more classes. Add.cs has the addnum () function, and subtract.cs has subnum (). I want to name these two functions inside the Main () of the Program class. How to do it?

+4
source share
2 answers

this value using.

Please check this out.

Using "use" in C #

this if your class is in a different namespace and then refers to that namespace that you are using.

But, if you created classes in the same namespace, you can directly call them, because the C # compiler will bind all the classes of the same namespace.

.

+4

, .

public class main()
{
ClassA objCclassa=new ClassA();
ClassB objClassb=new ClassB();
classa.Get;
classa.Display;
classb.Get;
classb.Display;
} 

public class ClassA
{
     public void Get()
     {
       //Some Code
     }
     public void Display()
     {
       //Some Code
     }
}
public class ClassB
{
      public void Get()
      {
        //Some Code
      }
      public void Display()
      {
        //Some Code
      }
}
0

Source: https://habr.com/ru/post/1665481/


All Articles