• 深入C#数据类型


    值类型和引用类型

    常用的数据类型

    整形 int
    浮点型 foalt
    双精度浮点型 double
    字符串 string
    布尔 bool
    枚举 enum

    值类型

          值类型继承与System.ValueType类,每个值类型的对象都有一个独立的内存区域用于保存自己的值,值类型数据所在的内存区域称为栈(Stack)。只要在代码中修改它,就会在它的内存区域内保存这个值。

    引用类型

         引用类型继承与System.Object类,在C#中引用类型主要包括数组、类和接口等。

    细分值类型和引用类型

    值类型:

    基本数据类型:

    整形 int
    长整形 long
    浮点型 foalt
    双精度浮点型 double
    字符型 char
    布尔型 bool

    枚举类型: enum

    结构类型: struct

    引用类型:

    类:

    基类 System.Object
    字符串 string
    自定义类 class

     

    接口:interface

    数组:int【】,string【】

    结构

    结构定义

    访问修饰符 sturct 结构名
    {
    //结构体
    }

    结构的特点:

          •     机构中可以有字段也可以有方法

          1.     定义时结构内的字段不能被赋初值
          2. 结构的使用
            1.    结构可以不用new 直接定义结构的对象即可
            2.    声明结构的对象后必须给结构赋初值

     

          1. demo:

    public struct student {public int id/;IDpublic int age //年龄

    public void sayhi() { Console.WriteLine("学号:"+id+"年龄:"+age) } } //结构定义 
    public static void Main(string[] args) { student stu; stu.id=1234; stu.age=18; stu.sayhi(); } //结构调用

    值方式参数传递

    值方式参数传递时,参数是值类型则在调用后值不变,是引用类型时值可变

     

    引用方式参数传递

    引用方式参数传递时,不管参数时值类型还是引用类型调用后值都可变

    demo:

    值方式传递值类型参数

    复制代码
    public void addage(int age) 
            {
                age++;
            }
    
    static void Main(string[] args)
            {
                stu sb = new stu();
                int num = 3;
                Console.WriteLine(num);
                sb.addage(num);
                Console.WriteLine(num);
            }
    复制代码

    结果:

    image

    值方式传递引用类型参数

    复制代码
    public class stu
        {
           public  int age;
            public void addage(stu student) 
            {
                student.age++;
            }
        }
      public static void Main(string[] args)
            {
                stu sb = new stu();
                sb.age= 3;
                Console.WriteLine(sb.age);
                sb.addage(sb);
                Console.WriteLine(sb.age);
            }
    复制代码

    结果:

    image

    引用方式传递值类型参数

    复制代码
    public void addage( ref int age) 
            {
                age++;
            }
    
    static void Main(string[] args)
            {
                stu sb = new stu();
                int num = 3;
                Console.WriteLine(num);
                sb.addage(ref num);
                Console.WriteLine(num);
            }
    复制代码

    结果:

    image

    引用方式传递引用类型参数

    复制代码
    public class stu
        {
           public  int age;
            public void addage( ref stu student) 
            {
                student.age++;
            }
        }
      public static void Main(string[] args)
            {
                stu sb = new stu();
                sb.age= 3;
                Console.WriteLine(sb.age);
                sb.addage( ref sb);
                Console.WriteLine(sb.age);
            }
    复制代码

    结果:

    image

    值类型和引用类型

    常用的数据类型

    整形 int
    浮点型 foalt
    双精度浮点型 double
    字符串 string
    布尔 bool
    枚举 enum

    值类型

          值类型继承与System.ValueType类,每个值类型的对象都有一个独立的内存区域用于保存自己的值,值类型数据所在的内存区域称为栈(Stack)。只要在代码中修改它,就会在它的内存区域内保存这个值。

    引用类型

         引用类型继承与System.Object类,在C#中引用类型主要包括数组、类和接口等。

    细分值类型和引用类型

    值类型:

    基本数据类型:

    整形 int
    长整形 long
    浮点型 foalt
    双精度浮点型 double
    字符型 char
    布尔型 bool

    枚举类型: enum

    结构类型: struct

    引用类型:

    类:

    基类 System.Object
    字符串 string
    自定义类 class

     

    接口:interface

    数组:int【】,string【】

    结构

    结构定义

    访问修饰符 sturct 结构名
    {
    //结构体
    }

    结构的特点:

          •     机构中可以有字段也可以有方法

          1.     定义时结构内的字段不能被赋初值
          2. 结构的使用
            1.    结构可以不用new 直接定义结构的对象即可
            2.    声明结构的对象后必须给结构赋初值

     

          1. demo:

    public struct student {public int id/;IDpublic int age //年龄

    public void sayhi() { Console.WriteLine("学号:"+id+"年龄:"+age) } } //结构定义 
    public static void Main(string[] args) { student stu; stu.id=1234; stu.age=18; stu.sayhi(); } //结构调用

    值方式参数传递

    值方式参数传递时,参数是值类型则在调用后值不变,是引用类型时值可变

     

    引用方式参数传递

    引用方式参数传递时,不管参数时值类型还是引用类型调用后值都可变

    demo:

    值方式传递值类型参数

    复制代码
    public void addage(int age) 
            {
                age++;
            }
    
    static void Main(string[] args)
            {
                stu sb = new stu();
                int num = 3;
                Console.WriteLine(num);
                sb.addage(num);
                Console.WriteLine(num);
            }
    复制代码

    结果:

    image

    值方式传递引用类型参数

    复制代码
    public class stu
        {
           public  int age;
            public void addage(stu student) 
            {
                student.age++;
            }
        }
      public static void Main(string[] args)
            {
                stu sb = new stu();
                sb.age= 3;
                Console.WriteLine(sb.age);
                sb.addage(sb);
                Console.WriteLine(sb.age);
            }
    复制代码

    结果:

    image

    引用方式传递值类型参数

    复制代码
    public void addage( ref int age) 
            {
                age++;
            }
    
    static void Main(string[] args)
            {
                stu sb = new stu();
                int num = 3;
                Console.WriteLine(num);
                sb.addage(ref num);
                Console.WriteLine(num);
            }
    复制代码

    结果:

    image

    引用方式传递引用类型参数

    复制代码
    public class stu
        {
           public  int age;
            public void addage( ref stu student) 
            {
                student.age++;
            }
        }
      public static void Main(string[] args)
            {
                stu sb = new stu();
                sb.age= 3;
                Console.WriteLine(sb.age);
                sb.addage( ref sb);
                Console.WriteLine(sb.age);
            }
    复制代码

    结果:

    image

  • 相关阅读:
    git命令参考
    Raft 简介
    关于 K8S 的几个问题
    Ubuntu中用普通方法无法添加自启动
    so编译 链接和加载
    GDB调试命令详解
    MinGW中的头文件路径
    GDB使用详解
    dlopen、dlsym和dlclose的使用
    Windows下配置VSCode使用mingww64的gcc、g++编译器和GDB调试器
  • 原文地址:https://www.cnblogs.com/1402380606HZ/p/8157699.html
Copyright © 2020-2023  润新知