C
C#15mo ago
titio-tk

❔ How to import different classes to main file.

hello, i have a question, i'm a beginner in C# and a try to split my program in different classes, now i making a simple calculator without graphic interface, and i can't split my program in 2 classes, one with basics operations and the main class with main method, my question is what i can import my basic operations class to my main class? ps: https://github.com/devv-thiago/calculadora_CMD
GitHub
GitHub - devv-thiago/calculadora_CMD
Contribute to devv-thiago/calculadora_CMD development by creating an account on GitHub.
2 Replies
Angius
Angius15mo ago
You don't import classes, you import namespaces And only if needed If two classes are in the same namespace you can just... use them
// Program.cs
namespace Foo;

public class Program
{
public static void Main()
{
var b = new Baz();
b.Print();
}
}
// Program.cs
namespace Foo;

public class Program
{
public static void Main()
{
var b = new Baz();
b.Print();
}
}
namespace Foo;

public class Baz
{
public void Print()
{
Console.WriteLine("henlo");
}
}
namespace Foo;

public class Baz
{
public void Print()
{
Console.WriteLine("henlo");
}
}
will just work, because same namespace If the namespace is different, then you need to use the using directive
// Program.cs
using Unga.Bunga;

namespace Foo;

public class Program
{
public static void Main()
{
var b = new Baz();
b.Print();
}
}
// Program.cs
using Unga.Bunga;

namespace Foo;

public class Program
{
public static void Main()
{
var b = new Baz();
b.Print();
}
}
namespace Unga.Bunga;

public class Baz
{
public void Print()
{
Console.WriteLine("henlo");
}
}
namespace Unga.Bunga;

public class Baz
{
public void Print()
{
Console.WriteLine("henlo");
}
}
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server