C# 学习教程之二

来源:互联网 发布:淘宝的直通车怎么开 编辑:程序博客网 时间:2024/06/16 23:43

C# 常量与变量


变量

        变量是表示内存地址的名称。变量具有名称、类型和值。变量名是变量在程序源代码中的标识;变量类型确定它所代表的内存大小和类型(堆栈、托管堆和非托管堆);变量值是指它所代表的内存块中的数据


类型

          C#完全支持.NET框架通用类型系统(CTS)定义的类型,所有的类型都是用类来定义的。

             C#语言类型划分两大类:引用类型和值类型

           值类型为自定义变量,而引用类型为类的实例化(对象)

            值类型和引用类型的区别:引用类型变量中存放的是对象的内存地址,对象的值存储在这个地址指示的内存当中(两个变量同时指向同一个对象,其中一个变量对数据操作会对另一个变量产生影响),值类型都有自己的数据副本,对一个变量的操纵不可能影响另一个变量。


值类型---------基本数据类型

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Hello_World{    class Program    {              public static void Main(string[] args)        {             //typeof 运算符用于获取“特定字符串”的基本数据类型            Console.WriteLine("object的数据类型:{0}", typeof(object));            Console.WriteLine("string的数据类型:{0}", typeof(string));            Console.WriteLine("sbyte的数据类型:{0}", typeof(sbyte));            Console.WriteLine("byte的数据类型:{0}", typeof(Byte));            Console.WriteLine("short的数据类型:{0}", typeof(short));            Console.WriteLine("ushort的数据类型:{0}", typeof(ushort));            Console.WriteLine("int的数据类型:{0}", typeof(int));            Console.WriteLine("uint的数据类型:{0}", typeof(uint));            Console.WriteLine("long的数据类型:{0}", typeof(long));            Console.WriteLine("ulong的数据类型:{0}", typeof(ulong));            Console.WriteLine("char的数据类型:{0}", typeof(Char));            Console.WriteLine("float的数据类型:{0}", typeof(float));            Console.WriteLine("double的数据类型:{0}", typeof(Double));              Console.WriteLine("Boolean的数据类型:{0}", typeof(Boolean));             Console.WriteLine("bool的数据类型:{0}", typeof(bool));             Console.WriteLine("decimal的数据类型:{0}", typeof(decimal));                 }    }}

结果展示:


变量和常量操作

            C#变量声明由一个类型和跟在后面的一个或多个变量组成,多个变量之间用都好隔开,声明以分号结束。

            C#使用赋值运算符”=“(等号)来给变量赋值,将等号右边的值赋给左边的变量。


            C#两种不同的常量(文本常量和符号常量)。

            文本常量:就是输入到程序的值。

            符号常量:使用常量修饰符”const“,并且常量在定义时必须初始化。


类型转化:类型转化就是将一种类型当做另一种个类型来使用。转型可以是隐式转型或者是显示转型

隐式转化:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Hello_World{   public  class Conversion    {       public static void Main() {           //隐式转型:系统默认的,不需要加以声明就能进行转化           byte byteVal = 120;           Console.WriteLine("byteVal 隐式转型前={0}", byteVal);           int intVal = byteVal;           Console.WriteLine("intVal ={0}", intVal);           long longVal = byteVal;           Console.WriteLine("longVal ={0}",longVal);           double doubleVal = byteVal;           Console.WriteLine("doubleVal  ={0}",doubleVal);       }                 }}

效果截图:

显示转化:请读者自行查阅相关资料,这里就不进行相关解说了。


装箱和拆箱

            装箱:将值类型转化为引用类型的过程。

            拆箱:将引用类型转化为值类型的过程。

相关代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Hello_World{   public  class Conversion    {       public static void Main() {           //装箱示列           int i = 100;//定义一个值类型变量           object obj=i;//将值类型变量的值装箱到一个引用类型对象中           Console.WriteLine("i 的值为{0},装箱的对象为{1}", i, obj);           i = 200;           Console.WriteLine("i的值为{0},装箱的对象为{1}", i, obj);               }                 }}

效果代码:


      拆箱代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Hello_World{   public  class Conversion    {       public static void Main() {           //装箱示列           int i = 100;//定义一个值类型变量           object obj=i;//将值类型变量的值装箱到一个引用类型对象中           Console.WriteLine("i 的值为{0},装箱的对象为{1}", i, obj);           i = 200;           Console.WriteLine("i的值为{0},装箱的对象为{1}", i, obj);           //拆箱示列           int j = (int)obj;           Console.WriteLine("拆箱:装箱对象为{0},值为{1}", obj ,j);               }                 }}

效果截图:

数组相关介绍:

               数组是一种包含相同数据类型的若干变量的数据结构,可以通过计算索引来访问这些变量。数组中包含的变量元素叫做数组元素。数组元素都有相同的类型,该类型为数组的元素类型。数组的元素类型可以是任意类型,包含数组类型。数组类型为其他元素类型后面加上一对中括号”[   ] “组成。

0 0
原创粉丝点击