02、C#预定义数据类型

一、数据类型分为值类型和引用类型。

二、内存空间有”栈“和”堆“,值类型是存在”栈“中的,引用的地址是存在”栈“中,值是放在”堆“中的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C_shape
{
       
    class Program
    {        
        static void Main(string[] args)
        {
            //值类型实例
            int x = 10;
            int y = x;
            Console.WriteLine("变量x的值为{0},变量y的值{1}",x,y);
            y = 20;
            Console.WriteLine("变量x的值为{0},变量y的值{1}", x, y);
            Console.ReadLine();
        }
    }
}

 

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C_shape
{
    class myclass 
    {
        public int a = 100;
    }
    class Program
    {        
        static void Main(string[] args)
        {
            //引用类型实例
            myclass my1 = new myclass();
            Console.WriteLine(my1.a);
            myclass my2 = my1;
            my2.a = 200;
            Console.WriteLine("my1={0},my2={1}", my1.a, my2.a);
            Console.ReadLine();
        }
    }
}

 

三、预定义类型

   1、C#中有15个预定义类型,13个是值类型,2个是引用类型(string和object)。

大类 类型 范围 大小 定义方法
整型          sbyte -128~127 有符号8位整数  
byte 0~255 无符号8位整数 byte bt  = 210;
char U+0000~U+ffff 16位Unicode字符

 char a='a'

char a='\u0041'

short -32768~32767 有符号16位整数  
ushort 0~65535 无符号16位整数  
int -2147483678~2147483647 有符号32位整数  
uint 0~4294967295 无符号32位整数 uint ui = 1234U;
long -9223372036854775808~9223372036854775807 有符号64位整数 long lg = 1234L;
ulong 0~18446744073709551615 无符号64位整数 ulong ulg = 1234UL;
高精度类型     float  -1.5e45~+3.4e38  精度7位  float f =12.3F
 double  -5.0e324~+1.7e308  精度15到16位  double price = 25D
 decimal  -1.0*10-28~+7.9*1028  精度28到29位  decimal d = 12.30M
 布尔  bool  true和false  true和false  

                 

类型 CTS类 描述 包含方法 定义方法
object System.Object 根类型,C#中所有类型都派生自本类 Equals()、GetHashCode()、GetTy()、ToString()

object o ="YWJ";

object o1 = 123;

string System.String 表示Unicode字符串  

string str1 = "YWJ";

 

 

    2、转义字符 \

      char类型和string类型中用"/"表示特殊意义。

      在路径中“\”也会当成转义符,用“@”可以。

    3、数据类型转换。

      隐式转换,小范围变大范围。

byte bt = 10;
int bt1 = bt;

      显示转换,大范围变小范围。 

 int i = 10;
 byte bt1 = Convert.ToByte(i);
 byte c = (type)i;
 int ii = int.Parse("23")

    4、装箱和拆箱

      装箱:值类型变成引用类型。

      拆箱:引用类型变成值类型。

int i = 10;
object o = i;   //装箱
int j = int(o);  //拆箱

 

 

四、自定义类型。

  1. 值类型:struct(结构)和enum(枚举)。
  2. 引用类型:class类
posted @ 2021-04-26 17:48  遵义枫叶  阅读(181)  评论(0编辑  收藏  举报