【Unity 3D】C#数据类型和变量、命名规范的讲解(附源码)

  • Post author:
  • Post category:其他




觉得有帮助请点赞关注收藏~~~

一、C#的值类型和引用类型

1:值类型

值类型变量可以直接分配给一个值,它是从类Ststem.ValueType中派生的,值类型直接包含数据,如int char float等等

bool 布尔值

byte 8位无符号整数

char 16位unicode字符

decimal 128位精确的十进制值

double 64位双精度浮点值

float 32位单精度浮点值

int 32位有符号整数类型

sbyte 8位有符号整数类型

short 16位有符号整数类型

uint 32位无符号整数类型

对于值类型来说,C#语言中每种数据类型都有自己的取值范围,即能够存储值的最大值和最小值,借助数据类型提供的两个 属性MaxValue和MinValue,可以轻松的获取该数据类型可以存储的最大值和最小值

代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class API : MonoBehaviour
{
    // Start is called before the first frame update
    private void Awake()
    {
        Debug.Log("Awake: hello world");

    }
    void Start()
    {
        Debug.Log("byte  min" + byte.MinValue + "\nmax" + byte.MaxValue);
        Debug.Log("char  min" + char.MinValue + "\nmax" + char.MaxValue);
        Debug.Log("double  min" + double.MinValue + "\nmax" + double.MaxValue);
        Debug.Log("float  min" + float.MinValue + "\nmax" + float.MaxValue);
        Debug.Log("int  min" + int.MinValue + "\nmax" + int.MaxValue);
        Debug.Log("sbyte  min" + sbyte.MinValue + "\nmax" + sbyte.MaxValue);

        Debug.Log("short  min" + short.MinValue + "\nmax" + short.MaxValue);
        Debug.Log("uint  min" + uint.MinValue + "\nmax" + uint.MaxValue);
        Debug.Log("ulong  min" + ulong.MinValue + "\nmax" + ulong.MaxValue);
        Debug.Log("ushort  min" + ushort.MinValue + "\nmax" + ushort.MaxValue);

    }

    // Update is called once per frame
     private void Update()
    {
        Debug.Log("update event!");

    }
    private void FixedUpdate()
    {
        Debug.Log("FixedUpdate event!");
    }
    private   void LateUpdate()
    {
        Debug.Log("LateUpdate Evnet!");

    }
}

2:引用类型

引用类型不包含存储在变量中的实际数据,但他们包含对变量的引用

换句话说,它们是指一个内存位置,使用多个变量时,引用类型可以指向一个内存位置,如果内存位置的数据是由一个变量改变的,其他变量会自动反映这种值的变化

值类型和引用类型的区别

1:存取速度上的区别 值类型块 引用类型慢

2:用途上的区别  值类型表示实际数据,  引用类型表示指向存储在内存堆中的数据指针或引用

3:来源上的区别

4:位置上的区别  值类型存储在栈中 引用类型存储在堆中

5:类型上的区别  值类型直接存放实际数据 引用类型存放数据的地址 即对象的引用

二、装箱和拆箱

当一个值类型转换为引用类型时,称为装箱,当一个引用类型转换为值类型时,称为拆箱

以下为测试结果

代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class API : MonoBehaviour
{
    // Start is called before the first frame update
    private void Awake()
    {
        Debug.Log("Awake: hello world");

    }
    void Start()
    {
        Debug.Log("byte  min" + byte.MinValue + "\nmax" + byte.MaxValue);
        Debug.Log("char  min" + char.MinValue + "\nmax" + char.MaxValue);
        Debug.Log("double  min" + double.MinValue + "\nmax" + double.MaxValue);
        Debug.Log("float  min" + float.MinValue + "\nmax" + float.MaxValue);
        Debug.Log("int  min" + int.MinValue + "\nmax" + int.MaxValue);
        Debug.Log("sbyte  min" + sbyte.MinValue + "\nmax" + sbyte.MaxValue);

        Debug.Log("short  min" + short.MinValue + "\nmax" + short.MaxValue);
        Debug.Log("uint  min" + uint.MinValue + "\nmax" + uint.MaxValue);
        Debug.Log("ulong  min" + ulong.MinValue + "\nmax" + ulong.MaxValue);
        Debug.Log("ushort  min" + ushort.MinValue + "\nmax" + ushort.MaxValue);

        int i = 0;
        System.Object obj = i;
        Debug.Log(obj);
        int j = (int)obj;
        Debug.Log(j);


    }
    /*
    // Update is called once per frame
     private void Update()
    {
        Debug.Log("update event!");

    }
    private void FixedUpdate()
    {
        Debug.Log("FixedUpdate event!");
    }
    private   void LateUpdate()
    {
        Debug.Log("LateUpdate Evnet!");

    }
    */
}

三、常量和变量

变量是供程序操作的,在存储区的名字,变量的值可以存储在内存中,可以对变量进行一系列操作,常量是固定值,初始化后就不会改变。

常量的初始化

const int a=100;

1:在声明时必须初始化 并且不能再修改

2:常量的值必须能在编译时用于计算

3:常量总是静态的 但声明时不必包含static

变量的初始化

int a=100;

变量的作用域

变量的作用域时指可以访问该变量的代码区域,循环体中允许出现相同的变量名

四、命名惯例和规范

1:匈牙利命名法

它的思想是再所有变量类型前添加一个前缀来说明变量的类型

bool b_flag; 这样过于繁琐 使用不多

2:帕斯卡命名法

帕斯卡命名法的规则是首字母大写

public void SomeMethod(){}

3:骆驼命名法

顾名思义 是指使用混合大小写字母构成变量和函数的名字

someMethod()



创作不易 觉得有帮助请点赞关注收藏~~~



版权声明:本文为jiebaoshayebuhui原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。