zy44_命名空间和装箱拆箱


1.命名空间

namespace 组织区分作用,组织不同类(可以同名)进入不同命名空间

程序(1)示例:

using A;

namespace _44_命名空间
{
    internal class Program
    {
        static void Main(string[] args)
        {
            A.Bird f1 = new A.Bird();
            f1.Fly();
            B.Bird f2 = new B.Bird();
            f2.Fly();

        }
    }
}
//类库。
namespace A
{
    class Bird
    {
        public void Fly()
        {
            Console.WriteLine("A小鸟飞。");
        }
    }
}
namespace B
{
    class Bird
    {
        public void Fly()
        {
            Console.WriteLine("B小鸟飞。");
        }
    }
}

程序(2)示例:

using System;

namespace _44_命名空间2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            double a = 4;
            double b =System.Math.Sqrt(a);//可以在前面加上命名空间
            System.Console.WriteLine(b);
            string cc = Console.ReadLine();
            int d=Convert.ToInt32(b);
        }
    }
}

.NET中部分命名空间

命名空间

在命名空间中继续定义命名空间;

程序(3)示例:

using A;

namespace _44_命名空间
{
    internal class Program
    {
        static void Main(string[] args)
        {
            A.AA.Bird f1 = new A.AA.Bird();
            f1.Fly();
            B.Bird f2 = new B.Bird();
            f2.Fly();

        }
    }
}
//类库。
namespace A
{
    namespace AA
    {
        class Bird
        {
            public void Fly()
            {
                Console.WriteLine("A小鸟飞。");
            }
        }
    }
    namespace AB
    {

    }
    
}

2.帮助文档

在VS中选择:Help<View Help


3.装箱和拆箱

  • 装箱:装箱前n的值存储在栈中,装箱后被封装在堆中;

    程序(4)示例:

    namespace _44_装箱和拆箱
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                int n = 16;
                object obj = n;//装箱过程:值类型赋给引用类型
                Console.WriteLine(obj);
    
            }
        }
    }
    
    

  • 拆箱

    程序(5)示例:

    namespace _44_装箱和拆箱
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                int n = 16;
                object obj = n;//装箱过程:值类型赋给引用类型
                Console.WriteLine(obj);
                int i = (int)obj;//拆箱:obj型转换为值类型
                Console.WriteLine(i);
            }
        }
    }
    

    ** 装箱拆箱会带来性能损失**

    程序(6)示例:

    namespace _44_装箱和拆箱2
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Ct.Storehouse store = new Ct.Storehouse(5);
                store.Add(100);
                store.Add(3.14);
                store.Add("Good");
                store.Add(new Ct.Storehouse(5));
                foreach(object item in store.Items)
                {
                    Console.WriteLine(item);
                }
            }
        }
    }
    namespace Ct
    {
        class Storehouse
        {
            public Object[] Items;
            private int count;
            public Storehouse(int size)
            {
                Items = new Object[size];
                count = 0;
            }
            public void Add(Object obj)
            {
                if(count<Items.Length)
                {
                    Items[count] = obj;
                    count++;
                }
                else
                {
                    Console.WriteLine("Storage if full!");
                }
            }
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值