深入C#数据类型

本文深入解析了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

转载于:https://www.cnblogs.com/1402380606HZ/p/8157699.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值