C#语言学习
记录C#的学习,便于在线复习
大大打打
这个作者很懒,什么都没留下…
展开
-
C# 将DataTable转为Excel输出
1. 创建DataTable2. 转为Excel并输出using System;using System.Data;using System.Text;namespace 进阶学习_DataTable转Excel{ class Program { static void Main(string[] args) { //创建一个名为"Table_New"的空表 DataTable dt = ne原创 2022-04-06 11:23:19 · 6576 阅读 · 3 评论 -
C# 学习 委托 Delegate
C# 中的委托可以理解为 Python 中传入函数的引用,经典应用为 闭包和装饰器使用委托分为三步:1. 申明委托类型 ,相当于定义一个函数,只申明了其返回类型和形参2. 修改形式参数,将原本的形参改为刚刚申明的委托3. 传入委托,申明了委托类型还不够,需要实现其功能,相当于定义了一个函数,此时要写它的方法了。class Program { // 申明委托类型,传入一个int参数,返回bool值 delegate bool func(int nu原创 2021-09-17 21:41:33 · 147 阅读 · 0 评论 -
C# 用递归实现查找一个目录下的文件和目录总数
练习: 如果输入的不是路径也会输出 1 。using System;using System.IO;namespace 进阶Csharp01{ class Program { static void Main(string[] args) { // 统计文件总和 static int getAllFiles(string path) { tr.原创 2021-09-13 23:33:49 · 323 阅读 · 0 评论 -
C# 面向对象随笔
3.字段声明访问修饰符 类型 变量名4.方法声明访问修饰符 返回值类型 方法名字(参数列表){//方法体}5.方法调用静态方法(用static 关键字修饰的)用类名.方法名(); 访问非静态方法 (没有static关键字修饰的)用对象名.方法名() 访问访问修饰符 【可选】public 公共的 公开的,在任何地方都可以访问private 私有的,只能在类的内部访问,出了这个类之后,就访问不到了类中的字段和方法默认的修饰符是inter..原创 2021-08-01 20:39:23 · 130 阅读 · 0 评论 -
C# 面向对象09 Dictionary的用法
Dictionary 和 Hashtable 的区别是 Dictionary 只能传入指定好的数据格式, 但是运行效率高using System;using System.Collections.Generic;namespace _012_Dictionary的用法{ class Program { static void Main(string[] args) { // 创建一个字典对象 Dic原创 2021-06-02 21:39:14 · 104 阅读 · 0 评论 -
C# 面向对象08 List的用法
List 的好处就是相比 Arraylist 运行效率更高, 减少了装箱和拆箱的过程。但是只能放入指定的数据。using System;using System.Collections.Generic;namespace _011_List的用法{ class Program { static void Main(string[] args) { // 创建一个 List,里面只能存放 int 数据原创 2021-06-02 21:23:19 · 134 阅读 · 0 评论 -
C# 面向对象07 Hashtable的用法
HashTable: 将数据用键值对的方式存储using System;using System.Collections;namespace _010_HashTable用法{ class Program { static void Main(string[] args) { // 创建一个HashTable的对象 Hashtable dir = new Hashtable();原创 2021-06-02 21:03:34 · 98 阅读 · 0 评论 -
C# 面向对象06 ArrayList的用法
ArrayList 中可以存放所有类型的值using System;using System.Diagnostics;using System.Text;using System.Collections;namespace _09_ArrayList的用法_{ class Program { static void Main(string[] args) { // 创建一个Arraylist数组 .原创 2021-06-01 22:48:15 · 163 阅读 · 0 评论 -
C# 面向对象05 StringBuilder的用法
好处:相比普通的 string处理, 提高了字符串的处理速度注意点:使用时需要使用对象的方式StringBuilder world = new StringBuilder();using System;using System.Diagnostics;using System.Text;namespace _08_StringBuilder{ class Program { static void Main(string[] args) ..原创 2021-06-01 21:11:09 · 244 阅读 · 0 评论 -
C# 面向对象04 自定义一个类
功能:自定义一个怪物类Monster类using System;using System.Collections.Generic;using System.Text;namespace _05自定义一个类{ public class Monster { private string mName; private int mLevel; private int mHp; private int mAttack.原创 2021-05-25 22:19:10 · 289 阅读 · 0 评论 -
C# 面向对象03 构造方法
给 Person 类添加构造方法using System;using System.Collections.Generic;using System.Text;namespace Person类{ class Person { private string _gender; private int _age; // 设置属性,开头大写,只有set和get方法,则不需要 _name 写法 public strin.原创 2021-05-16 21:15:14 · 76 阅读 · 0 评论 -
C# 面向对象03 属性和方法
使用 set 与 get 方法保护数据using System;using System.Collections.Generic;using System.Text;namespace Person类{ class Person { private string _gender; private int _age; // 设置属性,开头大写,只有set和get方法,则不需要 _name 写法 public str原创 2021-05-16 21:04:24 · 106 阅读 · 0 评论 -
C# 面向对象02 类的私有属性
自定义一个 Person 类,将类的 age 改为私有属性using System;using System.Collections.Generic;using System.Text;namespace Private设置类属性{ // class 关键字,自定义类 class Person { // 数据:自定义了五个字段 public string name; // 定义一个私有变量,只能在类的内部访问原创 2021-05-16 20:39:26 · 836 阅读 · 0 评论 -
C# 面向对象01 自定义类与使用
自定义一个 Peerson 类using System;using System.Collections.Generic;using System.Text;namespace Person类{ // class 关键字,自定义类 class Person { // 数据:自定义了五个字段,全部是公有的,任何地方都可以访问 public string name; public int age; publi原创 2021-05-16 20:32:36 · 729 阅读 · 0 评论 -
练习17获取数组中最大的值、练习18-out参数返回登陆信息、练习19斐波那契
练习17获取数组中最大的值using System;namespace 练习17获取数组中最大的值{ class Program { static void Main(string[] args) { int max = Maxnum(new int[]{ 9, 4, 20, 4, 5 }); Console.WriteLine(max); } static int M原创 2021-05-13 23:14:18 · 52 阅读 · 0 评论 -
练习14数组倒序、练习15冒泡排序、练习16返回值
练习14数组倒序using System;namespace 练习14数组倒序{ class Program { // 将12345变成54321 static void Main(string[] args) { int[] tup = { 1, 2, 3, 4, 5 }; string tup1 = ""; for (int i = 1; i <= t原创 2021-05-13 22:59:19 · 106 阅读 · 0 评论 -
练习11九九乘法表、练习12数组比大小、练习13字符串操作
练习11九九乘法表using System;namespace 练习11{ class Program { static void Main(string[] args) { for (int i = 1; i <= 9; i++) { for (int a=1;a<=i;a++) { .原创 2021-05-13 22:54:04 · 76 阅读 · 0 评论 -
练习08随机数应用、练习09记录班级的总成绩和平均成绩while、练习10水仙花数
练习08随机数应用using System;namespace 练习08随机数应用{ class Program { static void Main(string[] args) { Random random1 = new Random(); int num = random1.Next(1, 6); switch (num) { case原创 2021-05-13 22:37:09 · 79 阅读 · 0 评论 -
练习05判断分数、练习06判断闰年、练习07三元运算判断账号
练习05判断分数using System;namespace 判断练习{ class Program { static void Main(string[] args) { Console.WriteLine("请输入数学成绩"); string score_math = Console.ReadLine(); Console.WriteLine("请输入语文成绩");原创 2021-05-13 22:26:28 · 65 阅读 · 0 评论 -
练习01格式化字符串、练习02输入操作01、练习03输入操作02、练习04输入操作03
练习01格式化字符串using System;namespace 练习01{ class Program { static void Main(string[] args) { string name = "大壮"; string gender = "男"; int age = 20; string email = "123@qq.com";原创 2021-05-13 22:16:31 · 77 阅读 · 2 评论 -
022params不定参数、023out参数返回多个返回值、024结构体中的方法
022params不定参数using System;namespace _022params不定参数{ class Program { static void Main(string[] args) { int max = Maxnum("喵喵喵!",9, 4, 20, 4, 5); Console.WriteLine(max); } // 获取一组数中的最大值 .原创 2021-05-13 20:47:55 · 96 阅读 · 0 评论 -
017循环的方式定义数组、018循环for each、019方法练习、020实参和形参、021方法的返回值
017循环的方式定义数组using System;namespace _017循环的方式定义数组{ class Program { static void Main(string[] args) { // 创建一个 10 位 的数组 int[] tup = new int[10]; // 给其后四位位赋值 for (int i = 6; i < t原创 2021-05-13 20:41:21 · 129 阅读 · 0 评论 -
014枚举类型、015结构类型、016数组的存取
014枚举类型using System;namespace _014枚举类型{ // 使用enum关键字申明一个枚举类型,里面有两个值,male和female,使用枚举时只能用其中的值 enum Gender { Male, Female } // 可修改枚举类型 enum QQstate:short { Online, Offline, Away, Hid原创 2021-05-13 20:27:46 · 135 阅读 · 0 评论 -
011循环for、012continue语句、013break语句和return语句
011循环forusing System;namespace _011循环for{ class Program { static void Main(string[] args) { // 求0到100奇数的和 int sum = 0; for (int i = 1 ; i <= 100; i += 2) { su原创 2021-05-13 20:04:10 · 97 阅读 · 0 评论 -
010while循环
using System;namespace _010{ class Program { // 连续跑操场十圈,超出圈数退出 static void Main(string[] args) { int a = 1; while (a <= 10) { Console.WriteLine("我跑了{0}圈。", a); a.原创 2021-05-12 20:38:25 · 58 阅读 · 0 评论 -
009random产生随机数
using System;namespace _008random产生随机数{ class Program { static void Main(string[] args) { Random random = new Random(); int rest = random.Next(1,10); Console.WriteLine("生成一个随机数:"); .原创 2021-05-12 20:37:15 · 46 阅读 · 0 评论 -
008 if-else方法
using System;namespace _008if_else方法{ class Program // 模拟输入账号密码,如果输入正确则提示正确,否则提示错误。账号:123 密码:456 { static void Main(string[] args) { Console.WriteLine("请输入账号"); string count = Console.ReadLine();.原创 2021-05-12 20:35:06 · 51 阅读 · 0 评论 -
007三元运算
using System;namespace _007三元运算{ class Program { // 输入一个数,当大于700返回True,当小于700返回False static void Main(string[] args) { Console.WriteLine("请输入数值:"); double num = Convert.ToDouble(Console.ReadLine().原创 2021-05-12 20:33:34 · 50 阅读 · 0 评论 -
006Bool类型与比较运算
using System;namespace _006Bool类型{ class Program { static void Main(string[] args) { // 比较符号以及Bool类型的使用 int num1 = 20; int num2 = 30; bool a = num1 > num2; bool b = .原创 2021-05-12 20:31:39 · 167 阅读 · 0 评论 -
005一元运算
using System;namespace ConsoleApp2{ class Program { static void Main(string[] args) { // ++num是先赋值再用,num++是先用再赋值 int var1, var2 = 5, var3 = 5; var1 = var2++ * --var3; Console.Write.原创 2021-05-12 20:30:28 · 44 阅读 · 0 评论 -
004 @修饰字符串
using System;namespace _004_修饰字符串{ class Program { static void Main(string[] args) { // 使用“\”时如果不用“\”转义,代码会报错 string myStr = "C:\\software\\Google\\chrome.exe"; // 如果使用了“@”符号修饰,则会原样显示字符串内容,当在其.原创 2021-05-12 20:28:43 · 79 阅读 · 0 评论 -
003变量
using System;namespace _003变量{ class Program { static void Main(string[] args) { byte myByte = 123; // 存储较小的数据,小于255 int myInt = 25535; // 存储较大的数 double myDouble = 3.14159; // 默认的小数变量 .原创 2021-05-12 20:26:41 · 59 阅读 · 0 评论 -
002格式化字符串
using System;namespace _002变量{ class Program { static void Main(string[] args) { string name = "剑圣"; int level = 18; Console.WriteLine("角色为:{0}\n等级为:{1}", name, level);0000000 Cons.原创 2021-05-12 20:24:35 · 43 阅读 · 0 评论 -
001输出
//引入命名空间using System;//{}大括号括起来namespace ConsoleApp1{ //定义了一个类 class Program { //Main方法,程序的入口 #region 这是第一个测试程序 static void Main(string[] args) { //方法体 Console.WriteLine("Hello World.原创 2021-05-12 20:22:00 · 144 阅读 · 0 评论