string s1 =@"\\\\"; //这四个\都是非转义字符因为使用了@
string s2 = "\\\\";//这四个\中代表转义字符
Console.WriteLine(s1);//输出四个
Console.WriteLine(s2);//输出两个
Console.ReadKey();
1 第一个c#程序
(1)三条控制台指令:
Console.WriteLine("你好");
Console.ReadKey(); 控制台暂停
Console.ReadLine(); 控制台读入
(2)占位符
{0} {1} {2} ……
Code :
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("你好");
string s = Console.ReadLine();//用户输入,并存到一个字符串中
Console.WriteLine(s + " hello");
int i1 = 10;
int i2 = 20;
Console.WriteLine("i1+i2="+(i1+i2));
Console.WriteLine("{0}+{1}={2}",i1,i2,(i2+i1));
Console.WriteLine("boy name :");
string boyName = Console.ReadLine();
Console.WriteLine("gril name : ");
string girlName = Console.ReadLine();
Console.WriteLine("{0} love {1}!",boyName,girlName);
Console.ReadKey();//按一个按键继续执行
}
}
}
2 vs开发介绍
行号:工具-选项-文本编辑器-c#-行号
3 变量
//值类型
C# 类型 .NET Framework 类型
bool System.Boolean
4Byte 32bit布尔型变量 逻辑值,true或者false,默认值为false
byte System.Byte
1Byte 8bit无符号整数无符号的字节,所存储的值的范围是0~255,默认值为0
sbyte System.SByte 1Byte
8bit有符号整数带符号的字节,所存储的值的范围是-128~127,默认值为0
char System.Char
2Byte 16bit 无符号Unicode字符,默认值为’\0’
decimal System.Decimal
16Byte 128bit十进制数不遵守四舍五入规则的十进制数,28个有效数字,通常用于财务方面的计算,默认值为0.0m
double System.Double
8Byte 64bit双精度的浮点类型,默认值为0.0d
float System.Single
4Byte 32bit单精度的浮点类型,默认值为0.0f
int System.Int32
4Byte 32bit有符号整数,默认值为0
uint System.UInt32
4Byte 32bit无符号整数,默认值为0
long System.Int64
8Byte 64bit有符号整数,默认值为0
ulong System.UInt64
8Byte 64bit无符号整数,默认值为0
object System.Object
指向类实例的引用,默认值为null
short System.Int16
2Byte 16bit有符号整数,默认值为0
ushort System.UInt16
2Byte 16bit无符号整数,默认值为0
string System.String
指向字符串对象的引用,默认值为null
4 字符串
(1)转义字符
C#中转义字符 用\和java里差不多
但是可以使用@使\代表非转义
注意:@只对于\起作用,对于其他的”,:都不起作用!
Eg
5 类型转换
Convert 转换类
Console.WriteLine("Enter a number :");
string s1 = Console.ReadLine();
int i1 = Convert.ToInt32(s1);
Console.WriteLine("Enter a number :");
string s2 = Console.ReadLine();
int i2 = Convert.ToInt32(s2);
Console.WriteLine("{0} + {1} = {2}",i1,i2,i1+i2);
Console.ReadKey();
6 命名规则
(1) 第一个字母必须是字符或者下划线开头
(2)不能使用c#关键字 eg class namespace new void等
7 if
Console.WriteLine("enter a number:");
string ageStr = Console.ReadLine();
int age = Convert.ToInt32(ageStr);
if (age > 10)
{
Console.WriteLine("< 10");
}
else {
Console.WriteLine(" >=10");
}
Console.ReadKey();
8 switch case
有一点需要注意的是: break 不写不行!
Console.WriteLine("enter a number:");
string ageStr = Console.ReadLine();
int age = Convert.ToInt32(ageStr);
switch(age){
case 10:
Console.WriteLine(10);
break;
case 20:
Console.WriteLine(20);
break;
default:
Console.WriteLine("default");
break;
}