基本概念----Beginning Visual C#

更多相关文章,见本人的个人主页:zhongxiewei.com

变量

注释方式:// 注释在这里/* 注释在这里 */

整形变量的类型:

TypeAlias forAllowed Values
sbyteSystem.SByteInteger between -2^7 and 2^7-1
byteSystem.ByteInteger between 0 and 2^8-1
shortSystem.Int16Integer between -2^15 and 2^15-1
ushortSystem.UInt16Integer between 0 and 2^16-1
intSystem.Int32Integer between -2^31 and 2^31-1
uintSystem.UInt32Integer between 0 and 2^32-1
longSystem.Int64Integer between -2^63 and 2^63-1
ulongSystem.UInt64Integer between 0 and 2^64-1

浮点型:

TypeAlias forApprox Min ValueApprox Max Value
floatSystem.Single 1.5x10-45 3.4x1038
doubleSystem.Double 5.0x10-324 1.7x10308
decimalSystem.Decimal 1.0x10-28 7.9x1028

其他简单类型:

TypeAlias forAllowed Values
charSystem.CharSingle Unicode char, between 0 and 65535
boolSystem.Booleantrue or false
stringSystem.Stringa sequence of characters

关于变量命名:

对于简单的变量可以采用camelCase格式,如:firstName,对于一些高级的变量可以采用PascalCase格式,如LastName,这是微软建议的。

字面常量:

true, false, 100, 100U, 100L, 100UL, 1.5F, 1.5, 1.5M, 'a', "hello"

verbatim, 逐字的常量:

"C:\\Temp\\mydir\\myfile.doc"等同于@"C:\Temp\mydir\myfile.doc",另外可以跨行输入字符串,如:

@"first line
second line
third line"

关于变量的使用,在很多变成语言中都有一个要求,就是在使用前必须进行初始化

表达式

操作符与C语言类似

操作符的顺序:

PrecedenceOperators
Highest++, --(used as prefixes); (), +, -(unary), !, ~
 *,/,%
 +,-
 <<, >>
 <,>,<=,>=
 ==,!=
 &
 ^
 |
 &&
 ||
 =,*=,/=,%=,+=,-=,<<=,>>=,&=,^=,|=
Lowest++,--(used as suffixes)

控制流

允许使用goto语句。条件表达式返回的类型必须是bool。如: if (10) return false; // 这句话是不能通过编译的

在使用switch-case的时候,有一点和c++的用法是不同的,如:

switch(testVar)
{
    case var1:
        // execute code
        ...         // 如果这里没有break语句的话,编译器是不能通过的,而在c++中可以,
                    // 如果想要让它继续执行下面的case,必须加上“goto case var2;”语句
                    // 当然如果case var1下面没有执行语句的话,也是合理的
    case var2:
        // execute code
        ...
        break;
    default:
        break;
}

循环语句和C++类似

更多变量相关

类型转换

TypeCan safely be converted to
byteshort,ushort,int,uint,long,ulong,float,double,decimal
sbyteshort,int,long,float,double,decimal
shortint,long,float,double,decimal
ushortint,uint,long,ulong,float,double,decimal
intlong,float,double,decimal
uintlong,ulong,float,double,decimal
longfloat,double,decimal
ulongfloat,double,decimal
floatdouble
charushort,int,uint,long,ulong,float,double,decimal

除了以上的隐式转换之外,还存在显示转换。为了防止溢出发生,可以用checked(expression)表达式进行处理,如:

byte destVar;
short srcVar = 281;
destVar = checked((byte)srcVar);

或是在项目的选项中,直接开启默认转换检测机制。如下图所示:

一些复杂的变量类型

Enumeration

定义一个enum,如下:

enum orientation : byte // byte能够被其他的整型类型,如int,long等替换
{
    north,
    south,
    east,
    west
}

那么声明一个枚举类型采用的方法为:orientation myDirect = orientation.north;;直接输出myDirect的结果为:north。想要输出它所表示的byte类型的具体数值,就必须采用显示的类型转换:(byte)myDirect

也可以将“north”字符串转换成枚举类型,采用的方式稍微复杂,具体如下:

string myStr = "north";
orientation myDirect = (orientation)Enum.Parse(typeof(orientation), myStr);

struct

struct类型和C++不同的是,变量的类型默认不是public的。而是private的。

Arrays

数组的声明方式如下:<baseType>[] <name>;,如:int[] myIntArray = {1,2,3};int[] myIntArray = new int[5];。不能够采用如下的方式进行声明:<baseType> <name>[];

多维数组的语法结构也有其特殊性。声明方式如下:<baseType>[,] <name>;,如:double[,] hillHeight = new double[3,4];。在多维数组中各个数据的排序顺序为行优先排序,如:

double[,] hillHeight = {{1,2,3,4}, {2,3,4,5}, {3,4,5,6}};
foreach (double height in hillHeight)
{
    Console.WriteLine("{0}", height);
}
// 输出结果依次为:
// [0,0]
// [0,1]
// ...

在当每一行的数据量不相等的时候,可以使用Arrays of Arrays。在使用数组的数组的时候,不能像多维数组一样进行使用,如:

int[][] jagged;
jagged = new int[3][4]; // 在编译的过程中会出现’cannot implicitly convert type 'int' to 'int[][]'的错误

有两种方式可以用来实现声明。如:

jagged = new int[2][];
jagged[0] = new int[3];
jagged[1] = new int[4];

// or like below
jagged = {new int[] {1,2,3}, new int[] {1}, new int[] {4,5,6,7}};

在对其进行遍历的时候也需要注意,不能采用如下的方式:

foreach (int val in jagged) // 出现编译错误,不能将int[]转换成int
{
    Console.WriteLine(val);
}

// 于是应该更改为如下方式:

foreach (int[] valArray in jagged)
{
    foreach (int val in valArray)
    {
        Console.WriteLine(val);
    }
}

对String的操作

string str=" hello world ";常见的有: str.Trim();,str.TrimStart(),str.TrimEnd(),str.ToLower(),str.PadLeft(10, '-'),str.Split({' '})

练习

  1. 逆序输出字符串,递归的方式完成
public static void printReverse(string str, int i)
{
    if (i < str.Length)
    {
        printReverse(str, i + 1);
        Console.Write(str.Substring(i, 1));
    }

    return;
}
 

转载于:https://www.cnblogs.com/grass-and-moon/p/4046668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值