字典的创建
Dictionary<string, A> A_list = new Dictionary<string, A>(); //其中A可以是类名
new操作符
new Form().ShowDialog();//创建使用后就可以释放了
new关键字
实现类的继承
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace var
{
class Program
{
static void Main(string[] args)
{
new Student().Report();
new Math_Student().Report();
}
}
class Student
{
public string Name;
public int Age;
public void Report()
{
Console.WriteLine("1");
}
}
class Math_Student:Student //继承了父类的方法和属性,但是利用new可以对方法再编辑
{
new public void Report()
{
Console.WriteLine("2");
}
}
}
//运行结果1 【换行符】2
new和var的组合使用
var可以自动推断类型
利用下面的方法,可以直接省去创建只有属性而没有方法的类的操作,但是不要滥用
var person = new { Name="小明",Score = 100};
Console.WriteLine(person.Name);
//运行结果:小明
checked/unchecked
checked 关键字用于对整型类型算术运算和转换显式启用溢出检查。
一般将checked与try-catch联合使用,这样可以抛出异常
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace @checked
{
class Program
{
static void Main(string[] args)
{
//捕捉溢出异常
try
{
uint x = uint.MaxValue;
uint y = checked(x + 1);
Console.WriteLine(y);
}
catch (OverflowException ex)
{
Console.WriteLine("此处有异常"); ;
}
}
}
}
//运行结果:此处有异常
下面将checked修改为unchecked
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace @checked
{
class Program
{
static void Main(string[] args)
{
//捕捉溢出异常
try
{
uint x = uint.MaxValue;
uint y =unchecked(x + 1);
Console.WriteLine(y);
}
catch (OverflowException ex)
{
Console.WriteLine("此处有异常"); ;
}
}
}
}
//运行结果为:0
可以直接设置代码块进行溢出检查
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace @checked
{
class Program
{
static void Main(string[] args)
{
//捕捉溢出异常
try
{
uint x = uint.MaxValue;
uint y =unchecked(x + 1);
Console.WriteLine(y);
}
catch (OverflowException ex)
{
Console.WriteLine("此处有异常"); ;
}
checked
{
//捕捉溢出异常
try
{
uint x = uint.MaxValue;
uint y = checked(x + 1);
Console.WriteLine(y);
}
catch (OverflowException ex)
{
Console.WriteLine("此处有异常"); ;
}
}
}
}
}
//运行结果:此处有异常
delegate
将delegate使用为操作符的情况已经比较少见了
原程序:
namespace delegate操作符
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.mybutton.Click += Mybutton_Click;
}
private void Mybutton_Click(object sender, RoutedEventArgs e)
{
this.myTextbox.Text = "大家好!!!";
}
}
}
使用delegate后
namespace delegate操作符
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.mybutton.Click += delegate(object sender, RoutedEventArgs e)
{
this.myTextbox.Text = "大家好!!!";
};
}
}
}
现在的替代方案,使用"=>"
namespace delegate操作符
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.mybutton.Click += ( sender, e)=>
{
this.myTextbox.Text = "大家好!!!";
};
}
}
}
sizeof
sizeof 运算符返回给定类型的变量所占用的字节数。
链接: MSDN对sizeof的介绍.
&&
可能会存在条件与的短路
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace and
{
class Program
{
static void Main(string[] args)
{
int a;
int b = 0;
a = Convert.ToInt32(Console.ReadLine());
if(a>11&&b++<3)
{
Console.WriteLine("。。。。。");
}
}
}
}
类推可知条件或的短路(写程序的时候要小心)
可空类型
适用条件:存储可能为null的数据时适用
情景模拟:对于没交作业的同学暂时将其成绩记为null,待其作业交后,修改为正常成绩
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace and
{
class Program
{
static void Main(string[] args)
{
Nullable<int> x = null; //可以对此条件下的x赋予空值
x = 100;
Console.WriteLine(x);
}
}
}
还有一种写法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace and
{
class Program
{
static void Main(string[] args)
{
int? x = null;
x = 100;
Console.WriteLine(x);
}
}
}
后期如果涉及需要将控制进行特殊的处理,处理方法如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace and
{
class Program
{
static void Main(string[] args)
{
//Nullable<int> x = null; //可以对此条件下的x赋予空值
int? x = null;
int y = x ?? 122;
Console.WriteLine(y);
}
}
}
//运行结果122
条件操作符:?:
相当于if/else的简写,用于代替if/else可以大大简化代码;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 条件操作符
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入待判断奇偶的数字:");
int a = Int16.Parse(Console.ReadLine());
string result = (a % 2==1) ? "该数为奇数" : "该数为偶数";
Console.WriteLine(result);
}
赋值和lambda表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lambda表达式
{
class Program
{
static void Main(string[] args)
{
int x = 5;
x <<= 1;//x+=1;
Console.WriteLine(x);
}
}
}
try
用于控制错误,防止程序在运行的过程中卡死
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace switch语句
{
class Program
{
static void Main(string[] args)
{
try
{
int a = Int16.Parse(Console.ReadLine());
switch (a)
{
case 1:
default:
break;
}
}
catch (FormatException)
{
Console.WriteLine("输入语句格式错误");
//不写throw的话,这个异常将不会被抛出,程序就不会被卡死了
}
}
}
}
//输入:gfucgcuq
//输入语句格式错误