zoukankan      html  css  js  c++  java
  • using + .net 中的别名

    命名空间别名:

    如果你的类的名称恰巧和别人一样,但是两个类的命名空间不一样,那么该如何处理呢?

    namespace CompanyA.AssemblyA
    {
        public class Console
        {
            public static void DoA()
            {
                //just do nothing
            }
        }
    }

    namespace CompanyB.AssemblyB
    {
        public class Console
        {
            public static void DoB()
            {
                //just do nothing
            }
        }
    }

    如果要调用的话,代码可能会是下面这个样子:

    class Program
    {
        static void Main(string[] args)
        {
            global::System.Console.WriteLine("test");
            CompanyA.AssemblyA.Console.DoA();
            CompanyB.AssemblyB.Console.DoB();
        }
    }

    很明显,每次都要完整的写命名空间是一件很类的事情,如果你知道命名空间别名的话,你可以这样写:

    using SystemConsole = global::System;
    using CA = CompanyA.AssemblyA;
    using CB = CompanyB.AssemblyB;

    class Program
    {
        static void Main(string[] args)
        {
            SystemConsole.Console.WriteLine("test");
            CA.Console.DoA();
            CB.Console.DoB();
        }
    }

    类型别名:

    除了对命名空间别名的话,还可以对某个具体的类别名。

    using MyIntType = System.Int32;
    using MyDoubleType = System.Double;
    namespace CAStudy
    {
        class AppStart
        {
            public static void Main()
            {
                MyIntType intType = 10;
                Console.WriteLine(intType);

                MyDoubleType doubletype = 10.0;
                Console.WriteLine(doubletype);

                Console.ReadLine();
            }
        }
    }

    使用了类型别名后,使用System.Int32作为参数的类型也变成了MyIntType,同样返回值也会发生改变。

    image

    image

    作者:LoveJenny
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    java中判断字符是否为英文字母、中文汉字或者数字
    JavaScript:多种定义数组的方式
    java位移运算符<<、>>、>>>
    安卓进阶:元注解Support Annotation Library使用详解
    正则:匹配引号内的字符串内容
    安卓:自定义字体
    安卓:Activity的自定义主题
    Pyhton学习——Day38
    测试用html
    Pyhton学习——Day37
  • 原文地址:https://www.cnblogs.com/LoveJenny/p/2276324.html
Copyright © 2011-2022 走看看