#region//引入命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
#endregion
#region//命名规则
//变量命名 在简单的循环语句中计数器变量使用 i, j, k, l, m, n
// Camel 命名规则,即第一个单词首字母小写,其余单词首字母大写
//属性命名 命名名称应该为名词及名词短语,使用Pascal规则,即每个单词首字母大写
//枚举命名 对于 Enum 类型和值名称使用 Pascal 大小写。少用缩进,
//不要在 Enum 类型名称上使用 Enum 后缀。
//对大多数 Enum 类型使用单数名称,但是对作为位域的 Enum 类型使用复数名称。
//方法命名 对方法名采用一致的动词/宾语或宾语/动词顺序。
//推荐名称应该为动词或动词短语,使用Pascal规则
//集合命名 使用Pascal规则。名称应该为名词及名词短语
//名称后面追加“Collection”或者加复数形式-s也行
//接口命名 接口名称应该为名词及名词短语或者描述其行为的形容词,尽可能使用完整的词
//使用字符I为前缀,并紧跟一个大写字母,使用Pascal规则
//类命名 为名词及名词短语,尽可能使用完整的词,使用Pascal规则
//不要使用类前缀 - 不要使用下划线字符 (_)。
//使用复合单词命名派生的类。派生类名称的第二个部分应当是基类的名称
//创建类时不要与内置类名相同,以免混淆
#endregion
#region//命名空间
namespace ConsoleApp31
{
#region//抽象类 1 2
//抽象使用关键字 abstract
//抽象类不能实例化,抽象方法也不能直接实现,必须在抽象的派生类中重写
//如果类包含抽象方法,这个类就必须是抽象类
//继承抽象类时,需要实现所有抽象成员
#region//抽象类以及抽象方法
public abstract class MyClass1//抽象类
{
public abstract void OutPutX(string x);//抽象方法输出输入的字符串x
//因为是抽象方法,所以不能对他实现。
//抽象方法必须在非抽象的派生类中重写。
}
#endregion
#region//抽象类继承以及抽象方法重写
public class MyClass2 : MyClass1//派生类继承抽象类
//继承抽象类必须实现抽象类中所有抽象成员
{
public override void OutPutX(string x)//实现抽象基类的派生方法
//对抽象方法重写,使用 override 关键字
{
WriteLine(x);
}
}
#endregion
#endregion
#region//普通类 3
//类可以包含许多元素
//常见的有字段、属性、方法、构造函数
public class MyClass3
{
#region//字段
//定义三个私有字段,
private int studentId;//学生的id,
private string studentName;//学生的名字
private int studentAge;//学生的年龄
#endregion
#region//属性
public int StudentId//生成学生id的属性
//通过公有属性访问私有字段
//属性必须有get访问器,用于读,set访问器可以省略,即只读不写
{
get { return studentId; }//get访问器,用于读,返回私有学生id
set { studentId = value; }//set访问器,用于写。
}
public String StudentName//生成学生姓名的属性
{
get { return studentName; }
set { studentName = value; }
}
public int StudentAge//生成学生年龄的属性
{
get { return studentAge; }
set//对于写输入做判断,如果大于大于0,正常输入,如果小于0,设置为0
{
if (value >= 0)
{
studentAge = value;
}
else
{
studentAge = 0;
}
}
}
#endregion
#region//构造函数
//构造函数用于初始化赋值
//如果不写会生成默认的构造函数
//参数可有可无,但是要注意实例化时要根据构造函数是否有参数决定是否传参
//如果手动写构造函数,必须对所有字段赋值。
public MyClass3(int StudentId, String StudentName, int StudentAge)
{
this.studentId = StudentId;
this.studentName = StudentName;
this.studentAge = StudentAge;
}
#endregion
#region//方法
//静态方法使用关键字 static
//静态方法属于类中所有元素
//调用静态方法通过类调用
public static void OutPutMyClass3AllVariable(params MyClass3[] a)//定义静态方法,输出类中实例的所有字段
{
foreach (MyClass3 i in a)
{
WriteLine("{0} {1} {2}", i.StudentId, i.StudentName, i.StudentAge);
WriteLine("**************");
}
}
//实例方法,实例方法属于类的每个实例
//调用实例方法通过实例调用
public void OutPutMyClass3Variable()//定义实例方法,输出实例的所有字段
{
WriteLine("{0} {1} {2}",StudentId,StudentName,StudentAge);
}
#endregion
}
#endregion
#region//方法重载 4
//方法重载即方法名相同,但是方法的参数个数或数据类型不同
//只能根据参数来判定选择使用哪个方法,与方法内容返回类型无关。
public class MyClass4
{
public void MyMethod()
{
WriteLine("无参数无返回值");
}
public void MyMethod(int x)
{
WriteLine("有一个整形参数无返回值");
}
public void MyMethod(string s)
{
WriteLine("有一个字符串参数无返回值");
}
public void MyMethod(int x,int y)
{
WriteLine("有两个整形参数无返回值");
}
public void MyMethod(int x,string s)
{
WriteLine("一个整形参数,一个字符串参数无返回值");
}
public void MyMethod(string s,int x)
{
WriteLine("一个字符串参数一个整形参数参数无返回值");
}
}
#endregion
#region//方法重写与隐藏方法 5 6
public class MyClass5
{
public virtual void EmptyMethod()//使用virtual标注一个虚方法,可以在派生类中重写
{
WriteLine("这是一个基类虚方法");
}
public void OrdinaryMethod()
{
WriteLine("这是一个普通的基类方法");
}
//存在以下object方法可以被重写
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return base.ToString();
}
}
public class MyClass6 : MyClass5
{
public override void EmptyMethod()//重写虚方法需要使用 override 关键字
{
WriteLine("这是基类虚方法重写");
}
new public void OrdinaryMethod()
{
WriteLine("对基类方法隐藏");
}
}
#endregion
#region//派生类的构造函数 7 8
public class MyClass7
{
public int ID;
public string name;
public MyClass7(int ID, string name)//基类构造函数
{
this.ID = ID;
this.name = name;
}
}
public class MyClass8:MyClass7
{
public MyClass8(int ID,string name)://派生类构造函数
base(ID,name)
{
}
}
#endregion
#region//接口 9
public interface IOutPut//定义一个接口
{
void OutPut(string str);//接口方法,不用实现
}
public class MyClass9 : IOutPut//继承接口,必须实现接口的方法
{
public void OutPut(string str)//对接口方法的实现
//可以继承多个接口
//实现接口方法时方法的签名不能改变
{
WriteLine(str);
}
}
#endregion
#region//泛型 10 11 12 13
#region//泛型接口
public interface IOutPut<T>//泛型接口
{
void OutPut(T x);
}
#endregion
#region//泛型类
public class MyClass10<T1,T2>//泛型类
{
public T1 studentId;
public T2 studentName;
public T1 studentAge;
public MyClass10(T1 studentId, T2 studentName, T1 studentAge)
{
this.studentId = studentId;
this.studentName = studentName;
this.studentAge = studentAge;
}
}
#endregion
#region//泛型类,泛型接口继承
public class MyClass11<T1,T2,T>:MyClass10<T1,T2>, IOutPut<T>//泛型类继承
//继承泛型类,继承泛型接口
{
public MyClass11(T1 studentId, T2 studentName, T1 studentAge)://对于基类有构造函数的派生类需要生成派生类
base(studentId, studentName, studentAge)
{
}
#region//泛型接口方法实现
public void OutPut(T x)//对继承接口的方法实现
{
WriteLine(x);
}
#endregion
}
public class MyClass12:MyClass10<int,string>, IOutPut<string>//非泛型类也可以继承泛型类,泛型接口
//继承时类必须在接口前面
{
public MyClass12(int studentId, string studentName, int studentAge) ://对于基类有构造函数的派生类需要生成派生类
base(studentId, studentName, studentAge)
{
}
public void OutPut(string x)
{
WriteLine(x);
}
}
#endregion
#region//泛型约束
// sturct 对于结构约束,泛型必须是值类型
// class 类约束指定类型T必须是引用类型
//IFoo 指定类型T必须实现接口IFoo
//Foo 指定类型T必须派生自基类Foo
//new() 这个是一个构造函数约束,指定类型T必须有一个默认构造函数
//T2 这个约束也可以指定,类型T1派生自泛型类型T2
public class MyClass13<T>
where T:struct//约束泛型必须是值类型
//可以同时又多个约束,用逗号分隔。
{
public T x;
public static void OutPutX(T x)
{
WriteLine(x);
}
}
#endregion
#region//协变抗变
//如果泛型类用 out 关键字标注,那么泛型接口就是协变的
//返回值类型只能是T
public interface ISquare<out T>
{
T Square(int x);
}
//如果泛型类型使用 in 关键字标注,那么泛型接口就是抗变的
//接口只能将泛型类型T用作其方法的输入
public interface Iresist<in T>
{
void show(T x);
}
#endregion
#endregion
#region//数组 14
public class MyClass14<T>
{
public static void ShowArrayAll(params T[] x)//创建一个输出所有数组元素的方法
{
foreach (var i in x)
{
WriteLine(i);
}
}
public static void ShowTwoArrayAll(T[,] x)//创建一个输出所有二维数组元素的方法
//二维数组不需要 params
{
for (int i = 0; i < x.GetLength(0); i++)
{
for (int j = 0; j < x.GetLength(1); j++)
{
Write("{0} ", x[i, j]);
}
WriteLine();
}
}
public static void ShowSawtoothArrayAll(T[][] x)//输出锯齿数组中所有元素
{
for (int i = 0; i < x.Length; i++)
{
for (int j = 0; j < x[i].Length; j++)
{
Write("{0} ", x[i][j]);
}
WriteLine();
}
}
}
#endregion
#region//枚举 15
public class MyClass15
{
public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
//枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。
//枚举是值类型。
//枚举包含自己的值,且不能继承或传递继承。
public enum Color
{
red=100,//可以直接对枚举中元素赋值,
greed,//未赋值元素依据上一个元素的值自增
blue = 50//手动赋值的情况可以忽略从上到下的大小关系,但是不建议这样
}
}
#endregion
#region//运算符重载 16
public class MyClass16
{
public int length;//长
public int breadth;//宽
public static MyClass16 operator +(MyClass16 a, MyClass16 b)
//对运算符重写需要使用 operator 关键字
//后面加上要重写的运算符
{
//这里传入两个实例,将实例的每个字段相加,然后返回属性
MyClass16 x = new MyClass16();
x.length = a.length + b.length;
x.breadth = a.breadth + b.breadth;
return x;
}
}
#endregion
#region//委托 17
//委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。
//使用 delegate 关键字声明委托
//一旦声明了委托类型,委托对象必须使用 new 关键字来创建,且与一个特定的方法有关。
public class MyClass17
{
#region//用于普通委托的方法
public static int F1(int x)//随便写两个方法
{
return x;
}
public static int F2(int x)
{
return x - 1;
}
#endregion
#region//用于多播委托的方法
public static void F3()//随便写两个方法
{
WriteLine("这是第一个委托");
}
public static void F4()
{
WriteLine("这是第二个委托");
}
#endregion
}
public delegate int WT(int x);//声明一个委托,规定返回类型,需要的参数
public delegate void WT1();//这个委托一会用于演示多播委托
public delegate void WT2(string str);//用于创建匿名方法
#endregion
#region//lambda表达式 18
public class MyClas18
{
//=>左边方法,右边方法体,不需要return
public static int F1(int x) => x * x;//一个简单的lambda表达式
public static void F2(int x) => WriteLine(x);
}
#endregion
#region//linq 19
public class MyClass19
{
public int id;
public int mark;
public static List<MyClass19> Establish()
{
var listlinq = new List<MyClass19>();
Random r = new Random();
for (int i = 0; i < 100; i++)
{
MyClass19 x = new MyClass19();
x.id = i;
x.mark = r.Next(0, 100);
listlinq.Add(x);
}
return listlinq;
}
public static void PutOutAllList(List<MyClass19> listlinq)//输出列表
{
foreach(var i in listlinq)
{
WriteLine("ID是 {0} 分数是{1}",i.id,i.mark);
}
}
}
#endregion
#region//主函数类
class Program
{
#region//入口函数
static void Main(string[] args)
{
#region//对MyClass3实例化操作
MyClass3 lihua = new MyClass3(100,"lihua",23);
lihua.OutPutMyClass3Variable();
MyClass3 yb = new MyClass3(1614,"yb",22);
MyClass3[] a = new MyClass3[] { lihua,yb };
WriteLine("----------------------------");
MyClass3.OutPutMyClass3AllVariable(a);
#endregion
#region//MyClass4方法重载的检验
WriteLine("----------------------------------");
WriteLine("对MyClass4方法重载的检验");
MyClass4 v = new MyClass4();//因为都是实例方法,所以需要先实例化
v.MyMethod();
v.MyMethod(777);
v.MyMethod("asdw");
v.MyMethod(777,888);
v.MyMethod(777,"asdw");
v.MyMethod("asdw",777);
#endregion
#region//MyClass5,MyClass6虚方法重写,隐藏基类方法
WriteLine("---------------------");
WriteLine("对MyClass5,MyClass6虚方法重写,隐藏基类方法检测");
MyClass5 c5 = new MyClass5();
MyClass6 c6 = new MyClass6();
c5.EmptyMethod();
c6.EmptyMethod();
c5.OrdinaryMethod();
c6.OrdinaryMethod();
#endregion
#region//MyClass7,MyClass8派生类构造函数检验
WriteLine("-----------------------");
WriteLine("MyClass7,MyClass8派生类构造函数检验");
MyClass7 c7 = new MyClass7(777,"asdw");
MyClass8 c8 = new MyClass8(888,"qwe");
WriteLine("{0} {1}", c7.ID, c7.name);
WriteLine("{0} {1}", c8.ID, c8.name);
#endregion
#region//MyClass9接口检测
MyClass9 c9 = new MyClass9();
c9.OutPut("实现接口方法");
#endregion
#region//泛型检测 10 11 12 13
MyClass10<int, string> c10 = new MyClass10<int, string>(101, "qwe",10);
MyClass11<int, string,string> c11 = new MyClass11<int, string,string>(102, "asdw", 25);
WriteLine("{0} {1} {2}",c10.studentId,c10.studentName,c10.studentAge);
WriteLine("{0} {1} {2}", c11.studentId, c11.studentName, c11.studentAge);
c11.OutPut("这是一个派生接口的方法实现");
MyClass12 c12 = new MyClass12(102,"zxc",789);
c12.OutPut("这是非泛型类继承泛型接口");
MyClass13<int>.OutPutX(777);//必须是值类型
#endregion
#region//数组 14
#region//一维数组
WriteLine("一维数组");
WriteLine("---------------------");
int[] myArray1 = new int[4];//声明一个数组长度为4的整形一维数组
myArray1[0] = 100;
myArray1[1] = 777;
myArray1[2] = 888;
myArray1[3] = 1234;
MyClass14<int>.ShowArrayAll(myArray1);//调用方法输出数组的所有元素
int[] myArray2 = { 1,2,3,4,5};//声明数组的另一种方式
MyClass14<int>.ShowArrayAll(myArray2);
WriteLine(myArray1.Length);//输出数组的长度
#endregion
#region//多维数组
int[,] myArray3 = new int[2, 3];//创建一个两行三列的数组
WriteLine("二维数组");
WriteLine("---------------------------");
for (int i = 0; i<myArray3.GetLength(0); i++)
{
for (int j = 0; j < myArray3.GetLength(1); j++)
{
//WriteLine("{0} {1}", i,j);
myArray3[i, j] = i + j;
}
}
MyClass14<int>.ShowTwoArrayAll(myArray3);
WriteLine("-----------------------");
int[,] myArray4 = new int[2, 3] { { 4,5,6},{7,8,9 } };//同样可以初始化时直接赋值
MyClass14<int>.ShowTwoArrayAll(myArray4);
#endregion
#region//锯齿数组
WriteLine("锯齿数组");
WriteLine("-------------------");
int[][] myArray5 = new int[3][];//锯齿数组在创建的时候必须规定该数组最外层行数
myArray5[0] = new int[]{ 1,3};
myArray5[1] = new int[] { 5,6,7,8 };
myArray5[2] = new int[] { 777 };
MyClass14<int>.ShowSawtoothArrayAll(myArray5);
#endregion
#endregion
#region//枚举检测 15
WriteLine("------------------------");
WriteLine("枚举检测");
int d = (int)MyClass15.Day.Tue;//枚举类表中每个整形符号代表一个整形值,从0开始
WriteLine(d);
int c1 = (int)MyClass15.Color.blue;
WriteLine(c1);
#endregion
#region//运算符重载检测 16
WriteLine("---------------");
WriteLine("运算符重载检测");
MyClass16 aa = new MyClass16();
MyClass16 bb = new MyClass16();
aa.length = 100;
aa.breadth = 50;
bb.length = 77;
bb.breadth = 40;
MyClass16 xx = aa + bb;//用一个新的实例接收返回值
WriteLine("{0} {1}",xx.length,xx.breadth);
#endregion
#region//类型转换示例
WriteLine("------------------");
WriteLine("类型转换示例");
//类型转换分为两种,隐式类型转换和显示类型转换
//隐式类型转换,从小的整数类型转换为大的整数类型,从派生类转换为基类。
//只要保证数据不会丢失
int a1 = 100;
long a2 = a1;
//int a3 = a2;会报错,只能大的转换为小的
WriteLine("{0} {1}",a1.GetType(),a2.GetType());
//还有另一种是显示类型转换
//即强制类型转换。显式转换需要强制转换运算符,而且强制转换会造成数据丢失。
long b1 = 200;
int b2 = (int)b1;//显示类似转换用括号
WriteLine("{0} {1}", b1.GetType(), b2.GetType());
#endregion
#region//委托 17
WriteLine("------------------");
WriteLine("委托实践");
#region//普通委托
//一旦声明了委托类型,委托对象必须使用 new 关键字来创建,且与一个特定的方法有关。
//实例化委托
WT w1 = new WT(MyClass17.F1);
WT w2 = new WT(MyClass17.F2);
WriteLine("{0} {1}",w1(10),w2(100));
#endregion
#region//多播委托
//多播委托
//使用 + 将多个委托连接
//按照顺序调用多播委托,委托的签名必须的void,否则只返回最后一个
WT1 w3 = new WT1(MyClass17.F3);
WT1 w4 = new WT1(MyClass17.F4);
WT1 ww;
ww = w3 + w4;
ww();//调用多播委托
#endregion
#region//匿名方法
//提供了一种传递代码块作为委托参数的技术。
//匿名方法是没有名称只有主体的方法。
//在匿名方法中不需要指定返回类型,它是从方法主体内的 return 语句推断的。
WT2 www = delegate (string str)
{
WriteLine("这是匿名方法 {0}",str);
};
www("asdw");
#endregion
#endregion
#region//集合
WriteLine("---------------");
WriteLine("集合检测");
#region//列表
var list1 = new List<int>();
list1.Add(0);
list1.Add(10);
list1.Add(5);
foreach(var i in list1)
{
WriteLine(i);
}
WriteLine("-------------");
#endregion
#region//队列 先进先出
var list2 = new Queue();
list2.Enqueue(1);
list2.Enqueue("asdw");
list2.Enqueue(7777);
foreach(var i in list2)
{
WriteLine(i);
}
#endregion
#region//栈堆 后进先出
WriteLine("-----------------");
var list3 = new Stack();
list3.Push(75);
list3.Push("qwe");
list3.Push(999999999999);
foreach (var i in list3)
{
WriteLine(i);
}
#endregion
#endregion
#region//lambda 18
WriteLine("------------------");
WriteLine("lambda表达式");
int ss =MyClas18.F1(10);
WriteLine(ss);
MyClas18.F2(7777);
#endregion
#region//linq 19 看另一篇文章吧[LINQ](//www.greatytc.com/writer#/notebooks/46905221/notes/75179989/preview)
//from 查询表达式必须以 from 子句开头 代表从
//如 from i in asdw asdw是要操作的目标,i是asdw的每个对象
//where 限定条件 用于指定将在查询表达式中返回数据源中的哪些元素。
//select 查询 指定在执行查询时产生的值的类型。
//group
WriteLine("------------------------");
WriteLine("LINQ实践");
#region//创建数据
var listlinq = MyClass19.Establish();
MyClass19.PutOutAllList(listlinq);
#endregion
#region//查询数据
var s = from i in listlinq
where ((i.mark > 95 || i.mark < 10) && i.id >50)
orderby i.mark descending//依照分数排序 ascending正序,descending倒序
select i;
WriteLine("**********************************");
List<MyClass19> evenlist;
evenlist = s.ToList<MyClass19>();//将查询结果隐式转换为MyClass19类型的列表
MyClass19.PutOutAllList(evenlist);//输出这个列表
#endregion
#endregion
ReadKey();
}
#endregion
}
#endregion
}
#endregion
C#的基础语法示例还算是全吧
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...