Beginner with c# 3

1。3 类型
c#支持两种基本的类型:一种是值(value types),一种是引用(reference types)。值包括简单类型
(char、int、和float),枚举(enum)和结构(struct)。引用包括类(class),界面(interface),
代表(delegate)和数组阵列(array)。值与引用不同之处在于:值直接存储它的数据内容;而引用存储对象
的引用。是不是粉费解?!打个比方吧。你在某地买了套别墅(好棒噢)。却从未去过,只知道地址,怎
么办?你可以坐出租车,司机看了地址就知道怎样走不用你操心。你手里的地址就好像对象的名字,你把
它写在程序中,就好像把地址给了司机。司机就是你的编译器,它知道该去哪。你豪华的房子就好比那个
NGWS SDK开发包(82mb噢,够豪华了!俺的m啊--就这样烧喽)。房子里有你想要的东东,比如你想写一句
话(i dont like Hello world),就好像上面例子,要用到“WriteLine”。于是,你就给出“WriteLine”
的地址,比如:“Console.WriteLine”。明白?!俺可累了。zzz... (强打精神)不知道你想到没有,
值和引用的区别可以引出一个重要特性。值的变量和变量存储的数据是一一对应的,唯一性。而引用则不
然。引用中不同的变量可以引用同一个对象的实例。当其中一个变量改变实例的值时,其他引用这个实例的
变量也会受到影响(当然,变量本身并没有改变,即,地址没变)。瞧,变量只是说明存储对象的位置(地
址),而不是对象本身。就好像你漂亮的房子被烧了,但你的地址并没有改变,但地址对应的房子就没了。
也许是别人也有这个地址,他去烧了你的房子!好了,在给个例子:*/

1: using System;
2: class CValue
3: {
4: public int Value = 0;
5: }
6: class Test
7: {
8: static void Main() {
9: int val1 = 0;
10: int val2 = val1;
11: val2 = 123;
12: CValue ref1 = new CValue();
13: CValue ref2 = ref1;
14: ref2.Value = 123;
15: Console.WriteLine("Values: {0}, {1}", val1, val2);
16: Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);
17: }
18: }

/* 下面是输出的结果:
Values: 0, 123
Refs: 123, 123

啊哈,应该粉清楚了吧。变量val1和变量val2互不影响,它们各自有自己的存储空间。而ref2复制
了ref1,所以,它们引用了同一个对象的实例。当改变它们其中一个的时候,就会影响到另一个的
值。

时间: 2024-07-31 13:57:40

Beginner with c# 3的相关文章

新手教程之:循环网络和LSTM指南 (A Beginner’s Guide to Recurrent Networks and LSTMs)

  新手教程之:循环网络和LSTM指南 (A Beginner's Guide to Recurrent Networks and LSTMs)   本文翻译自:http://deeplearning4j.org/lstm.html   其他相关教程: 1. 深度神经网络简介 http://deeplearning4j.org/zh-neuralnet-overview 2. 卷积网络 http://deeplearning4j.org/zh-convolutionalnets   目录: 1.

(转)A Beginner's Guide To Understanding Convolutional Neural Networks Part 2

Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolutional Neural Networks Part 2 Introduction Link to Part 1                 In this post, we'll go into a lot more of the specifics of ConvNets. Disclaimer:

Beginner: Using Servlets to display, insert and update records in database.(1)

servlet Displaying Records from the Database with Java Servlets. Overview : In this article I'll explain each step you need to know to display records from the database using Servlets. The steps for displaying records in JSP pages and Java Beans are

Beginner: Using Servlets to display, insert and update records in database.(3)

servlet Updating records in the Database with Java Servlets. Overview : This article is next in the series of articles about selecting, inserting, updating and deleting records from the database using JDBC. In this article we will learn how to update

Beginner: Using Servlets to display, insert and update records in database.(2)

servlet Inserting records into the Database with Java Servlets. Overview : This article is next in the series of articles about selecting, inserting, updating and deleting records from the database using JDBC. In this article we will learn how to ins

Beginner with c# 4

1¡£4 Ô¤¶¨ÒåÀàÐÍ£¨Predefined types£ c#Ìá¹ÁËһϵÁÐÔ¤¶¨ÒåÀàÐÍ¡£ËüÃÇÓëc/c++Óв»ÉÙÏàËƵĵط½¡£Ô¤¶¨ÒåÒýÓÃÀàÐÍÓÐobjectºÍstring¡£ objectÀàÐÍÊÇËùÓÐÆäËûÀàÐ͵Ļù´¡¡£ Ô¤¶¨ÒåÀàÐÍ°üÀ¨·ûºÅÊý¡¢ÎÞ·ûºÅÊý¡¢¸¡µã¡¢²¼¶û¡¢×Ö·ûºÍʽøÖÆÊý¡£·ûºÅÊýÓУºsbyte¡¢short¡¢ intºÍlong£»

Beginner with c# 5

1.5 数组类型(Array types) 数组可以是一维的,也可是多维的.数祖的成员可以是整齐的,也可以是变长(jagged)的. 一维的数组是最普通,最简单的.这里值给出一个例子,就不多解释了.*/ using System; class Test { static void Main() { int[] arr = new int[5]; for (int i = 0; i < arr.Length; i++) arr[i] = i * i; for (int i = 0; i < ar

Beginner with c# 7

1.7 语句(Statements) c#借用了c/c++大多数的语句方法,不过仍然有些值得注意的地方.还有些地方是有所改动的. 在这里,我只提一些c#特有的东东. 1.7.10 "foreach"语句 "foreach"语句列举一个集合内的所有元素,并对这些元素执行一系列的操作.还是看看例子吧:*/ using System; using System.Collections; class Test { static void WriteList(ArrayLis

Beginner with C#

1 绪论 c# 是一种简练,时髦(?),面向对象(object oriented),类型可靠(type-safe)的 编程语言.它(发音:C sharp)是从c/c++发展而来的(?俺觉得更象是java),和c/c++ 是一个语系.所以,很容易被c/c++的程序员接受.c#的目标是结合Visual Basic的高产和 C++质朴的力量. c#将会是vs7的一分子.vs7还支持vb,vc和标记语言--VBScript和JScript.所有这些语言 都会在Next Generation Window

Beginner with c# 2

1.2 自动化的内存管理(Automatic memory management) 手动管理内存需要程序员自行分配和释放内存块.这要求程序员有清晰的头脑和对整个运行过程有十分的 把握(好难!).而c#把程序员从这难以承担的任务中解放出来.在多数的情况下,这种自动内存管理提 高代码的质量和程序员的生产力.并且,不会对程序的意图和执行产生幅面的影响(?俺可不相信m$的鬼 话).不过,估计比java的回收站好一点吧.因为c#出道迟嘛(尽胡扯).好了,来看看例子.*/ using System; pub