基本概念----Beginning Visual C#
更多相关文章,见本人的个人主页:zhongxiewei.com
变量
注释方式:// 注释在这里和/* 注释在这里 */
整形变量的类型:
| Type | Alias for | Allowed Values |
|---|---|---|
| sbyte | System.SByte | Integer between -2^7 and 2^7-1 |
| byte | System.Byte | Integer between 0 and 2^8-1 |
| short | System.Int16 | Integer between -2^15 and 2^15-1 |
| ushort | System.UInt16 | Integer between 0 and 2^16-1 |
| int | System.Int32 | Integer between -2^31 and 2^31-1 |
| uint | System.UInt32 | Integer between 0 and 2^32-1 |
| long | System.Int64 | Integer between -2^63 and 2^63-1 |
| ulong | System.UInt64 | Integer between 0 and 2^64-1 |
浮点型:
| Type | Alias for | Approx Min Value | Approx Max Value |
|---|---|---|---|
| float | System.Single | 1.5x10-45 | 3.4x1038 |
| double | System.Double | 5.0x10-324 | 1.7x10308 |
| decimal | System.Decimal | 1.0x10-28 | 7.9x1028 |
其他简单类型:
| Type | Alias for | Allowed Values |
|---|---|---|
| char | System.Char | Single Unicode char, between 0 and 65535 |
| bool | System.Boolean | true or false |
| string | System.String | a 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语言类似
操作符的顺序:
| Precedence | Operators |
|---|---|
| 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++类似
更多变量相关
类型转换
| Type | Can safely be converted to |
|---|---|
| byte | short,ushort,int,uint,long,ulong,float,double,decimal |
| sbyte | short,int,long,float,double,decimal |
| short | int,long,float,double,decimal |
| ushort | int,uint,long,ulong,float,double,decimal |
| int | long,float,double,decimal |
| uint | long,ulong,float,double,decimal |
| long | float,double,decimal |
| ulong | float,double,decimal |
| float | double |
| char | ushort,int,uint,long,ulong,float,double,decimal |
除了以上的隐式转换之外,还存在显示转换。为了防止溢出发生,可以用checked(expression)表达式进行处理,如:
byte destVar;
short srcVar = ;
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 = {{,,,}, {,,,}, {,,,}};
foreach (double height in hillHeight)
{
Console.WriteLine("{0}", height);
}
// 输出结果依次为:
// [0,0]
// [0,1]
// ...
在当每一行的数据量不相等的时候,可以使用Arrays of Arrays。在使用数组的数组的时候,不能像多维数组一样进行使用,如:
int[][] jagged;
jagged = new int[][]; // 在编译的过程中会出现’cannot implicitly convert type 'int' to 'int[][]'的错误
有两种方式可以用来实现声明。如:
jagged = new int[][];
jagged[] = new int[];
jagged[] = new int[]; // or like below
jagged = {new int[] {,,}, new int[] {}, new int[] {,,,}};
在对其进行遍历的时候也需要注意,不能采用如下的方式:
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({' '})
练习
- 逆序输出字符串,递归的方式完成
public static void printReverse(string str, int i)
{
if (i < str.Length)
{
printReverse(str, i + );
Console.Write(str.Substring(i, ));
} return;
}
基本概念----Beginning Visual C#的更多相关文章
- Events基本概念----Beginning Visual C#
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
- 与类相关基本概念----Beginning Visual C#
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
- Windows Programming ---- Beginning Visual C#
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
- 函数----Beginning Visual C#
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
- visual formatting model (可视化格式模型)【持续修正】
概念: visual formatting model,可视化格式模型 The CSS visual formatting model is an algorithm that processes a ...
- 用于 Visual Studio 和 ASP.NET 的 Web 应用程序项目部署常见问题
https://msdn.microsoft.com/zh-cn/library/ee942158(v=vs.110).aspx#can_i_exclude_specific_files_or_fol ...
- Visual Studio 2012 开发环境配置+控制台工具+桌面应用程序
一.界面布局视图设置 1.窗口的布局.控制台窗口运行恢复到开发环境的设置方法 也可以保存好设好的个性化设置,导入设置: 2.视图|服务器资源管理器(sever explorer) 可以访问数据源.服务 ...
- Visual Basic了解
Visual Basic是一种由微软公司开发的结构化的.模块化的.面向对象的.包含协助开发环境的事件驱动为机制的可视化程序设计语言.这是一种可用于微软自家产品开发的语言.它源自于Basic编程语言.V ...
- Web 应用程序项目与 Visual Studio 中的网站项目的异同
要查看英语原文,请勾选“英语”复选框.也可将鼠标指针移到文本上,在弹出窗口中显示英语原文. 翻译 英语 本文档已存档,并且将不进行维护. Web 应用程序项目与 Visual Studio 中的网站项 ...
随机推荐
- JsonUtil
package com.test.base.util.json; import java.beans.IntrospectionException; import java.beans.Introsp ...
- 学习 React(jsx语法) + es2015 + babel + webpack
视频学习地址: http://www.jtthink.com/course/play/575 官方地址 https://facebook.github.io/react/ 神坑: 1.每次this.s ...
- org.apache.jasper.JasperException:省略"/html/sysmaintain/authority/user/../../module/verify_login.jsp" not found
说明了JSP页面里引用安全登录页面的jsp路径代码:<%@ include file="../../module/verify_login.jsp"%>这句代码引用的路 ...
- 初探ReactJS.NET 开发
ReactJS通常也被称为"React",是一个刚刚在这场游戏中登场的新手.它由Facebook创建,并在2013年首次发布.Facebook认为React在处理SPA问题上可以成 ...
- ASP.NET Core 1.0中的管道-中间件模式
ASP.NET Core 1.0借鉴了Katana项目的管道设计(Pipeline).日志记录.用户认证.MVC等模块都以中间件(Middleware)的方式注册在管道中.显而易见这样的设计非常松耦合 ...
- 剑指Offer面试题:15.反转链表
一.题目:反转链表 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点. 链表结点定义如下,这里使用的是C#描述: public class Node { public in ...
- Key/Value之王Memcached初探:一、掀起Memcached的盖头来
一.Memcached是何方神圣? 在数据驱动的Web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的HttpRuntim ...
- 飞鱼(FlyFish)——便捷的原型在线制作工具
关于项目原型制作,小菜先前写过一篇文章<FastUI快速界面原型制作工具>,只不过那个是用C#写的原型制作工具,但是感觉用C#写起来比较费力,而且也不太好用,经过高人指点,茅塞顿开,决定重 ...
- 修改注册表 去除Windows快捷方式图标小箭头
一些朋友不喜欢Windows系统中快捷方式图标上面的小箭头,下面介绍如何修改注册表去除快捷方式图标上的小箭头. 1.开始->运行->输入regedit,启动注册表编辑器,然后; 2.依次展 ...
- 服务器.htaccess 详解以及 .htaccess 参数说明(转载)
htaccess文件(或者”分布式配置文件”)提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户,所能使用的命令受到限 ...