文章目录
基础语法
+号
实验一:
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(1 + 1);
Console.WriteLine("1" + 1);
Console.WriteLine("1" + "1");
//Console.WriteLine("1" 1); 报错
Console.ReadKey();
}
}
}
output:
2
11
11
实验二:
string name = "Jack";
Console.WriteLine("My name is "+name);
Console.ReadKey();
output:
My name is Jack
占位符的使用
string name = "Jack";
int grade = 3;
Console.WriteLine("My name is {0},this is my {1}th year in college.",name,grade);
Console.ReadKey();
异常
转义符
算数运算符
1.两个int类型进行加,减,乘,除,取余,只能得到int类型
2.int*1.0
可以变成double(注意:是1.0,不是0.1)
例如:
int a = 10;
int b = 3;
double c = a / b;
double d = a * 1.0 / b;
Console.WriteLine(c);
Console.WriteLine(d);
Console.ReadKey();
Output:
类型转换
1.隐式类型转换:要求参与运算的操作数的类型必须一致,如果不一致,满足以下条件会发生自动类型转换,或者称之为隐式类型转换:
(1).两种类型兼容
(2).目标类型大于原类型
int num=10;
double d=num;
2.强制类型转换(显示类型转换):
(1).两种类型相兼容
(2).原类型大于目标类型
double d=303.6;
int n=(int) d;
Console.WriteLine(n);
Console.ReadKey();
Output:
303
Tips:保留两位小数的方法:
double a = 10;
double b = 3;
double c = a / b;
Console.WriteLine("{0:0.00}",c);//别忘了加冒号
Output:
3.33
Convert类型转换
1.
例子
string s = "123";
double a=Convert.ToDouble(s);
Console.WriteLine(a);
int b = Convert.ToInt32(s);
Console.WriteLine(b);
Console.ReadKey();
Output:
123
123
2.注意:使用convert必须"面子上"过得去。
//运行下面这行代码会异常,因为从表面上看123abc不是数字。
string s = "123abc";
double a=Convert.ToDouble(s);
Console.WriteLine(a);
Console.ReadKey();
3.各种类型转换的比较
逻辑运算符
&&逻辑与
||逻辑或
!逻辑非
异常捕获
假设一个块将出现异常,一个方法使用 try 和 catch 关键字捕获异常。try/catch 块内的代码为受保护的代码,使用 try/catch 语法如下所示:
try
{
// 引起异常的语句
}
catch( ExceptionName e1 )
{
// 出现异常后的要执行的代码
}
说明:
2.变量的作用域
3.switch-case语句
看看表达式和哪个case匹配
switch(表达式){
case 值1:
表达式的值和 值1匹配上了,需要执行的代码;
break;
case 值2:
表达式的值和 值2匹配上了,需要执行的代码;
break;
case 值3:
表达式的值和 值3匹配上了,需要执行的代码;
break;
default:
如果表达式的值和以上的case后面的值都没有匹配上,那么就执行这里的代码。
break;
}
复杂的数据类型
枚举
1.语法:
public enum 枚举名
{
值1,
值2,
值3,
... ...
}
Note:
2.枚举和int与string之间的转换
Note:枚举类型和int类型默认可以相互转换,所有类型都能转向string。
(1).枚举转int
namespace ConsoleApp1
{
public enum QQState
{
Online,
Offline,
Leave,
Busy,
Qme
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine((int)QQState.Online);
Console.WriteLine((int)QQState.Offline);
Console.WriteLine((int)QQState.Leave);
Console.WriteLine((int)QQState.Busy);
Console.WriteLine((int)QQState.Qme);
Console.ReadKey();
}
}
}
(1).int转为枚举
namespace ConsoleApp1
{
public enum QQState
{
Online,
Offline,
Leave,
Busy,
Qme
}
class Program
{
static void Main(string[] args)
{
int n = 3;
QQState state = (QQState)n;
Console.WriteLine(state);
Console.ReadKey();
}
}
}
(3).int类型转换为string
namespace ConsoleApp1
{ class Program
{
static void Main(string[] args)
{
int n = 3;
string s = n.ToString();
Console.WriteLine(s);
Console.ReadKey();
}
}
}
(4).将字符串转换为枚举类型
结构
1.语法:
public struct 结构名{
成员;//字段(字段前面要加"_")
}
Note:变量在程序运行的时候只能存储一个值,而字段可以存储多个值。
例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{ class Program
{
public struct Person
{
public string _name;
public int _age;
public Gender _gender;
}
public enum Gender
{
男,
女
}
static void Main(string[] args)
{
Person p;
p._name = "张三";
p._age = 20;
p._gender = Gender.男;
Console.WriteLine(p._name);
Console.WriteLine(p._age);
Console.WriteLine(p._gender);
Console.ReadKey();
}
}
}
数组
1.语法:
//第一种
数组类型[] 数组名=new 数组类型[数组长度]
//int[] nums = new int[10];
//第二种
数组类型[] 数组名 = {数值1,数值2,... ...};
//int[] nums = {1,2,3,4,5};
例子:
namespace ConsoleApp1
{ class Program
{
static void Main(string[] args)
{
int[] nums = new int[10];
//赋值
for(int i = 0; i < nums.Length; i++){
nums[i] = i;
}
for(int i = 0; i < nums.Length; i++){
Console.WriteLine(nums[i]);
}
Console.ReadKey();
}
}
}
冒泡排序
函数
方法
2.
Out参数
ref参数
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a = 3;
Test(ref a);
Console.WriteLine(a);
Console.ReadKey();
}
public static void Test(ref int a)
{
a = a + 5;
}
}
}
params参数
方法的重载
例子:
重载自己写的打印函数
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Printfline(1, 2);
Printfline("hello",1);
Console.ReadKey();
}
public static void Printfline(string a,string b)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
public static void Printfline(int a, int b)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
public static void Printfline(string a, int b)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
public static void Printfline(char a, char b)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
public static void Printfline(double a, double b)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
public static void Printfline(bool a, bool b)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
public static void Printfline(float a, float b)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
}
}