当前位置: 首页 > article >正文

C# 传值参数

传值参数

1.值类型

  • 值参数创建变量的副本:当传递值参数时,实际上是创建了原始变量的一个副本,然后将副本传递给方法。
  • 对值参数的操作永远不影响变量的值:由于是复制了一份新的副本,所以对副本进行操作不会影响原始变量的值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParametersExample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student stu=new Student();
            int y = 100;
            stu.AddOne (y);
            Console.WriteLine("y="+y);
            Console.ReadKey();
        }
    }

    class Student
    {
        public void AddOne(int x)
        {
            x = x + 1;
            Console.WriteLine(x);

        }
    }
}

在该代码中:AddOne 方法中的 x 是按值传递的,即 y 的值被复制给了 x,而在 AddOne 方法中对 x 的修改不会影响到 y 的原始值。

由此可知,在 C# 中,当我们通过值传递方式将一个变量传递给方法时,该方法内部对参数所做的任何修改都不会影响到原始变量的值。


2.引用类型1

我们都知道,引用类型的变量与实例时分开的

引用类型的变量会引用引用类型的实例,本质上是,引用类型的变量存储的值是引用类型的实例在堆内存当中的地址

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

namespace ParametersExample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student() { Name = "Tim" };
            SomeMethod(stu);
            Console.WriteLine("{0},{1}", stu.GetHashCode(), stu.Name);
            Console.ReadKey ();
        }
        static void SomeMethod(Student stu)
        {
            stu = new Student() { Name = "Tim" };
            Console.WriteLine("{0},{1}",stu.GetHashCode(),stu.Name);
        }
    }

    class Student
    {
        public string Name { get; set; }    
    }
}

当运行这段代码时:

  1. SomeMethod 方法中创建了一个新的 Student 实例,并打印了它的哈希码和名称。因为两次都设置 Name 为 "Tim",所以输出的名称将是 "Tim"
  2. 虽然 SomeMethod 方法内部改变了 stu 的引用指向了一个新的实例,但 Main 方法中的 stu 实例没有改变,因此Main 方法中输出的是原始 stu 实例的哈希码和名称。

注意,在实际编程中,很少会在方法内部创建一个新的对象。


3.引用类型2

通过值参数,只更新对象不创建新对象

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

namespace ParametersExample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student() { Name = "Tim" };
            UpdateObject(stu);
            Console.WriteLine("{0},{1}", stu.GetHashCode(), stu.Name);
            Console.ReadKey ();
        }
        static void UpdateObject(Student stu)
        {
            stu.Name = "Tim" ;
            Console.WriteLine("{0},{1}",stu.GetHashCode(),stu.Name);
        }
    }

    class Student
    {
        public string Name { get; set; }    
    }
}

注意,在实际编程中,很少通过传进来的参数,修改引用对象的值。

对于方法而言,输出主要靠返回值。


http://www.kler.cn/news/284754.html

相关文章:

  • Python 算法交易实验85 QTV200日常推进-钳制指标与交易量
  • 量化交易backtrader实践(四)_评价统计篇(4)_多个回测的评价列表
  • Python 如何进行密码学操作(cryptography模块)
  • 通帆科技“液氢微型发电站”:点亮氢能产业新征程
  • Mysql的InnoDB存储引擎
  • Unity-高版本的 bundle 资源路径的变化
  • 在大语言模型中,生成文本的退出机制,受max_generate_tokens限制,并不是所有的问答都完整的跑完整个transformer模型
  • 红黑树模拟实现STL中的map与set——C++
  • React 学习——zustand切片拆分
  • BUUCTF PWN wp--jarvisoj_level0
  • 入行「游戏策划」,该从何处下手?
  • 【FPGA】入门学习路线
  • 【QNX+Android虚拟化方案】114 - QNX /dev/switch 节点创建 及 读写功能实现实例
  • 3d网格补洞算法
  • 实测数据处理(RD算法处理)——SAR成像算法系列(十)
  • Python编码系列—Python中的安全密码存储与验证:实战指南
  • 每日一题,零基础入门FPGA——逻辑门
  • NFC射频--天线设计
  • 集成电路学习:什么是BIOS基本输入/输出系统
  • pytorch 均方误差损失函数
  • C_08_动态内存申请
  • 未来城市生活:科技与人文的交响
  • Docker 实战加速器(紧急情况!镜像库全面失效,一招解决Docker无法下载)
  • 云轴科技ZStack产品升级,浙江分公司产品发布会成功举办
  • tailwindcss
  • 《黑神话:悟空》:30%抽成真相
  • 如何使用 AWS CLI 为私有 AWS S3 存储桶中的对象创建预签名 URL
  • 2016年系统架构师案例分析试题五
  • 前端面试——八股文
  • 又一个免费代码生成工具