C#中引用的用法
来源:互联网 发布:手机淘宝宝贝链接 编辑:程序博客网 时间:2024/05/21 10:00
因为C#的所有对象都是从Object派生,所以C#对象的创建,类似C++中指针的创建,如果按照C++的模式来理解C#的引用关系就非常简单了。
C#对象之间的传递,始终是传递的是指针(按照C++的理解),所以在函数参数传递过程中就需要考虑到应用的问题了,也就是给指针的地址重新赋值。
看看我这个C#的新手自己写的demo。仅仅学习分享。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace ch11Ex02{ partial class Person { private int age; public int Age { set { age = value; } get { return age; } } } partial class Person { private string name; public string Name { set { name = value; } get { return name; } } } class Program { static void increment(int x) { ++x; } static void increment2(ref int x) { ++x; } static void changePerson(Person p) { //++p.Age; Person pp = new Person(); pp.Age = 200; p = pp; } static void changePerson2(ref Person p) { //++p.Age; Person pp = new Person(); pp.Age = 200; p = pp; } static void Main(string[] args) { Person pTest = new Person(); pTest.Age = 10; Person pTest2 = pTest; pTest2.Age = 20; Debug.Assert(pTest.Age == 20, "ERROR!!!!!!"); changePerson(pTest); Debug.Assert(pTest.Age == 20, "ERROR"); changePerson2(ref pTest); Debug.Assert(pTest.Age == 200, "ERROR!!!!!!!!!!!!"); Console.ReadKey(); } }}
0 0
- C#中引用的用法
- C++中引用的用法
- java中引用的用法
- C++中引用的用法
- C#中 @ 的用法
- c#中@的用法
- C#中@的用法
- C#中@的用法
- C#中@的用法
- C#中 ?? 的用法
- C#中@的用法
- C#中!的用法
- C#中$的用法
- C++中关于 引用 的用法
- C++中引用(reference)的用法详解
- c++中引用(&)的用法
- php中引用变量&的用法
- C++中指针的引用用法实例
- 题目1004:Median
- 蘑菇街如何在双11中创造99.99%的可用性
- 履单流程的弹性架构——麦包包峰值架构实践
- 这是怎么回事啊怎么全没有开通啊
- 系统间的通信方式
- C#中引用的用法
- SQL_DML常用语句
- 电商峰值监控经验谈
- CentOS 7下安装docker
- iScreenLocker 3.1.8 安卓锁屏通知--苹果一样的体验
- JQuery 选择器
- mysql中datetime,timestamp的区别
- 学习笔记:GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2)
- unity3D技术之特殊文件夹和脚本编译顺序