C#学习笔记(七)中级 枚举 结构体 this关键字 base关键字 值类型 引用类型 装箱和拆箱

第三十四讲 枚举

如果你想让一组数字代表特定的意义,并且希望是安全的,可读性
强。那就用枚举吧

enum Color
{
Red,
Green,
Blue
}

代码:
namespace _234 {
//enum Color {Red,Green,Blue }
//enum Color:long { Red, Green, Blue }
enum Color { Red, Green=10, Blue }
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender,
EventArgs e) {
/*
Color c = Color.Blue;
//Color c = Color.Yellow;
MessageBox.Show(StringFromColor
(c));
//*/
int c1 = (int)Color.Red;
int c2 = (int)Color.Green;
int c3 = (int)Color.Blue;
MessageBox.Show(c1.ToString());
MessageBox.Show(c2.ToString());
MessageBox.Show(c3.ToString());
}

private string StringFromColor(Color color)
{
return color.ToString();
}
}

}

枚举类型是以自己的方式使用整型;
枚举类型可以是byte。sbyte。short。ushort。 int uint long
ulong的子类如果没有指定类型,则默认为int类型。

enum Day:byte{Sun=1,Mon,Tue,Wed,Thu,Fri,Sat}

第二十五讲 结构体 struct

通常用来封装小型相关变量组,例如:点的x,y坐标

public struct Point
{
public int X;
public int Y;
}

结构体:没有方法,只有成员变量的类。

代码:
namespace _234 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender,
EventArgs e) {
Book theBook = new Book();
theBook.price = 20;
theBook.title = "C#最好的视频教程";
theBook.author = "鹏哥";

this.textBox1.Text =
theBook.price.ToString() + "\r\n" + theBook.title + "\r\n"
+ theBook.author;}
}
public struct Book {
public int price;
public string title;
public string author;
}
}

第二十六讲 this关键字

1.索引
2.this简单的说,表示所在类。准确的说是代表类的对象
3.其他场合。代表构造函数

public class Person
{
private string name

public Person(string name)
{this.name=name;}
}

在静态成员中使用this都是不允许的。this代表的都是类的实例

而静态成员只能由类来访问,不能由对象来访问

代码:
namespace _234 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender,
EventArgs e) {
Person thePerson = new Person("鹏哥
");
this.textBox1.Text = "鹏哥";
}

}
public class Person {
private string name;
public Person(string name) {
this.name = name;
}
}
}

第二十七讲 base关键字

base:简单的说,代表直接父类
1.使用base可以访问父类中的成员
2.代表父类构造函数。

base用于类的继承场合用的多
2.不能访问静态成员和私有(private)的成员

namespace _234 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender,
EventArgs e) {
//Car theCar = new Car();
//Car theCar = new Car("宝马");
GaoJiCar theGaoJiCar = new
GaoJiCar();
}

}

}
namespace Traffic {
public class Vehicle {
private string name;
private int speed;
public Vehicle() { }
public Vehicle(string name) {
this.name = name;
}
public Vehicle(string name, int speed) {
this.name = name;
this.speed = speed;
MessageBox.Show("调用了父类中的两个
参数的构造函数");
}
public void getName() {
MessageBox.Show(this.name);
}
}
public class Car : Vehicle {
public Car() { base.getName(); }
public Car(string name) : base(name, 200) {
}//不是继承 是先执行 是调用的构造函数
}
public class GaoJiCar : Car {
public GaoJiCar() : base("宝马") { }
}
}

第二十九讲 值类型

堆栈int i=123;在堆栈中开辟空间 放入值类型,

托管站:放置所有的引用类型的数据

值类型: 基本数据类型 枚举 结构体
int i = 10;
int j = i;
i++;
MessageBox.Show(i.ToString());
MessageBox.Show(j.ToString());
第三十讲 引用类型
1.类
2.接口
3.委托

用引用类型的变量给另一个引用型的变量赋值时,只是把引用赋值
给它来了

Person thePerson1=new Person();
Person thePerson2=thePerson1;

namespace _234 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender,
EventArgs e) {
//空间是共享的
MyClass theClass1 = new MyClass();
MyClass theClass2 = theClass1;
theClass2.Value = 123;
MessageBox.Show
(theClass1.Value.ToString());
MessageBox.Show
(theClass2.Value.ToString());
}

}
public class MyClass {
public int Value = 0;
}

}

第三十一讲 装箱和拆箱

装箱:

值类型的变量——>引用类型的变量

int i = 123;
object o = i;

把数据从堆栈“装到”托管堆中

拆箱是一个强制转换的类型

原因:

学习装箱,是为了尽量避免装箱,装箱是被迫的。

在C#没有支持泛型之前,为了使某些程序具有通用性,使用了
Object(Object是所有类的根类)所有必须装箱。(数据结构)

对于已装箱的对象,因为无法直接调用其指定方法,所有必须先拆
箱,再调用方法,但再次拆箱,会生成新的栈实例,而无法修改装
箱对象。

资源消耗很大

原文地址:https://www.cnblogs.com/cheshui/p/2366704.html