一步一步学习C#编程札记


C#编程基础

一、C#程序结构

//文件名:Hiker.cs 类名不一定等于文件名
using System; //每一个程序必须在开头使用这一语句
public sealed class HitchHiker
{
public static void Main()//程序从Main 开始执行
{
int result;
result = 9 * 6;
int thirteen;
thirteen = 13;
Console.Write(result / thirteen); //输出函数
Console.Write(result % thirteen);
}
}
C#程序文件无源文件和头文件之分。

二、C#语言关键字

C#中76 个关键字:
abstract as
base bool break byte
case catch char checked class const continue
decimal default delegate do double
else enum event explicit extern
false finally fixed float for foreach
goto
if implicit in int interface internal is
lock long
namespace new null
object operator out override
params private protected public
readonly ref return
sbyte sealed short sizeof stackalloc static string struct switch
this throw true try typeof
uint ulong unchecked unsafe ushort using
virtual void
while
5 个在某些情况下是关键字:
get set value add   remove

三、数据类型
1.分类
值类型:变量直接包含它们自己的数据,局部变量总是放在栈(stack)中.
引用类型: 变量间接指向它们的数据,局部变量指向堆(heap)中的对象.
枚举(enum) 值类型
结构(struct) 值类型
类(class)     引用类型
接口(interface)引用类型
数组([ ]array ) 引用类型
委托(delegate) 引用类型
类型         取值                   解释
bool        true false          布尔型
float        3.14                    实型
double    3.1415                 双精度型
char        'X'                        字符型
int            9                        整型
string      "Hello"               字符串
object     null                     对象
2.整型分为:
类型 位数 System. 与CLS 兼容    有无符号
sbyte    8         SByte    否               有
ushort 16        UInt16 否               无
uint       32         UInt32 否               无
ulong    64          UInt64 否               无
byte      8           Byte     是               无
short     16         Int16     是               有
int          32          Int32      是                 有
long       64          Int64      是                 有
CLS:通用语言认证系统。
当把类型名作为.net framework函数实参的时候,只能使用System.Int32,而不能使用int。例如,Type.GetType("System.Int32"),而Type.GetType("int")语句是错误的。
注意:byte 和sbyt 只有8 位,因此它们不能作为数组的元素,因为数组元素的
最小尺寸是16 位(2 字节)。

  • 1
  • 2
  • 3
  • 下一页

相关内容