Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# importing class as *

After building my first big project using C# i ran into a problem. My project file is about 7000 lines big atm and fillde with functions and other classes. This is ofcourse a little messy and to find the code that i actually need i sometimes need to scroll around quite a bit so i want to remove all the classes and functions to a different file. I know i can just add a c# class file and then access it by doing

    namespace Namespace
{
    class Functions
    {
        // Example Functions here
    }
}

    namespace OtherNamespace
{
    class OtherClass
    {
        Namespace.Functions.Examplefunction
    }
}

If the example function would actually exist that is. But i would like to get rid of the Namespace.Functions part as it would save me alot of typing and i use these functions often. How would i do that ? Is it even possible?

I know it is achievable in python like this

import math as *

for example, then you wouldnt have to write

math.cos(0)

but instead you can just write

cos(0)
like image 417
cooldanietje Avatar asked Feb 29 '20 20:02

cooldanietje


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

You can use a static import feature of C# if you want to use many static helper functions. I've provided a basic example. You can use this for standard .NET framework classes such as System.Math, System.Console etc.

namespace Utils 
{
    // using static
    using static Utils.Helper;

    public class Program
    {
        public static void Main()
        {
            // no need to type Helper.Pow(2, 2);
            var x = Pow(2, 2);

            Console.WriteLine(x);
        }
    }
}

namespace Utils
{
    public static class Helper
    {
        public static double Pow(int x, int pow) => Math.Pow(x, pow);
    }
}
like image 133
Patrick Magee Avatar answered Sep 19 '22 13:09

Patrick Magee