Espace de commande C#

using System;
using Project=Test.Learn;//起别名
using static Test.MyClass;//直接使用类中的静态成员
using Test2;
using Test3;

namespace Nullable
{
    class Program
    {
        static void Main(string[] args)
        {
            //同时using Test和using Test2的时候,直接调用MyClass.Method();会出现类在不同命令空间的不明确引用
            //在同时using Test和using Test2的情况下,在调用MyClass.Method()时可以加上对应的命令空间,如Test.MyClass.Method()
            Project.MyClass.Method();
            Method();
            MyClass.Method();
            //使用using block时,没有释放释放资源,会出现栈溢出
            //参考:https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-7.0&redirectedfrom=MSDN
            using (People p1=new People(1,2),p2=new People(3,4))
            {
                (p1 + p2).Show();
            }
            Console.ReadKey();
        }
    }
}
namespace Test
{
    namespace Learn
    {
        class MyClass
        {
            public static void Method()
            {
                Console.WriteLine("Learn");
            }
        }
    }
    class MyClass
    {
        public static void Method()
        {
            Console.WriteLine("Test");
        }
    }
}
namespace Test2
{
    class MyClass
    {
        public static void Method()
        {
            Console.WriteLine("Test2");
        }
    }
}
namespace Test3
{
    class People:IDisposable
    {
        int a;
        int b;
        public People(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
        public void Show()
        {
            Console.WriteLine($"a:{a},b:{b}");
        }
        public static People operator+(People p1,People p2)
        {
            People p = new People(0,0);
            p.a = p1.a + p2.a;
            p.b = p1.b + p2.b;
            return p;
        }
        private bool disposed = false;
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // 在这里释放托管资源
                }
                // 在这里释放非托管资源
                disposed = true;
            }
        }
    }
}

おすすめ

転載: blog.csdn.net/falsedewuxin/article/details/131518680