很久没有学习c#的一些基础知识了,发现自己的桌面上下了一本c#完全手册的书,于是从简入难,再会回顾一下基础知识,温故而知新嘛。
本文纯粹是学习记录,大部分无原创成分,如有侵权请告知。
--- c# 传值:
如果是值参数,swap不能实现换值:
- static void Swap(int x, int y)
- {
- int temp = x;
- x = y;
- y = temp;
- }
- static void Main()
- {
- int i = 1, j = 2;
- Swap(i,j);
- Console.WriteLine("i={0},j={1}",i,j);
- }
- 返回 : i=1,j=2
----c# 传址
引用型参数以ref修饰声明:
- static void Swap(ref int x, ref int y)
- {
- int temp = x;
- x = y;
- y = temp;
- }
- static void Main()
- {
- int i = 1, j = 2;
- Swap(ref i, ref j);
- Console.WriteLine("i={0},j={1}", i, j);
- }
- 返回值: i=2,j=1
---- 输出型参数:
输出型参数加out
- string s;
- void F(ref string a, ref string b)
- {
- s = "1";
- a = "2";
- b = "3";
- }
- void G()
- {
- F(ref s, ref s);
- }
- static void SplitPath(string path, out string dir, out string name)
- {
- int i = path.Length;
- while (i > 0)
- {
- char ch = path[i - 1];
- if (ch == '\\' || ch == '/' || ch == ':') break;
- i--;
- }
- dir = path.Substring(0, i);
- name = path.Substring(i);
- }
- static void Main()
- {
- string dir, name;
- SplitPath("c:\\Temp\\test1.xls", out dir, out name);
- Console.WriteLine(dir);
- Console.WriteLine(name);
- }
--- 数组类型的参数:
args用法; 循环些数组的方法: foreach(int i in args) Console.Write("{0}",i);
- static void F(params int[] args)
- {
- Console.WriteLine("Array contains {0} elements:", args.Length);
- foreach (int i in args) Console.Write("{0}",i);
- Console.WriteLine();
- }
- public static void Main()
- {
- int[] a ={ 1, 2, 3 };
- F(a);
- F(10,20,30,40);
- F();
- }
--- 类的声明和调用例子:
- class Student
- {
- public string s_name;
- public int s_age;
- public float s_weight;
- public Student(string n, int a, float w)
- {
- s_name = n;
- s_age = a;
- s_weight = w;
- }
- public int max_age(int x, int y)
- {
- if (x > y) return x;
- else return y;
- }
- public float max_weight(float x, float y)
- {
- if (x > y) return x;
- else return y;
- }
- }
- class Test
- {
- public static void Main()
- {
- Student s1 = new Student("Mike", 21, 70);
- Student s2 = new Student("John", 21, 70);
- if (s1.max_age(s1.s_age, s2.s_age) == s1.s_age)
- Console.WriteLine("{0}'s age is bigger than {1}'s", s1.s_name, s2.s_name);
- else
- Console.WriteLine("{0}'s age is bigger than {1}'s", s1.s_name, s2.s_name);
- if (s1.max_weight(s1.s_weight, s2.s_weight) == s1.s_weight)
- Console.WriteLine("{0}'s age is weighter than {1}'s", s1.s_name, s2.s_name);
- else
- Console.WriteLine("{0}'s age is weighter than {1}'s", s1.s_name, s2.s_name);
- }
- }
---- 类的声明和调用,还有方法的重载:
- class Student
- {
- public string s_name;
- public int s_age;
- public float s_weight;
- public Student(string n,int a, float w)
- {
- s_name = n;
- s_age = a;
- s_weight = w;
- }
- public float max(float x, float y)
- {
- if (x > y) return x;
- else return y;
- }
- public int max(int x, float y)
- {
- if (x > y) return x;
- else return y;
- }
- }
- class Test
- {
- public static void Main()
- {
- Student s1 = new Student("Mike",21,70);
- Student s2 = new Student("John",21,70);
- if (s1.max(s1.s_age, s2.s_age) == s1.s_age)
- Console.WriteLine("{0}'s age is bigger than {1}'s", s1.s_name, s2.s_name);
- else
- Console.WriteLine("{0}'s age is smaller than {1}'s", s1.s_name, s2.s_name);
- if (s1.max(s1.s_weight, s2.s_weight) == s1.s_weight)
- Console.WriteLine("{0}'s weight is bigger than {1}'s", s1.s_name, s2.s_name);
- else
- Console.WriteLine("{0}'s weight is smaller than {1}'s", s1.s_name, s2.s_name);
- }
- }
---- 构造函数与析构函数;
构造函数是在初始化类和实例的时候第一个开始调用的:
如果缺省,系统自动创建;
- class Vehicle
- {
- public int wheels;
- protected float weight;
- public Vehicle() { ;}
- public Vehicle(int w, float g)
- {
- wheels = w;
- weight = g;
- }
- public void show()
- {
- Console.WriteLine("the wheel of vehicle is :{0}",wheels);
- Console.WriteLine("the weight of vehicle is :{0}",weight);
- }
- }
- class train
- {
- public int num;
- private int passeners;
- private float weight;
- public train(){;}
- public train(int n,int p,float w)
- {
- num = n;
- passeners = p;
- weight = w;
- }
- public void show()
- {
- Console.WriteLine("the number of train is :{0}", num);
- Console.WriteLine("the weight of train is :{0}", weight);
- Console.WriteLine("the number of passenger is :{0}", passeners);
- }
- }
- class car : Vehicle
- {
- int passengers;
- public car(int w, float g, int p)
- : base(w, g)
- {
- wheels = w;
- weight = g;
- passengers = p;
- }
- new public void show()
- {
- Console.WriteLine("the wheel of car is :{0}",wheels);
- Console.WriteLine("the weight of car is :{0}", weight);
- Console.WriteLine("the passengers of car is :{0}", passengers);
- }
- }
- class Test
- {
- public static void Main()
- {
- Vehicle v1 = new Vehicle(4, 5);
- train t1 = new train();
- train t2 = new train(10, 100, 100);
- car c1 = new car(4, 2, 4);
- v1.show();
- t1.show();
- t2.show();
- c1.show();
- }
- }
--- 编辑操作符:
作用就是定义一种操作符来批量修改类里面的属性.
- class player
- {
- public int neili;
- public int tili;
- public int jingyan;
- public int neili_r;
- public int tili_r;
- public player()
- {
- neili = 10;
- tili = 50;
- jingyan = 0;
- neili_r = 50;
- tili_r = 50;
- }
- public static player operator ++(player p)
- {
- p.neili = p.neili + 50;
- p.tili = p.tili + 100;
- p.neili_r = p.neili;
- p.tili_r = p.tili;
- return p;
- }
- public void show()
- {
- Console.WriteLine("Tili:{0}", tili);
- Console.WriteLine("Jingyan:{0}", jingyan);
- Console.WriteLine("Neili:{0}", neili);
- Console.WriteLine("Tili_full:{0}", tili_r);
- Console.WriteLine("Neili_full:{0}", neili_r);
- }
- class Test
- {
- public static void Main() {
- player man = new player();
- man.show();
- man++;
- Console.WriteLine("Now upgrading ....");
- man.show();
- }
- }
- }
---- 杨辉三角,用二维数组实现:
- static void Main(string[] args)
- {
- int[,] a = new int[5, 5];
- a[0, 0] = 1;
- for (int i = 1; i < 5; i++)
- {
- a[i, 0] = 1;
- a[i, i] = 1;
- for (int j = 1; j < i; j++)
- {
- a[i, j] = a[i - 1, j - 1] + a[i - 1, j];
- }
- }
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j <= i; j++)
- {
- Console.Write("{0}",a[i,j]);
- }
- Console.WriteLine();
- }
- }
输出结果如下:
1
11
121
1331
14641
+++ 静态类和非静态类区别:
静态域申明: static修饰符,不加则为非静态域; 静态域和非静态域分数静态变量与非静态变量; 静态域无论建立多少该类的实例,内存中只有一个静态数据拷贝,所以当这个的第一个实例建立时,域被初始化,以后在创建新对象时就不需要再做初始化了. 非晶态类实例化相反,每次都要创建一份单独的拷贝。
转载于:https://blog.51cto.com/mengya520/843605