一、基本结构
//命名控件 名称空间
namespace hello
{
//类型
internal class Program
{
//函数、方法
static void Main(string[] args)
{
Console.WriteLine("hello world!");
Console.ReadKey();
}
}
}
二、基本语法
1.注释
//注释
2.输出
Console.WriteLine("hello world!");
System.Console.WriteLine("hello world!");
3.变量
//变量的类型 变量的名称 = 初始化;
//CPU 100 200
//CPU只管执行指令 不管数据存储
//赋值
var n = 100;
var m = 200;
var sum = 0;
sum = n + m ;
//输出,按下任意键
Console.WriteLine(sum);
Console.ReadKey();
4.数据类型
4.1.值类型(Value types)
类型 描述 范围 默认值 bool 布尔值 True 或 False False byte 8 位无符号整数 0 到 255 0 char 16 位 Unicode 字符 U +0000 到 U +ffff ‘\0’ decimal 128 位精确的十进制值,28-29 有效位数 (-7.9 x 1028 到 7.9 x 1028) / 100 到 28 0.0M double 64 位双精度浮点型 (+/-)5.0 x 10-324 到 (+/-)1.7 x 10308 0.0D float 32 位单精度浮点型 -3.4 x 1038 到 + 3.4 x 1038 0.0F int 32 位有符号整数类型 -2,147,483,648 到 2,147,483,647 0 long 64 位有符号整数类型 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 0L sbyte 8 位有符号整数类型 -128 到 127 0 short 16 位有符号整数类型 -32,768 到 32,767 0 uint 32 位无符号整数类型 0 到 4,294,967,295 0 ulong 64 位无符号整数类型 0 到 18,446,744,073,709,551,615 0 ushort 16 位无符号整数类型 0 到 65,535 0
4.2.字符串(String)类型
String str = "hello";
5.不同类型变量的运算
Console.WriteLine("请输入一个数字:");
string str = Console.ReadLine();
int num = int.Parse(str) + 20;
Console.WriteLine(num);
Console.ReadKey();
6.输入输出
Console.WriteLine("请输入你的姓名:");
string name = Console.ReadLine();
Console.WriteLine("请输入你的故乡:");
string address = Console.ReadLine();
Console.WriteLine("你的名字是:"+name+" 你的故乡是:"+address);
Console.ReadKey();
7.if判断语句
bool flag = true;
if (flag)
{
Console.WriteLine("进入if内部");
}
else
{
Console.WriteLine("没有进入");
}
Console.ReadKey ();
8.方法
public int FindMax(int num1, int num2)
{
/* 局部变量声明 */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
9.for
/* for 循环执行 */
for (int a = 10; a < 20; a = a + 1)
{
Console.WriteLine("a 的值: {0}", a);
}
Console.ReadLine();
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int element in fibarray)
{
System.Console.WriteLine(element);
}
System.Console.WriteLine();
// 类似 foreach 循环
for (int i = 0; i < fibarray.Length; i++)
{
System.Console.WriteLine(fibarray[i]);
}
System.Console.WriteLine();
10.异常处理
try
{
// 引起异常的语句
}
catch( ExceptionName e1 )
{
// 错误处理代码
}
catch( ExceptionName e2 )
{
// 错误处理代码
}
catch( ExceptionName eN )
{
// 错误处理代码
}
finally
{
// 要执行的语句
}
异常类 描述 System.IO.IOException 处理 I/O 错误。 System.IndexOutOfRangeException 处理当方法指向超出范围的数组索引时生成的错误。 System.ArrayTypeMismatchException 处理当数组类型不匹配时生成的错误。 System.NullReferenceException 处理当依从一个空对象时生成的错误。 System.DivideByZeroException 处理当除以零时生成的错误。 System.InvalidCastException 处理在类型转换期间生成的错误。 System.OutOfMemoryException 处理空闲内存不足生成的错误。 System.StackOverflowException 处理栈溢出生成的错误。
11.类(class)
class Box
{
public double length; // 长度
public double breadth; // 宽度
public double height; // 高度
}
class Boxtester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // 声明 Box1,类型为 Box
Box Box2 = new Box(); // 声明 Box2,类型为 Box
double volume = 0.0; // 体积
// Box1 详述
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// Box2 详述
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// Box1 的体积
volume = Box1.height * Box1.length * Box1.breadth;
Console.WriteLine("Box1 的体积: {0}", volume);
// Box2 的体积
volume = Box2.height * Box2.length * Box2.breadth;
Console.WriteLine("Box2 的体积: {0}", volume);
Console.ReadKey();
}
}
12.文件操作
String path = "C:/Users/Administrator/Desktop/File" ;
DirectoryInfo root = new DirectoryInfo ( path) ;
FileInfo[ ] files = root. GetFiles ( ) ;
List< FileInfo> ListFiles = files. ToList ( ) ;
for ( int i = 0 ; i < ListFiles. Count; i++ )
{
String FileName = ListFiles[ i] . Name;
Console. WriteLine ( FileName) ;
if ( ListFiles[ i] . Name == "1.txt" )
{
File. Delete ( ListFiles[ i] . FullName) ;
Console. WriteLine ( "1.txt已被删除" ) ;
}
bool isHave = FileName. Contains ( "hello" ) ;
if ( isHave )
{
String srcFileName = ListFiles[ i] . FullName;
String destFileName = ListFiles[ i] . Directory. FullName + "/ddd" + ListFiles[ i] . Extension;
File. Move ( srcFileName, destFileName) ;
}
}
Console. ReadKey ( ) ;