笔记都很零碎,不系统,也可能有不得体的地方
● 变量
表面看是变量用途是储存数据,实际上,变量表示了储存位置,并且每个变量都有一个类型,以决定什么样的值能存入变量,比如
static void Main(string[] args)
{
int x;
x = 100;
}
x是个变量,类型为 int 型,100满足这个类型就可以储存在int对应的内存中(软件没运行是放在硬盘中,运行会加载到内存中,不同类型的值对应着内存中的不同位置,long 类型可以存int类型,反之则不行,因为装不下)
变量一共有7种
静态变量 实例变量(成员变量,字段) 数组元素 值参数 引用参数 输出形参 局部变量
狭义的理解(大多数交流)多说变量其实就指的是局部变量,因为其他几种都有比较鲜明的名字。
静态变量:(举个例子)
创建了个学生类,Amount 就是创建的静态变量(static),静态变量这个成员隶属于student这个类,而不是这个类的实例(所以只有student.才能出来)
局部变量:简单讲就是声明在方法(函数)里的变量 ????
变量的声明:有效的修饰符opt(如public static)+类型+变量名+初始化opt opt代表根据情况有时候有有时候没有
● 一些重要单词
argument 参数
overload 重载
● 方法
1 方法只能建立在类里面
2 命名规范:由动词和动词短语构成,各单词首字母大写
3 静态方法(static)只属于类不属于实例,实例不能调用静态方法。 ps:这里分为实例方法和静态方法,有无static修饰符的区别
4 构造函数 构造函数有默认的(快捷键 ctor+tap两下),作用是系统自动给你初始化了一个值,但是我们想初始化一个自己想要的值所以要自定义,下面这是我自定义的两个构造函数来初始化想初始化的变量(ID和Name),构造好构造函数后,主函数里就必须要给初始值了,不然就会提示错误,可以起到警示作用,构造函数会自己对应有参数和无参数(块注释快捷键 Ctrl+K C,消除Ctrl+K U)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApp19
{
class Program
{
static void Main(string[] args)
{
student stu1 = new student();
student stut2 = new student(2, "hhh");
Console.WriteLine(stu1.ID);
Console.WriteLine(stu1.Name);
Console.WriteLine(stut2.ID);
Console.WriteLine(stut2.Name);
}
class student
{
public int ID;
public string Name;
public student()//无参构造函数
{
this.ID = 1;
this.Name = "hh";
}
public student(int x, string y)//有参构造函数
{
this.ID = x;
this.Name = y;
}
}
}
}
● 重载:简单说就是方法名一样,括号里的形参个数和类型不能完全一样,与返回值类型和形参名字无关,构造函数也是一样的(其实还有更详细的,先不慌,初学先了解这个);
● Debug(调试)
先断点 : 让程序运行断点处,方便调试
Step in(F11):详细追踪过程,一直下一步
Step over (F10) : 比较粗略追踪,熟悉的地方直接跳过,看结果,如果出错就在这里再进一步详细调试,可以节约时间,效率高
Step out(Shift+F11):Step in 的逆过程,追踪上一步
以上调试时都会一起用,会更方便
● 操作符(运算符)Operator
这个图是一个教学视频里的,从上到下优先级降低,同一行优先级一样
操作符不能脱离于它关联的数据类型,比如“ / ” 除号,第一个输出是整型 1 ,第二个是double型 1.25
static void Main(string[] args)
{
int a = 5;
int b = 4;
int c = a / b;
Console.WriteLine(c);
double x = 5;
double y = 4;
double z = x / y;
Console.WriteLine(z);
}
" . " 这个点操作符,术语为成员访问操作符
(1)访问外层名词空间的子集名词空间 比如System.IO
(2)访问名词空间中的类型 比如IO.File
(3) 访问类型的静态的静态成员 比如 File.Copy()
(4)访问对象里的成员
“ () ”方法调用操作符,跟在方法名后,表示调用这个方法,委托的时候不跟
“ [] ”访问集合里的元素,一般是数组
“ ++ ” “ -- ”这两个操作符为自加和自减符号,下面这个输出分别是101,100,这是后置加加,会先赋值再自加,减减同理
static void Main(string[] args)
{
int x = 100;
int y = x++;
Console.WriteLine(x);
Console.WriteLine(y);
}
下面这个输出是 101 ,101 ,这是前置加加,先自加后赋值,减减同理
static void Main(string[] args)
{
int x = 100;
int y = ++x;
Console.WriteLine(x);
Console.WriteLine(y);
}
" * " " / " 乘除法注意数值提升,比如double和int之间运算,结果会转为double
" ?: " 条件操作符,正确就执行'' : ''左边,错误执行右边
static void Main(string[] args)
{
int x = 70;
string str;
str=x >= 60 ? "pass" : "faied";
Console.WriteLine(str);
}
" += " x=x+1; 等价于 x+=1;
typeof: 帮助我们查看一个类型的内部结构(包括属于什么名次空间,什么名字,包含什么方法......)
static void Main(string[] args)
{
Type t = typeof(int);
Console.WriteLine(t.Namespace);
Console.WriteLine(t.Name);
Console.WriteLine(t.FullName);
}
new:
1.帮助创建一个类型实例,并调用一个实例构造器,还可以调用初始化器,比如下面可以在后面初始化一些实例的一些属性
static void Main(string[] args)
{
Form myform =new Form() { Text="hello world!"};
myform.ShowDialog();
}
2.还可以配合 var 来实例化匿名类的实例,上面是非匿名类,var作用是在不清楚变量什么类型时候让系统自己根据前后文判断类型,这里是个匿名类,所以不知道类型用var,可以用GetType().Name函数来判断出这个匿名类是什么
static void Main(string[] args)
{
var person = new { Name = "hh", Age = 20 };
Console.WriteLine(person.Age);
Console.WriteLine(person.Name);
}
● 类型转换
隐式(implicit)类型转换
1.不丢失精度的转换
比如 int类型转换为long类型,以此类推
2.子类向父类的转换
3.装箱
显示(explicit)类型转换
1.有可能丢失精度的转换
不能隐式的转换过去,就只能强制显示转换,如下long类型转为int,需要显示转换格式,格式为(T).xx
static void Main(string[] args)
{
long x = 1000;
int y = (int)x;
}
2.使用Convert类
convert有很多重载。“+”这个操作符会根据类型自动运算出相应的格式,所以下面输出为1213,25
static void Main(string[] args)
{
string a = "12";
string b = "13";
int x = System.Convert.ToInt32(a);
int y = System.Convert.ToInt32(b);
Console.WriteLine(a+b);
Console.WriteLine(x+y);
}
3.ToString方法和各数据类型的Parse/ TryParse方法
ToString格式为 xx.ToString(); 把数据类型转化为字符串 (c#中所有数据类型都有这个方法)
Parse (TryParse有输出类型,先放着)
static void Main(string[] args)
{
string a = "12";
string b = "13";
int x = int.Parse(a);
int y = int.Parse(b);
Console.WriteLine(a+b);
Console.WriteLine(x+y);
}
4.拆箱
● 语句
1.声明语句
声明变量
声明常量(不能被改变的量):要在前面加const,比如 const int x =100; 后面就不能再改变了,不然编译不过去。
2.表达语句
3.块语句
4.选择语句
if ;if else; if if else... else;
switch
5.try 语句
try + 一个或多个 catch
class Program
{
static void Main(string[] args)
{
Calculator cal = new Calculator();
Console.WriteLine(cal.GetAdd("abc", "100")); //对应字符串格式不对
Console.WriteLine(cal.GetAdd(null, "100")); //不能为null值
Console.WriteLine(cal.GetAdd("999999999999", "100")); //值超出范围
}
}
class Calculator
{
public int GetAdd(string a,string b)
{
int x=0;
int y=0;
try
{
x = int.Parse(a);
y = int.Parse(b);
}
catch(ArgumentNullException ane)
{
Console.WriteLine(ane.Message);
}
catch(FormatException fe)
{
Console.WriteLine(fe.Message);
}
catch(OverflowException oe)
{
Console.WriteLine(oe.Message);
}
int result = x + y;
return result;
}
}
try + finally
try + 一个或多个 catch + finally
6.循环(迭代)语句
while
do while
for
foreach:遍历集合元素,每遍历一个元素,可以执行对应语句,这里声明了个数组,也可以用params关键字
static void Main(string[] args)
{
int[] myArray = new int[] { 1, 2, 3 };
var result = Add(myArray);
Console.WriteLine(result);
}
static int Add( int[] intArray)
{
int sum = 0;
foreach (var item in intArray)
{
sum += item;
}
return sum;
}
7.跳转语句
continue; break 只影响直接包含它的循环
static void Main(string[] args)
{
int score = 0;
int sum = 100;
do
{
int a = 0;
Console.WriteLine("输入数字1: ");
string str1 = Console.ReadLine();
if (str1.ToLower() == "end")
{
break;
}
try
{
a = int.Parse(str1);
}
catch
{
Console.WriteLine("输入格式错误");
continue;
}
Console.WriteLine("输入数字2: ");
int b = 0;
string str2 = Console.ReadLine();
if (str2.ToLower() == "end")
{
break;
}
try
{
b = int.Parse(str2);
}
catch
{
Console.WriteLine("输入格式错误");
continue;
}
sum = a + b;
if (sum == 100)
{
score++;
Console.WriteLine("恭喜得一分!");
}
else
{
Console.WriteLine("游戏失败!");
}
}
while (sum == 100);
Console.WriteLine("你的得分为{0}",score);
Console.WriteLine("游戏结束!");
}
字段,属性
字段尽量是private或protected的,用属性来保护
class Program
{
static void Main(string[] args)
{
try
{
Student stu1 = new Student();
stu1.Age = 20;
Student stu2 = new Student();
stu2.Age = 30;
Student stu3 = new Student();
stu3.Age = 200;
int avgAge = (stu3.Age + stu2.Age + stu1.Age) / 3;
Console.WriteLine(avgAge);
}
catch
{
Console.WriteLine("输入数据不正确!");
}
}
}
class Student
{
private int age;//字段
public int Age//属性:字段的包装器,对字段进行了限制
{
get
{
return this.age;
}
set
{
if(value>=0&&value<=120)
{
this.age = value;
}
else
{
throw new Exception("数据异常!");
}
}
}
}
索引器(indexer):它使对象能够与数组相同的方式(使用下标)进行索引
创建索引器:用indexer+两下Tap
● 多线程