5

I am new in C# that's why I am struggling with basic concepts.

I have created multiple classes in one project under one namespace out which one class is having Main() and other classes don't have. Now I want to use other classes inside one class.

I am not getting it that how I can import all the classes inside one class and call their functions by creating objects inside Main() of a class.

Like in Java we used to do import packagename.ClassName;

In c# how to do the same?

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

Surabhi Pandey
  • 4,058
  • 4
  • 18
  • 25
  • 2
    You don't need to import what's in the current namespace. You only import other namespaces with the keyword `using`. – Zohar Peled Jan 02 '17 at 04:51
  • 1
    If they are all in the same project and they are all in the same namespace then you do not need anything. If they are in different namespaces, then you need to use `using` – CodingYoshi Jan 02 '17 at 04:52
  • Okay so how to call those functions addnum() and subnum() inside Main(). Directly we can create object of other classes under one namespace without importing them. – Surabhi Pandey Jan 02 '17 at 04:53
  • Okay Thank you everyone. It's little bit confusing in some of the concepts in Java and c#. – Surabhi Pandey Jan 02 '17 at 05:00

2 Answers2

5

it is by using.

Please check out this.

Uses of “using” in C#

this is if your class is in different namespace, then to refer that namespace you use this.

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

You shouldn't be getting any trouble while accessing those classes of same namespace.

Community
  • 1
  • 1
Prajwal
  • 3,930
  • 5
  • 24
  • 50
  • So in c# if we create many classes under one namespace we can directly call them from any class by just creating object of those classes and there is no need to import those classes. Right??? – Surabhi Pandey Jan 02 '17 at 04:56
1

You can create the objects of each class the you can use all the methods inside that class.

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