c#入门经典5~8章 与c++对比

来源:互联网 发布:linux打开u盘命令 编辑:程序博客网 时间:2024/06/15 15:06

第5章  变量的更多内容

  5.1 类型转换  c#相对c++强制类型转换他多了一个函数checked检测是否溢出, 如果溢出的话会报错。 c++需要我们自己来做判断

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _5Uint{    class Program    {        static void Main(string[] args)        {            byte bInteger = 0;            short shInteger = 281;            // 强制类型转换时记得注意是否移除, c#已经帮我们检查过直接有checked这个函数可以做检查            bInteger = checked((byte)shInteger);            Console.WriteLine("bInteger = {0}", bInteger);            Console.WriteLine("shInteger = {0}", shInteger);            Console.ReadKey();        }    }}
当然也可以不加这个checkout 可以直接在windows下的编译器vs2010中设置  按照下面步骤就可以了

    对了还有用convert命令进行显示转换看下表

代码举例:

// 举例显示类型转换string stInfo = "256";int a = 0;a = Convert.ToInt32(stInfo);Console.WriteLine("a = {0}", a);Console.ReadKey();
  5.2 枚举,结构体,数组等的使用  与c++差不多不再多说

  5.3 有一个新的循环当然bost库中也有封装成类似于c#的这种方法下面说说这个函数foreach循环。  这个循环会迭代每个元素 。下面还是以举例看下:

 // 举例foreachint[] nArr = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };foreach ( int i in nArr ){Console.WriteLine("i = {0}", i);}Console.ReadKey();
不过这个foreach是只读访问,不能修改数组内的内容, 如果你是想修改里面的内容的话请不要使用这个函数

  5.4 string 字符串有4个函数挺好用的  ToCharArray()  ToLower()  ToUpper()   分别是转化为char类型,  转化为小写, 转化为大写。 用法就是<string>.ToCharArray() 以此类推这里不多做介绍 

     以及myString.Split(..) 可以根据某个字符把字符串分割为多个字符串,返回值试一个string类型的数组


            // 举例string.sqlit            string stTextSqplit = "hello;meizi;nihao";            string[] stTextArr;            stTextArr = stTextSqplit.Split(';');            foreach( string stOneInfo in stTextArr)            {                Console.WriteLine("stOneInfo = {0}", stOneInfo);            }            Console.ReadKey();


第六章  函数

 6.1函数与c++有点不同对于引用只需要在类型前加ref就可以了  一下举例  这个就类似于c++里面的引用的功能

        // 对于引用值的写法        static void addInteger(ref int nInteger)        {            nInteger *= 2;        }

            int nAddInteger = 10;            Console.WriteLine("nAddInteger = {0}", nAddInteger);            addInteger(ref nAddInteger);            Console.WriteLine("nAddInteger = {0}", nAddInteger);            Console.ReadKey();
这样的话打印出来一个是10 一个是20

 6.2 输出参数 关子健字是out, 使用方式和ref关键字相同。


第七章  调试和错误处理

  这张没仔细看。  调试和错误处理要看平时的积累了,不过里面有一些调试的小技巧大家可以看一下

第八章  面向对象编程的简介

  这章主要讲解面向对象的思想, 这个只看书也是理解不了了,需要在开发过程中顿悟



。。。


0 0
原创粉丝点击