一、new操作符
在内存当中创造一个类型的实例,并且调用这个实例的实构造器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Form myForm = new Form() { Text = "hello",FormBorderStyle = FormBorderStyle.SizableToolWindow};//在后面加括号,可以设定属性的值
myForm.ShowDialog();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
var Person = new { Name = "Mr.OK", Age = 34 };
Console.WriteLine(Person.Name);
Console.WriteLine(Person.Age);
Console.WriteLine(Person.GetType().Name);
}
}
}
结果:
一个继承的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu.Report();
CsStludent csStu = new CsStludent();
csStu.Report();
}
class Student
{
public void Report()
{
Console.WriteLine("I'm a student");
}
}
class CsStludent: Student //继承父类
{
new public void Report()
{
Console.WriteLine("I'm a teacher,not a student.");//子类把父类的方法给隐藏掉了
}
}
}
}
结果:
这段程序中
class Csstludent:student
是继承的意思,但子类的方法可以改变(覆盖)。
二、checked,unchecked
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
uint x = uint.MaxValue;
Console.WriteLine(x);
string binStr = Convert.ToString(x, 2);
Console.WriteLine(binStr);
//uint y = x + 1;
//Console.WriteLine(y);//这里y溢出了,所以y的结果是0
/*==========================================================================*/
try
{
uint y = checked(x + 1);
Console.WriteLine(y);
}
catch (OverflowException)
{
Console.WriteLine("There is a overflow!");
}
}
}
}
结果:
checked就是检测到溢出就报警,unchecked反之
三、delegate
把delegate当做操作符使用(比较少用),不会懒学
四、sizeof
默认只能获取基本数据类型的字节数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
int x = sizeof(decimal);
Console.WriteLine(x);
unsafe //sizeof可以在unsafe中获取自定义的结构体类型尺寸
{
int y = sizeof(Student);
Console.WriteLine(y);
}
}
}
struct Student
{
int ID;
long Score;
}
}
答案是16,16,至于为什么自定义类型Student是16而不是12,不细究
五、->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
unsafe
{
Student stu;
stu.ID = 1;
stu.Score = 99;
Student* pStu = &stu;
pStu->Score = 100;
Console.WriteLine(stu.Score);
}
}
}
struct Student
{
public int ID;
public long Score;
}
}
基本操作符到此结束,下面是一元操作符
六、*和&
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
unsafe
{
Student stu;
stu.ID = 1;
stu.Score = 99;
Student* pStu = &stu;
pStu->Score = 100;
(*pStu).Score = 1000;
Console.WriteLine(stu.Score);
}
}
}
struct Student
{
public int ID;
public long Score;
}
}
七、+,-,++,–,~,!
略,小心造成溢出
~:在二进制进行取反
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
int x = 12345678;
int y = ~x;
string xStr = Convert.ToString(x, 2).PadLeft(32, '0');
string yStr = Convert.ToString(y, 2).PadLeft(32, '0');
Console.WriteLine(y);
Console.WriteLine(xStr);
Console.WriteLine(yStr);
}
}
}
!:取反,只能操作bool类型的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student(null);
Console.WriteLine(stu.Name);
}
}
class Student
{
public Student(string initName)
{
if (!string.IsNullOrEmpty(initName))
{
this.Name = initName;
}
else
{
throw new ArgumentException("initName cannot be null or empty");
}
}
public string Name;
}
}