C#(二十四)之.NET框架

173 阅读1分钟

「这是我参与2022首次更文挑战的第23天,活动详情查看:2022首次更文挑战

1:.NET是一个平台。

每一个变量都有明确的数据类型:

优点:安全、垃圾回收等。

缺点:有些地方性能较低。

1.png

2:类型判定:

Sizeof:查找数据类型占几个字节

Typeof:查找通用系统类型名

Gettype:获取变量的类型


3:命名空间

using 关键字表明程序使用的是给定命名空间中的名称。

using System;

这个是C#中的基类

正常来写是这个样子的:System.Console.WriteLine();

 

以下是C#中的一些命名空间:

2.png

命名空间的嵌套:

namespace c
{
    namespace ac
    {
        class cat
        {
            public void Jump()
            {
                Console.WriteLine("C下的AC下的猫再跳");
            }
        }
    }
}

 

4:装箱和拆箱

这个我理解其实就是数据类型转换,一般情况下不用,因为有泛类型,且其十分损耗性能。

int nn = 16;
object obj = nn;  //装箱
Console.WriteLine(obj);
int i = (int)obj; // 拆箱
Console.WriteLine(i);

 

测试使用全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gc
{
    class Program
    {
        static void Main(string[] args)
        {
            // sizeof
            Console.WriteLine("int:"+sizeof(int));
            Console.WriteLine("float:" + sizeof(float));
            Console.WriteLine("long:" + sizeof(long));
            Console.WriteLine("decimal:" + sizeof(decimal));
            Console.WriteLine("-----------------------------------------------------");
            // typeof
            Console.WriteLine("int:" + typeof(int));
            Console.WriteLine("float:" + typeof(float));
            Console.WriteLine("long:" + typeof(long));
            Console.WriteLine("decimal:" + typeof(decimal));
            Console.WriteLine("-----------------------------------------------------");
            // GetType
            int n = 10;
            double f = 1.1;
            string s = "GC";
            Console.WriteLine("n:"+n.GetType());
            Console.WriteLine("f:" + f.GetType());
            Console.WriteLine("s:" + s.GetType());
            cat c = new cat();
            Console.WriteLine("c:"+c.GetType());
            Console.WriteLine("-----------------------------------------------------");
            a.cat aaa = new a.cat();
            aaa.Jump();
            b.cat bbb = new b.cat();
            bbb.Jump();
 
            c.ac.cat ccc = new c.ac.cat();
            ccc.Jump();
            Console.WriteLine("-----------------------------------------------------");
            int nn = 16;
            object obj = nn;  //装箱
            Console.WriteLine(obj);
            int i = (int)obj; // 拆箱
            Console.WriteLine(i);
        }
        class cat { }
    }
}
// 命名空间的嵌套
namespace c
{
    namespace ac
    {
        class cat
        {
            public void Jump()
            {
                Console.WriteLine("C下的AC下的猫再跳");
            }
        }
    }
}
namespace a
{
    class cat
    {
        public void Jump()
        {
            Console.WriteLine("a猫再跳");
        }
    }
}
namespace b
{
    class cat
    {
        public void Jump()
        {
            Console.WriteLine("b猫再跳");
        }
    }
}

有好的建议,请在下方输入你的评论。

欢迎访问个人博客 guanchao.site

欢迎访问小程序:

在这里插入图片描述