1.注释
(1).单行注释 // (2).多行注释/* */ (3)XML注释 /// ///
2.结构
namespace
class
method
3.变量
声明:datatype identifier
变量声明后必须初始化,C#强调安全性。
int i; //声明了变量,未初始化
int i = 10; //声明了并初始化 C#里的赋值=,和delphi的赋值有区别:=
int a = 2,b=3; //连续声明并初始化变量
.net 3.0以后(包括3.0)新增了“类型推断”,类型推断使用var关键字。编译器可以根据变量的初始值推断变量的类型
static void Main(string[] args)
{
var name = "kiny";
var age = 27;
Type nameType = name.GetType(); //判断变量类型
Type ageType = age.GetType();
Console.WriteLine("name is type " + nameType.ToString());
Console.WriteLine("age is type " + ageType.ToString());
Console.ReadLine();
}
4.常量 const
在声明时进行初始化
const string Version = "1.0";
5.预定义数据类型
(1)值类型和引用类型
值类型存储其值,引用类型存储对值的引用。
例:
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Study
7 {
8 class Program
9 {
10 static void Main( string [] args)
11 {
12 Study kiny = new Study();
13 kiny.setName( " hello " );
14 Study jyy = kiny;
15 Console.WriteLine(kiny.getName()); // 输出:hello
16 jyy.setName( " world " );
17 Console.WriteLine(kiny.getName()); // 输出:world
18 Console.ReadLine();
19 }
20 }
21
22 class Study
23 {
24 string name;
25 public void setName( string AName)
26 {
27 this .name = AName;
28 }
29
30 public string getName()
31 {
32 return this .name;
33 }
34 }
35 }
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study
{
class Program
{
static void Main(string[] args)
{
string name = "kiny";
string myname = name;
myname = "jyy";
Console.WriteLine(name); //输出:kiny
Console.WriteLine(myname);//输出:jyy
Console.WriteLine(name); //输出:kiny
Console.ReadLine();
}
}
class Study
{
string name;
public void setName(string AName)
{
this.name = AName;
}
public string getName()
{
return this.name;
}
}
}
为什么第二次Console.WriteLine(name)输出结果为:kiny? string本身是引用类型操作?为什么没有改变name的值呢?这个运算符=重载的原因,当myname = name时重载了运算符=;在堆上重新创建了一个String对象实际上指向了两个不用的引用,所以不会改变。运算符重载还有+,-等。
如果类型是引用类型,不想引用任何对象,可以把他设置为null
C#认定的基本数据类型没有内置在C#语音中,内置于.Net FrameWork中。存储的形式任然是基本数据类型。
C#有15个预定义数据类型,13基本数据类型,2个引用类型(String和Object)
一、基本数据类型
1.整型
sbyte System.SByte 8位有符号整数 -128---127
short System.Int16 16位有符号整数 -32768---32767
int System.Int32 32位有符号整数 -2147483648---214748647
long System.Long 64位有符号整数 -9223372063854775808---9223372063854775807
byte System.Byte 8位无符号整数 0---255
ushort System.UShort 16位无符号整数 0---65536
uint System.UInt 32位无符号整数 0---4294963967295
ulong System.ULong 64位无符号整数 0---18446744073709551615
C#和Dephi数据类型对照
C# Delphi
sbyte shortint
byte byte
short smallint
int integer
long int64
ushort word
uint cardinal
ulong -----
2.浮点类型
float System.Single 32位 精确到7位
double System.Double 64位 精确到15位
C#和Delphi对应
C# Delphi
float Single
double Double
float f = 123f;
3.decimal类型
decimal System.Decimal 128位 精确到28位
decimal Bin = 12.4M
4.bool类型
bool System.Boolean true或false
C#里面的true和false不能用1和0或者-1和0代替。
5.字符类型
char System.Char 16位的unicode字符
char类型用''引起来。 不能用双引号引起来,编译器误认为是字符串。
char myChar = 'A';
char myChar = (char)65;
char myChar = '\'';
char myChar = '\\';
6.object类型
object System.Object
System.Object和delphi中的TObject相似,其他的类型都是从他派生而来。
7.string类型
string System.Object
string字符串是引用类型
string name = "kiny";
string filepath = "c:\\windows\\system32";
string filepath = @"c:\windos\system32";