案例1:装箱拆箱
class Program
{
/// <summary>
/// 装箱,拆箱
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//装箱:就是值类型到引用类型的转换
//值类型对值类型的转换叫做数据转换
//值类型
int i = 123;
//值类型赋给引用类型称为装箱
Object o = i;
i = 567;
Console.WriteLine("值类型:{0}",i);
Console.WriteLine("引用类型:{0}",o);
//拆箱:将引用类型转换给值类型
int j = (int)o;
Console.WriteLine("值类型:{0}",j);
//
object obj = 100;
int jt = (int)obj;
Console.WriteLine(jt);
Console.ReadKey();
}
}
——————————————————————————————————————————————————————————
案例2:
char[] c=new char[2]{'H','I'};
string str = new string(c);
Console.WriteLine(str);
Object o = new object();
Console.ReadKey();
————————————————————————————————————————————————————————
案例3:类和结构
namespace e3
{
public enum Genders
{
Male,Female
}
class Student
{
public string name;
public int age;
public Genders genders;
public string hobby;
public int popularity;
public void Vote(Student stu)
{
stu.popularity++;
}
public void SayHi()
{
Console.WriteLine("姓名:{0},年龄:{1},性别:{2},爱好:{3},受欢迎度:{4}", name, age, genders, hobby, popularity);
}
}
}
——————————————————————————————————————————————————————
namespace e3
{
struct studentStruct
{
public string name;
public int age;
public Genders genders;
public string hobby;
public int popularity;
public void Vote(ref studentStruct ss)
{
ss.popularity++;
}
public void SayHi()
{
Console.WriteLine("姓名:{0},年龄:{1},性别:{2},爱好:{3},受欢迎度:{4}",name,age,genders,hobby,popularity);
}
}
}
——————————————————————————————————————————————————————
namespace e3
{
class Program
{
static void Main(string[] args)
{
studentStruct ss1 = new studentStruct();
//studentStruct ss1;
ss1.age = 20;
ss1.genders = Genders.Male;
ss1.hobby = "越狱";
ss1.name = "Scofield";
ss1.popularity = 100;
ss1.SayHi();
ss1.Vote(ref ss1);//加上ref 变成传引用类型
ss1.SayHi();
//
Student stu1 = new Student();
stu1.age = 20;
stu1.genders = Genders.Male;
stu1.hobby = "越狱";
stu1.name = "Scofield";
stu1.popularity = 100;
stu1.SayHi();
stu1.Vote(stu1);
stu1.SayHi();
Console.ReadKey();
}
}
}
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
案例4
namespace e5
{
public enum Genders
{
Male,Female
}
class Student
{
public Student(string name, int age, string hobby, Genders gender, int popularity)
{
this.name = name;
this.age=age;
this.hobby = hobby;
this.gender = gender;
this.popularity = popularity;
}
private string name;
private int age;
private string hobby;
private Genders gender;
private int popularity;
public void SayHi()
{
Console.WriteLine("姓名:{0},年龄:{1},爱好:{2},性别:{3},受欢迎度:{4}", name, age, hobby, gender, popularity);
}
}
}
——————————————————————————————————————————————————————
namespace e5
{
class MyClass
{
public MyClass(string className, Student[] stu)
{
this.ClassName = className;
this.stu = stu;
}
//班级名称
private string className;
public string ClassName
{
set { className = value; }
get { return className; }
}
//班级的学生(数组)
private Student[] stu;
public Student[] Stu
{
set { stu = value;}
get { return stu; }
}
public void Display()
{
Console.WriteLine("班级是:{0}",className);
}
}
}
————————————————————————————————————————————————————————
namespace e5
{
class Program
{
static void Main(string[] args)
{
Student[] stu = new Student[3]
{
new Student("张江江", 25, "唱歌", Genders.Female, 100),
new Student("周杰杰", 29, "耍耍双截棍", Genders.Male, 101),
new Student("Scofield", 25, "越狱", Genders.Male, 102)
};
//stu[0] = new Student("张江江", 25, "唱歌", Genders.Female, 100);
//stu[1] = new Student("周杰杰", 29, "耍耍双截棍", Genders.Male, 101);
//stu[2] = new Student("Scofield", 25, "越狱", Genders.Male, 102);
MyClass c1 = new MyClass("LGXRDotNet", stu);
c1.Display();
//for (int i = 0; i <c1.Stu.Length; i++)
//{
// c1.Stu[i].SayHi();
//}
Student[] stu2=c1.Stu;
for (int i = 0; i < stu2.Length; i++)
{
Student s = stu2[i];
s.SayHi();
}
Console.ReadKey();
}
}
}
————————————————————————————————————————————————————————————
案例5
namespace e4
{
class Program
{
static void Main(string[] args)
{
int[] array={1,2,3,4,5};
ToEven(array);
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
}
public static void ToEven(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i] = arr[i] * 2;
}
}
}
}