/*
思考:从人类的视角,从上帝的视角,从整体方面,从细节方面
1,实例化对象来考虑这个需求。
2,画或者数据模仿要实现的功能或需求。
2,在数据处理过程中数据的角度和各个条件。
3,要实现数据处理,是使用什么样的逻辑部分。
*/
https://item.taobao.com/item.htm?id=670484418437&ns=1&pisk=gZQxnacAp820khlr7AZkjLZ-jcNlDNC4nt5ISdvmfTBR1L0ggEqwWOpRQKxDlK09W1BNIOCagC91COpGmu4hgs8w5J0T-yf2ETrHEOKXh3a5aCusfyDAGElX5J23W0cW0-Y1jDYIQ0iWTLOXGVgbwUOM9mT6fI17wCOihcgfC_NJUCRXCV9bP4OeGAT61q1WNIRI5Ig6CLMWUCTs5hGzTPJShdgOb_2Euhcz2qgfyIKvBVv-WCQj-nJQxLutWKLDDmfXeVg1ygkzu-JbYbvwoK5XMOzEuEtOY1AOHr3fB67PeQ6QW2vfa6jH0ZZi-HLJHhbXJfafwEpvAZxKGqXCw1sH2aVQ8ELXhGYVsXecwZBcgaCi10TvogKdkeaEhdjl9NdO7RzRp67PeQ6QW-sPDwbdsaMhl7ibw7nZbnOreNkoHJdC1ERJKSeZbc-uUQp3w7nZbnOywpVlPcowq85..&priceTId=213e369517274429087616084ee97e&spm=a21n57.1.p4pright.15.514a523clf64ZH&utparam=%7B%22aplus_abtest%22%3A%22095aee122711b55c135d5c2a9e4bd6e6%22%7D&xxc=ad_ztc&skuId=4998354767033
微软开发指南:
https://learn.microsoft.com/zh-cn/dotnet/csharp/how-to/
C#上机位开发学习路线图:
C# 教程
Winform视频教程
wpf基础
wpf进阶
上位机通信协议
C#上位机开发串口通信编程(22讲)
C#串口通信测控应用实例
C#串口通信实例程序技术资料源码视频教程PC单片机PLC例程实例
Socket课程
基础学习:
《c#从入门到精通》第4版
====================================================================================
以下是第2次学习文档
====================================================================================
第1篇基础知识
第2篇核心技术
第3篇高级应用
第4篇项目实际
//========第1篇基础知识
//====第1章认识c#开发环境
Visual Studio 2022 新建项目
//====第2章
//=2.1 第一个c#程序
demo1:
using System;
namespace Test1 {
class Prages {
static void Main(string[] args) {
Console.Write("输出第一个代码");
Console.WriteLine();
}
}
}
demo2:
using System; //引入命名空间
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test01 //Test01 命名空间
{
class Program //类名
{
static void Main(string[] args) //静态方法 接收命令行传入的参数。 args 字符串数组
{
Console.WriteLine("Hello World!"); //输出“Hello World!”
Console.ReadLine(); //固定屏幕
}
}
}
2.2 类
类修饰符 class 类名 基类或接口
class name{
}
2.3Mian方法
注:只有一个 Main方法。
方法修饰符:
public 共有的
static 静态的
void 无返回值
namespace Test01 //Test01 命名空间
{
class Program //类名
{
static void Main(string[] args) //静态方法 接收命令行传入的参数。 args 字符串数组
{
Console.WriteLine("Hello World!"); //输出“Hello World!”
Console.ReadLine(); //固定屏幕
}
}
}
2.4标识符和关键字
标识符:
与其它语言一样,正常写就可以。
关键字:
系统关键字与其它的语言差不多。
2.5c#语句
Console.WriteLine();
2.6注释
//
/**/
//====第3章
3.2.1 声明变量
int num;
string str1,str2,str3;
3.2.2 变量赋值
注:与其它的语言一样。
int num sum;
num =1;
sum =2;
3.2.3 作用域
1,成员变量,在类中定义的变量
class name {
int num;
static int y = 90;
}
2,局部变量,在类的方法体中定义的变量。
注:在定义局部变量的时,要对其进行初始化。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test07
{
class Program
{
static void Main(string[] args)
{
//调用for语句循环输出数字
for (int i = 0; i <= 20; i++) //for循环内的局部变量i
{
Console.WriteLine(i.ToString()); //输出0~20的数字
}
Console.ReadLine(); //固定屏幕
}
}
}
3.3.1 值 类型
值类型,引用类型,枚举类型
//值类型
1,整数型
int num;
2,浮点型
float a;
3,布尔型
bool a = true;
using System;
namespace Test1 {
class Mame {
static void Main(string[] args) {
bool a = true;
int b = 10;
float c = 0.111f;
double d = 112d;
Console.WriteLine("输出内容");
Console.ReadLine();
}
}
//引用类型
类,接口,数组,委托!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test02
{
class Program
{
class C //创建一个类C
{
public int Value;//声明一个公共int类型的变量Value
}
static void Main(string[] args)
{
int v1 = 0; //声明一个int类型的变量v1,并初始化为0
int v2 = v1;//声明一个int类型的变量v2,并将v1赋值给v2
v2 = 927;//重新将变量v2赋值为927
C r1 = new C(); //使用new关键字创建引用对象
C r2 = r1;//使r1等于r2
r2.Value = 112; //设置变量r2的Value值
Console.WriteLine("Values:{0},{1}", v1, v2);//输出变量v1和v2
Console.WriteLine("Refs:{0},{1}", r1.Value, r2.Value);//输出引用类型对象的Value值
Console.ReadLine();
}
}
}
//值类型和引用类型的区别
值类型存储其值
引用类型存储其值的引用
//枚举类型(或写在类中)
用于生成一组相同性质的常量
enum MyDate //使用enum创建枚举
{
Sun = 0, //设置枚举值名称Sun,枚举值为0
Mon = 1, //设置枚举值名称Mon,枚举值为1
Tue = 2, //设置枚举值名称Tue,枚举值为2
Wed = 3, //设置枚举值名称Wed,枚举值为3
Thi = 4, //设置枚举值名称Thi,枚举值为4
Fri = 5, //设置枚举值名称Fri,枚举值为5
Sat = 6 //设置枚举值名称Sat,枚举值为6
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test03
{
class Program
{
enum MyDate //使用enum创建枚举
{
Sun = 0, //设置枚举值名称Sun,枚举值为0
Mon = 1, //设置枚举值名称Mon,枚举值为1
Tue = 2, //设置枚举值名称Tue,枚举值为2
Wed = 3, //设置枚举值名称Wed,枚举值为3
Thi = 4, //设置枚举值名称Thi,枚举值为4
Fri = 5, //设置枚举值名称Fri,枚举值为5
Sat = 6 //设置枚举值名称Sat,枚举值为6
}
static void Main(string[] args)
{
int k = (int)DateTime.Now.DayOfWeek; //获取代表星期几的返回值
switch (k)
{
//如果k等于枚举变量MyDate中的Sun的枚举值,则输出今天是星期日
case (int)MyDate.Sun: Console.WriteLine("今天是星期日"); break;
//如果k等于枚举变量MyDate中的Mon的枚举值,则输出今天是星期一
case (int)MyDate.Mon: Console.WriteLine("今天是星期一"); break;
//如果k等于枚举变量MyDate中的Tue的枚举值,则输出今天是星期二
case (int)MyDate.Tue: Console.WriteLine("今天是星期二"); break;
//如果k等于枚举变量MyDate中的Wed的枚举值,则输出今天是星期三
case (int)MyDate.Wed: Console.WriteLine("今天是星期三"); break;
//如果k等于枚举变量MyDate中的Thi的枚举值,则输出今天是星期四
case (int)MyDate.Thi: Console.WriteLine("今天是星期四"); break;
//如果k等于枚举变量MyDate中的Fri的枚举值,则输出今天是星期五
case (int)MyDate.Fri: Console.WriteLine("今天是星期五"); break;
//如果k等于枚举变量MyDate中的Sat的枚举值,则输出今天是星期六
case (int)MyDate.Sat: Console.WriteLine("今天是星期六"); break;
}
Console.ReadLine();
}
}
}
switch(){
case a:
console.writeLine();
break;
case b:
console.writeLine();
break;
case c:
consoe.writeLine();
}
3.3.5 类型转换
1,隐式转换
2,显式转换
3,装箱和拆箱
1,隐式转换
int i = 1;
long a =i;
2,显式转换(强制转换)
double x = 1.454645665465;
int a =(int)x;
3,装箱和拆箱
装箱:将值类型转换为引用类型的叫装箱
拆箱:将引用类型转为值类型的叫拆箱
装箱:将值类型转换为引用类型的叫装箱
int i = 2008; //声明一个int类型变量i,并初始化为2008
object obj = i; //声明一个object类型obj,其初始化值为i
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test05
{
class Program
{
static void Main(string[] args)
{
int i = 2008; //声明一个int类型变量i,并初始化为2008
object obj = i; //声明一个object类型obj,其初始化值为i
Console.WriteLine("1、i的值为{0},装箱之后的对象为{1}", i, obj);
i = 927; //重新将I赋值为927
Console.WriteLine("2、i的值为{0},装箱之后的对象为{1}", i, obj);
Console.ReadLine();
}
}
}
拆箱:将引用类型转为值类型的叫拆箱
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test06
{
class Program
{
static void Main(string[] args)
{
int i = 112; //声明一个int类型的变量i,并初始化为112
object obj = i; //执行装箱操作
Console.WriteLine("装箱操作:值为{0},装箱之后对象为{1}", i, obj);
int j = (int)obj; //执行拆箱操作
Console.WriteLine("拆箱操作:装箱对象为{0},值为{1}", obj, j);
Console.ReadLine();
}
}
}
//=3.4常量
其值固定不变的值
const string CHA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test08
{
class Program
{
static void Main(string[] args)
{
int MyInt = 927; //声明一个整型变量
const int MyWInt = 112; //声明一个整型常量
Console.WriteLine("变量MyInt={0}", MyInt); //输出
Console.WriteLine("常量MyWInt={0}", MyWInt); //输出
MyInt = 1039; //重新将变量赋值为1039
Console.WriteLine("变量MyInt={0}", MyInt); //输出
Console.ReadLine();
}
}
}
//====第4章
//=4.1表达式
与其他语言没有区别
//=4.2运算符
与其他语言没有区别
4.2.4逻辑运算符
&& & 与和
|| 或
! 非
4.2.5 位运算符
与其他语言没有区别
4.2.6 其他特殊运算符
1,is (检查变量是否指定类型)
2,条件运算符 (三元运算 ?:)
3,new运算符
4,typeof运算符
1,is (检查变量是否指定类型)
int a = 1;
bool tr = a is int;
2,条件运算符
条件式? 结果1:结果2;
bool isleapyear = ((year % 400) == 0) || (((year % 4) == 0) && ((year % 100) != 0));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test23
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个年份:"); //屏幕输入提示字符串
string str = Console.ReadLine(); //获取用户输入的年份
int year = Int32.Parse(str); //将输入的年份转换成32位 int类型
//计算输入的年份是否为闰年
bool isleapyear = ((year % 400) == 0) || (((year % 4) == 0) && ((year % 100) != 0));
//利用条件运算符输入“是”或者“不是”
string yesno = isleapyear ? "是" : "不是";
Console.WriteLine("{0}年{1}闰年", year, yesno); //输出结果
Console.ReadLine();
}
}
}
3,new运算符
对象创建表达式
数组创建表达式
代表创建表达式
string str = new string[5];
str[0] = 1;
............
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test24
{
class Program
{
static void Main(string[] args)
{
string[] ls = new string[5]; //创建具有5个项目的string类型数组
ls[0] = "ls1"; //为数组第一项赋值
ls[1] = "ls2"; //为数组第二项赋值
ls[2] = "ls3"; //为数组第三项赋值
ls[3] = "ls4"; //为数组第四项赋值
ls[4] = "ls5"; //为数组第五项赋值
Console.WriteLine(ls[0]); //输出数组第一项
Console.WriteLine(ls[1]); //输出数组第二项
Console.WriteLine(ls[2]); //输出数组第三项
Console.WriteLine(ls[3]); //输出数组第四项
Console.WriteLine(ls[4]); //输出数组第五项
Console.ReadLine();
}
}
}
4,typeof运算符
获取系经原型对象的类型
Type mytype = typeof(string);
typeof(string); 获取的是 Type 类型。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test25
{
class Program
{
static void Main(string[] args)
{
Type mytype = typeof(string); //获取引用类型的信息
Console.WriteLine("类型:{0}", mytype); //输出结果
Console.ReadLine();
}
}
}
//=4.3 运算符优先级
与其它的语言基本相同。
//====第5章
//=5.1 字符 Char类 的使用
5.1.1 Char类的概述
占用16位,2个字节的内存空间。使用单引号。''
Char st = 'a';
只定义一个unicode 字符,目前电脑通用的字符编码。二进制的编码。
5.1.2 Char类的使用。
使用时,可以使用Is 提示为判断
To 为转换。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test01
{
class Program
{
static void Main(string[] args)
{
char a = 'a'; //声明字符a
char b = '8'; //声明字符b
char c = 'L'; //声明字符c
char d = '.'; //声明字符d
char e = '|'; //声明字符e
char f = ' '; //声明字符f
//使用IsLetter方法判断a是否为字母
Console.WriteLine("IsLetter方法判断a是否为字母:{0}", Char.IsLetter(a));
//使用IsDigit方法判断b是否为数字
Console.WriteLine("IsDigit方法判断b是否为数字:{0}", Char.IsDigit(b));
//使用IsLetterOrDigit方法判断c是否为字母或数字
Console.WriteLine("IsLetterOrDigit方法判断c是否为字母或数字:{0}", Char.IsLetterOrDigit(c));
//使用IsLower方法判断a是否为小写字母
Console.WriteLine("IsLower方法判断a是否为小写字母:{0}", Char.IsLower(a));
//使用IsUpper方法判断c是否为大写字母
Console.WriteLine("IsUpper方法判断c是否为大写字母:{0}", Char.IsUpper(c));
//使用IsPunctuation方法判断d是否为标点符号
Console.WriteLine("IsPunctuation方法判断d是否为标点符号:{0}", Char.IsPunctuation(d));
//使用IsSeparator方法判断e是否为分隔符
Console.WriteLine("IsSeparator方法判断e是否为分隔符:{0}", Char.IsSeparator(e));
//使用IsWhiteSpace方法判断f是否为空白
Console.WriteLine("IsWhiteSpace方法判断f是否为空白:{0}", Char.IsWhiteSpace(f));
Console.ReadLine();
}
}
}
5.1.3 转义字符
\
\n
\r
\"
\b
\r
\f
\\
\'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test02
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("用一生下载你"); //通过Console.WriteLine输出字符串
Console.Write("用一生下载你\n"); //通过使用转义字符输出字符串
Console.Write("用一生下载你\n芸烨湘枫"); //通过使用转义字符输出字符串
Console.ReadLine();
}
}
}
//= 5.2 字符串类的String使用。
5.2.1 字符串的使用
String str = "adsfdasfdasf";
5.2.2 连接多个字符串 +
String str1 = "111asdfdsa";
String str2 = "asdfdsafsdaf";
String str3 = str1 + "" + str2;
5.2.3 比较字符串
1,String.Compare(str1,str2);
2,CompareTo();
3,Equals();
1,String.Compare(str1,str2);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test04
{
class Program
{
static void Main(string[] args)
{
string Str1 = "芸烨湘枫"; //声明一个字符串Str1
string Str2 = "用一生下载你"; //声明一个字符串Str2
Console.WriteLine(String.Compare(Str1, Str2)); //输出字符串Str1与Str2比较后的返回值
Console.WriteLine(String.Compare(Str1, Str1)); //输出字符串Str1与Str1比较后的返回值
Console.WriteLine(String.Compare(Str2, Str1)); //输出字符串Str2与Str1比较后的返回值
Console.ReadLine();
}
}
}
2,CompareTo();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test05
{
class Program
{
static void Main(string[] args)
{
string Str1 = "芸烨湘枫"; //声明一个字符串Str1
string Str2 = "用一生下载你"; //声明一个字符串Str2
Console.WriteLine(Str1.CompareTo(Str2)); //输出Str1与Str2比较后的返回值
Console.ReadLine();
}
}
}
3,Equals();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test06
{
class Program
{
static void Main(string[] args)
{
string Str1 = "芸烨湘枫"; //声明一个字符串Str1
string Str2 = "用一生下载你"; //声明一个字符串Str2
Console.WriteLine(Str1.Equals(Str2)); //用Equals方法比较字符串Str1和Str2
Console.WriteLine(String.Equals(Str1, Str2)); //用Equals方法比较字符串Str1和Str2
Console.ReadLine();
}
}
}
5.2.4 格式化字符
string newstr = String.Format("{0},{1}!!!", StrA, StrB); //格式化字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test07
{
class Program
{
static void Main(string[] args)
{
string StrA = "用一生下载你"; //声明字符串StrA
string StrB = "永不放弃"; //声明字符串StrB
string newstr = String.Format("{0},{1}!!!", StrA, StrB); //格式化字符串
Console.WriteLine(newstr); //输出结果
Console.ReadLine();
}
}
}
string strB = String.Format("{0:D}", dt);
0为索引
D为格式日期时间格式,可以查日期时间格式规范
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test08
{
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Now; //获取系统当前日期
string strB = String.Format("{0:D}", dt); //格式化成短日期格式
Console.WriteLine(strB); //输出日期
Console.ReadLine();
}
}
}
5.2.5 截取字符串
StrB = StrA.Substring(1, 4);
1 4 起始结束。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test09
{
class Program
{
static void Main(string[] args)
{
string StrA = "用一生下载你"; //声明字符串StrA
string StrB = ""; //声明字符串StrB
StrB = StrA.Substring(1, 4); //截取字符串
Console.WriteLine(StrB); //输出截取后的字符串
Console.ReadLine();
}
}
}
5.2.6 分割字符串
这个写法与其它语言不同。
string str = "s!ad&fds(afdsaf";
Char[] fg = {'!','#','('};
String[] splitstrings = new String[100];
splitstrings = StrA.Split(str);
这个写法很有意思。
string StrA = "用^一生#下载,你"; //声明字符串StrA
char[] separator = { '^', '#', ',' }; //声明分割字符的数组
String[] splitstrings = new String[100]; //声明一个字符串数组
splitstrings = StrA.Split(separator);
//分割字符串
for (int i = 0; i < splitstrings.Length; i++)
{
Console.WriteLine("item{0}:{1}", i, splitstrings[i]);
}
Console.ReadLine();
这个写法很有意思。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test10
{
class Program
{
static void Main(string[] args)
{
string StrA = "用^一生#下载,你"; //声明字符串StrA
char[] separator = { '^', '#', ',' }; //声明分割字符的数组
String[] splitstrings = new String[100]; //声明一个字符串数组
splitstrings = StrA.Split(separator); //分割字符串
for (int i = 0; i < splitstrings.Length; i++)
{
Console.WriteLine("item{0}:{1}", i, splitstrings[i]);
}
Console.ReadLine();
}
}
}
5.2.7 插入和填充字符串
str2 = str1.Insert(0, "用一生");
str2.PadRight(8, ')');
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test11
{
class Program
{
static void Main(string[] args)
{
string str1 = "下载"; //声明字符串变量str1并赋值为“下载”
string str2; //声明字符串变量str2
str2 = str1.Insert(0, "用一生"); //使用Insert方法向字符串str1中插入字符串
string str3 = str2.Insert(5, "你"); //使用Insert方法向字符串str2中插入字符串
Console.WriteLine(str3); //输出字符串变量str3
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test12
{
class Program
{
static void Main(string[] args)
{
string str1 = "*^__^*"; //声明一个字符串变量str1
//声明一个字符串变量str2,并使用PadLeft方法在str1的左侧填充字符“(”
string str2 = str1.PadLeft(7, '(');
//声明一个字符串变量str3,并使用PadRight方法在str2右侧填充字符“)”
string str3 = str2.PadRight(8, ')');
Console.WriteLine("补充字符串之前:" + str1); //输出字符串str1
Console.WriteLine("补充字符串之后:" + str3); //输出字符串str2
Console.ReadLine();
}
}
}
5.2.8 删除字符串
str1.Remove(3);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test13
{
class Program
{
static void Main(string[] args)
{
string str1 = "用一生下载你"; //声明一个字符串变量str1
//声明一个字符串变量str2,并使用Remove方法从字符串str1的索引3处开始删除
string str2 = str1.Remove(3);
Console.WriteLine(str2); //输出字符串str2
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test14
{
class Program
{
static void Main(string[] args)
{
string str1 = "芸烨湘枫"; //声明一个字符串变量str1,并初始化
//声明一个字符串变量str2,并使用Remove方法从字符串str1的索引1处开始删除两个字符
string str2 = str1.Remove(1, 2);
Console.WriteLine(str2); //输出字符串str2
Console.ReadLine();
}
}
}
5.2.9 复制字符串
str1.CopyTo(1, str, 0, 4);
//将字符串str从索引1开始的4个字符串复制到字符数组str中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test16
{
class Program
{
static void Main(string[] args)
{
string str1 = "用一生下载你"; //声明一个字符串变量str1并初始化
char[] str = new char[100]; //声明一个字符数组str
//将字符串str从索引1开始的4个字符串复制到字符数组str中
str1.CopyTo(1, str, 0, 4);
Console.WriteLine(str); //输出字符数组中的内容
Console.ReadLine();
}
}
}
5.2.10 替换字符串
string b = a.Replace(',', '*');
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test17
{
class Program
{
static void Main(string[] args)
{
string a = "one world,one dream"; //声明一个字符串变量a并初始化
//使用Replace方法将字符串a中的“,”替换为“*”,并赋值给字符串变量b
string b = a.Replace(',', '*');
Console.WriteLine(b); //输出字符串变量b
//使用Replace方法将字符串a中的“one world”替换为“One World”
string c = a.Replace("one world", "One World");
Console.WriteLine(c); //输出字符串变量c
Console.ReadLine();
}
}
}
//= 5.3可变字符串类 StringBuider
大大提高了频繁增加字符串的效率。
5.3.1 StringBuider 类的定义。
StringBuilder builder = new StringBuilder("");//创建字符串生成器
5.3.2 StringBuider 类的使用
关于格式符 C
可以查看:
https://learn.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test18
{
class Program
{
static void Main(string[] args)
{
int Num = 1000; //声明一个int类型变量Num并初始化为1000
//实例化一个StringBuilder类,并初始化为“用一生下载你”
StringBuilder LS = new StringBuilder("用一生下载你", 100);
LS.Append("VS芸烨湘枫"); //使用Append方法将字符串追加到LS的末尾
Console.WriteLine(LS); //输出LS
//使用AppendFormat方法将字符串按照指定的格式追加到LS的末尾
LS.AppendFormat("{0:C}", Num);
Console.WriteLine(LS); //输出LS
LS.Insert(0, "名称:"); //使用Insert方法将“名称:”追加到LS的开头
Console.WriteLine(LS); //输出LS
//使用Remove方法从LS中删除索引15以后的字符串
LS.Remove(15, LS.Length - 15);
Console.WriteLine(LS); //输出LS
//使用Replace方法将“名称:”替换成“一生所爱”
LS.Replace("名称", "一生所爱");
Console.WriteLine(LS); //输出LS
Console.ReadLine();
}
}
}
5.3.3 StringBuider 类 与 String 类的区别
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main(string[] args)
{
String str = ""; //创建空字符串
//定义对字符串执行操作的起始时间
long starTime = DateTime.Now.Millisecond;
for (int i = 0; i < 10000; i++)
{ //利用for循环执行10000次操作
str = str + i; //循环追加字符串
}
long endTime = DateTime.Now.Millisecond; //定义对字符串操作后的时间
long time = endTime - starTime; //计算对字符串执行操作的时间
Console.WriteLine("String消耗时间:" + time); //将执行的时间输出
StringBuilder builder = new StringBuilder("");//创建字符串生成器
starTime = DateTime.Now.Millisecond; //定义操作执行前的时间
for (int j = 0; j < 10000; j++)
{ //利用for循环进行操作
builder.Append(j); //循环追加字符
}
endTime = System.DateTime.Now.Millisecond; //定义操作后的时间
time = endTime - starTime;//追加操作执行的时间
Console.WriteLine("StringBuilder消耗时间:" + time); //将操作时间输出
Console.ReadLine();
}
}
}
//====第6章 流程控制语句
//= 6.1流程控制语句
6.1.1 if
if(){
}
if(){
}else{
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test01
{
class Program
{
static void Main(string[] args)
{
int i = 927; //声明一个int类型变量i
if (i > 927) //调用if语句判断i是否大于927
{
Console.WriteLine("i大于927"); //如果大于927则输出“i大于927”
}
else //否则
{
Console.WriteLine("i不大于927"); //输出“i不大于927”
}
Console.ReadLine();
}
}
}
6.1.2 switch(){}
switch(){
case:
Console.writeLine();
break;
case:
Console.writeLine();
break;
case:
Console.writeLine();
break;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test03
{
class Program
{
static void Main(string[] args)
{
string MyStr = "用一生下载你"; //声明一个字符串变量MyStr并初始化
switch (MyStr) //调用switch语句
{
//如果MyStr的值是“用一生下载你”,在执行分支1
case "用一生下载你": Console.WriteLine("用一生下载你"); break;
//如果MyStr的值是“一生所爱”,在执行分支2
case "一生所爱": Console.WriteLine("一生所爱"); break;
//如果MyStr的值是“芸烨湘枫”,在执行分支3
case "芸烨湘枫": Console.WriteLine("芸烨湘枫"); break;
//如果MyStr的值都不符合以上分支的内容,则执行default语句
default: Console.WriteLine("无字符"); break;
}
Console.ReadLine();
}
}
}
//= 6.2 循环控制语句
6.2.1 while语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test05
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("2014年NBA总冠军马刺队主力队员:");
//声明一个string类型的数组,并初始化
string[] Players = new string[] { "蒂姆·邓肯", "马努·吉诺比利", "托尼·帕克", "卡哇伊·莱昂纳德", "伯瑞斯·迪奥", "丹尼·格林", "帕蒂·米尔斯" };
int i = 0;//声明一个int类型的变量i并初始化为0
while (i < Players.Length)//调用while语句当i小于数组长度时执行
{
//输出数组中的值
Console.WriteLine(Players[i]);
i++;//i自增1
}
Console.ReadLine();
}
}
}
请注意的是:
在循环中遇到break语句时,它会立即终止当前循环的执行,并跳出循环体,继续执行循环后面的代码,而不是重新开始一个新的循环
而continue; 跳出不在执行后边的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test06
{
class Program
{
static void Main(string[] args)
{
int s = 0, num = 80; //声明两个int类型的变量并初始化
while (s < num) //调用while语句,当s小于num时执行
{
s++; //s自增1
if (s > 40) //使用if语句判断s是否大于40
{
break; //使用break语句终止循环
}
if ((s % 2) == 0) //调用if语句判断s是否为偶数
{
continue; //使用continue语句将程序转到下一次循环
}
Console.WriteLine(s); //输出s
}
Console.ReadLine();
}
}
}
6.2.2 do...while
注意:
string[] myArray = new string[3]
C# 强制类型,字符串数组。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test07
{
class Program
{
static void Main(string[] args)
{
string[] myArray = new string[3] { "世界杯", "欧洲杯", "欧冠" };//声明一个string数组并初始化
int i = 0;
do//调用do…while语句
{
Console.WriteLine(myArray[i]);//输出数组中数据
i++;
} while (i < myArray.Length);//设置do…while语句的条件
Console.ReadLine();
}
}
}
6.2.3 for语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test08
{
class Program
{
static void Main(string[] args)
{
int[] myint = new int[10]; //声明一个具有10个元素的整型数组
myint[0] = 0; //向数组中添加第一个元素
myint[1] = 1; //向数组中添加第二个元素
myint[2] = 2; //向数组中添加第三个元素
myint[3] = 3; //向数组中添加第四个元素
myint[4] = 4; //向数组中添加第五个元素
myint[5] = 5; //向数组中添加第六个元素
myint[6] = 6; //向数组中添加第七个元素
myint[7] = 7; //向数组中添加第八个元素
myint[8] = 8; //向数组中添加第九个元素
myint[9] = 9; //向数组中添加第十个元素
for (int i = 0; i < myint.Length; i++) //调用for循环语句
{
Console.WriteLine("myint[{0}]的值是:{1}", i, myint[i]); //输出结果
}
Console.ReadLine();
}
}
}
6.2.4 foreach()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test09
{
class Program
{
static void Main(string[] args)
{
ArrayList alt = new ArrayList(); //实例化ArrayList类
alt.Add("用一生下载你"); //使用Add方法向对象中添加数据
alt.Add("芸烨湘枫"); //使用Add方法向对象中添加数据
alt.Add("一生所爱"); //使用Add方法向对象中添加数据
alt.Add("情茧"); //使用Add方法向对象中添加数据
alt.Add("痞子CAI"); //使用Add方法向对象中添加数据
Console.WriteLine("您收藏的网名有:"); //输出提示
foreach (string InternetName in alt) //使用foreach语句输出数据
{
Console.WriteLine(InternetName); //输出ArrayList对象中的所有数据
}
Console.ReadLine();
}
}
}
6.3.1 break语句
请注意的是:
在循环中遇到break语句时,它会立即终止当前循环的执行,并跳出循环体,继续执行循环后面的代码,而不是重新开始一个新的循环
而continue; 跳出不在执行后边的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test10
{
class Program
{
static void Main(string[] args)
{
int i = Convert.ToInt32(DateTime.Today.DayOfWeek); //获取当前日期的数值
switch (i) //调用switch语句
{
case 1: Console.WriteLine("今天是星期一"); break; //如果i是1,则输出今天是星期一
case 2: Console.WriteLine("今天是星期二"); break; //如果i是2,则输出今天是星期二
case 3: Console.WriteLine("今天是星期三"); break; //如果i是3,则输出今天是星期三
case 4: Console.WriteLine("今天是星期四"); break; //如果i是4,则输出今天是星期四
case 5: Console.WriteLine("今天是星期五"); break; //如果i是5,则输出今天是星期五
case 6: Console.WriteLine("今天是星期六"); break; //如果i是6,则输出今天是星期六
case 0: Console.WriteLine("今天是星期日"); break; //如果i是7,则输出今天是星期日
}
Console.ReadLine();
}
}
}
6.3.2continue
请注意的是:
在循环中遇到break语句时,它会立即终止当前循环的执行,并跳出循环体,继续执行循环后面的代码,而不是重新开始一个新的循环
而continue; 跳出不在执行后边的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test12
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 4; i++) //调用for循环
{
Console.Write("\n第{0}次循环:", i); //输出提示第几次循环
for (int j = 0; j < 20; j++) //调用for循环
{
if (j % 2 == 0) //调用if语句判断j是否是偶数
continue; //如果是偶数则使用continue语句继续下一循环
Console.Write(j + " "); //输出j
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
6.3.3 goto
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test13
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入要查找的文字:"); //输出提示信息
string inputstr = Console.ReadLine(); //获取输入值
string[] mystr = new string[5]; //创建一个字符串数组
mystr[0] = "用一生下载你"; //向数组中添加第一个元素
mystr[1] = "芸烨湘枫"; //向数组中添加第二个元素
mystr[2] = "一生所爱"; //向数组中添加第三个元素
mystr[3] = "情茧"; //向数组中添加第四个元素
mystr[4] = "风华绝代"; //向数组中添加第五个元素
for (int i = 0; i < mystr.Length; i++) //调用for循环语句
{
//通过if语句判断是否存在输入的字符串
if (mystr[i].Equals(inputstr))
{
goto Found; //调用goto语句跳转到Found
}
}
Console.WriteLine("您查找的{0}不存在!", inputstr); //输出信息
goto Finish; //调用goto语句跳转到Finish
Found:
Console.WriteLine("您查找的{0}存在!", inputstr); //输出信息,提示存在输入的字符串
Finish:
Console.WriteLine("查找完毕!"); //输出信息,提示查找完毕
Console.ReadLine();
}
}
}
6.3.4 return
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test14
{
class Program
{
static string MyStr(string str) //创建一个string类型方法
{
string OutStr; //声明一个字符串变量
OutStr = "您输入的数据是:" + str; //为字符串变量赋值
return OutStr; //使用return语句返回字符串变量
}
static void Main(string[] args)
{
Console.WriteLine("请您输入内容:"); //输出提示信息
string inputstr = Console.ReadLine(); //获取输入的数据
Console.WriteLine(MyStr(inputstr)); //调用MyStr方法并将结果显示出来
Console.ReadLine();
}
}
}
//====第7章 数组和集合
一维数组实质上是一组相同类型数据的线性集合
//=7.1数组概述
具有相同数据类型的一组数据集合
//=7.2 一维数组的创建和使用
7.2.1 一维数组的创建
1,先声明,再new;
int[] arr;
string[] str;
int =new int[5];
2,声明同时分配内存
int[] = new int[12];
7.2.2 一维数组初始化
1,初始化1
int[] ar = new int[]{1,2,3,6,87};
2,初始化2 !!! 用的比较多 !!!
int[] ar ={1,2,3,6,87};
7.2.3 一维数组的使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test01
{
class Program
{
static void Main(string[] args)
{
//创建并初始化一维数组
int[] day = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (int i = 0; i < 12; i++)//利用循环将信息输出
{
Console.WriteLine((i + 1) + "月有" + day[i] + "天");//输出的信息
}
Console.ReadLine();
}
}
}
//=7.3 二维数组的创建和使用
7.3.1 二维数组的创建
常用来表示表和列,第一个下标表示行,第二个下标表示列。
int[,] myar;
1,直接为每一维分配内存空间。
int[,] a = new int[2,3];
2,分别为每一维分配内存空间。
int[][] a = new int[2][];
a[0]=new int[2];
a[1]=new int[3];
7.3.2 二维数组初始化
int[,] ar1 = new int[,]{{1,33,5},{1,3,4}};
int[,] ar2 ={{1,33,5},{1,3,4}};
7.3.3 二维数组的使用
外行,内列;
for(){
for(){}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test02
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[2, 2] { { 1, 2 }, { 3, 4 } }; //自定义一个二维数组
Console.Write("数组的行数为:");
Console.Write(arr.GetLength(0)); //获得二维数组的行数
Console.Write("\n");
Console.Write("数组的列数为:");
Console.Write(arr.GetUpperBound(arr.Rank - 1) + 1);//获得二维数组的列数
Console.Write("\n");
for (int i = 0; i < arr.Rank; i++)
{
string str = "";
for (int j = 0; j < arr.GetUpperBound(arr.Rank - 1) + 1; j++)
{
str = str + Convert.ToString(arr[i, j]) + " ";
//循环输出二维数组中的每个元素
}
Console.Write(str);
Console.Write("\n");
}
Console.ReadLine();
}
}
}
注意:以下是关于arr.GetUpperBound(arr.Rank - 1) 的一个demo; 可以参考写法。
arr.GetUpperBound() 输出: 上限索引值为: 9
int[] myArray = new int[10];
int upperBound = Array.GetUpperBound(myArray);
Console.WriteLine("上限索引值为: " + upperBound); // 输出: 上限索引值为: 9
// 多维数组
int[,] multiArray = new int[5, 10];
int dim0UpperBound = Array.GetUpperBound(multiArray, 0);
int dim1UpperBound = Array.GetUpperBound(multiArray, 1);
Console.WriteLine("第一维上限索引值为: " + dim0UpperBound); // 输出: 第一维上限索引值为: 4
Console.WriteLine("第二维上限索引值为: " + dim1UpperBound); // 输出: 第二维上限索引值为: 9
//= 7.4 数组的基本操作
7.4.1 遍历数组
foreach(int numae in ar){
//
}
7.4.2 添加数组,删除数组元素
//添加数组
思考:
1,数据
2,逻辑
2,22,33,44,55,66
99 2 22 33 44 55 66 <=
2,22,33,99,44,55,66
2 22 33 44 55 66 99 >=
i =0;
i==2
fun(ar,2,99);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AddItem
{
class Program
{
/// <summary>
/// 增加单个数组元素
/// </summary>
/// <param name="ArrayBorn">要向其中添加元素的一维数组</param>
/// <param name="Index">添加索引</param>
/// <param name="Value">添加值</param>
/// <returns></returns>
static int[] AddArray(int[] ArrayBorn, int Index, int Value)
{
if (Index >= (ArrayBorn.Length))//判断添加索引是否大于数组的长度
Index = ArrayBorn.Length - 1;//将添加索引设置为数组的最大索引
int[] TemArray = new int[ArrayBorn.Length + 1];//声明一个新的数组
for (int i = 0; i < TemArray.Length; i++)//遍历新数组的元素
{
if (Index >= 0)//判断添加索引是否大于等于0
{
if (i < (Index + 1))//判断遍历到的索引是否小于添加索引加1
TemArray[i] = ArrayBorn[i];//交换元素值
else if (i == (Index + 1))//判断遍历到的索引是否等于添加索引加1
TemArray[i] = Value;//为遍历到的索引设置添加值
else
TemArray[i] = ArrayBorn[i - 1];//交换元素值
}
else
{
if (i == 0)//判断遍历到的索引是否为0
TemArray[i] = Value;//为遍历到的索引设置添加值
else
TemArray[i] = ArrayBorn[i - 1];//交换元素值
}
}
return TemArray;//返回插入元素后的新数组
}
static void Main(string[] args)
{
int[] ArrayInt = new int[] { 0, 1, 2, 3, 4, 6, 7, 8, 9 };//声明一个一维数组
Console.WriteLine("原数组元素:");
foreach (int i in ArrayInt)//遍历声明的一维数组
Console.Write(i+" ");//输出数组中的元素
Console.WriteLine();//换行
ArrayInt = AddArray(ArrayInt, 4, 5);//调用自定义方法向数组中插入单个元素
Console.WriteLine("插入之后的数组元素:");
foreach (int i in ArrayInt)//遍历插入元素后的一维数组
Console.Write(i+" ");//输出数组中的元素
Console.ReadLine();
}
}
}
//填加数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AddArrays
{
class Program
{
/// <summary>
/// 向一维数组中添加一个数组
/// </summary>
/// <param name="ArrayBorn">源数组</param>
/// <param name="ArrayAdd">要添加的数组</param>
/// <param name="Index">添加索引</param>
/// <returns>新得到的数组</returns>
static int[] AddArray(int[] ArrayBorn, int[] ArrayAdd, int Index)
{
if (Index >= (ArrayBorn.Length))//判断添加索引是否大于数组的长度
Index = ArrayBorn.Length - 1;//将添加索引设置为数组的最大索引
int[] TemArray = new int[ArrayBorn.Length + ArrayAdd.Length];//声明一个新的数组
for (int i = 0; i < TemArray.Length; i++)//遍历新数组的元素
{
if (Index >= 0)//判断添加索引是否大于等于0
{
if (i < (Index + 1))//判断遍历到的索引是否小于添加索引加1
TemArray[i] = ArrayBorn[i];//交换元素值
else if (i == (Index + 1))//判断遍历到的索引是否等于添加索引加1
{
for (int j = 0; j < ArrayAdd.Length; j++)//遍历要添加的数组
TemArray[i + j] = ArrayAdd[j];//为遍历到的索引设置添加值
i = i + ArrayAdd.Length - 1;//将遍历索引设置为要添加数组的索引最大值
}
else
TemArray[i] = ArrayBorn[i - ArrayAdd.Length];//交换元素值
}
else
{
if (i == 0)//判断遍历到的索引是否为0
{
for (int j = 0; j < ArrayAdd.Length; j++)//遍历要添加的数组
TemArray[i + j] = ArrayAdd[j];//为遍历到的索引设置添加值
i = i + ArrayAdd.Length - 1;//将遍历索引设置为要添加数组的索引最大值
}
else
TemArray[i] = ArrayBorn[i - ArrayAdd.Length];//交换元素值
}
}
return TemArray;//返回添加数组后的新数组
}
static void Main(string[] args)
{
int[] ArrayInt = new int[] { 0, 1, 2, 3, 8, 9 };//声明一个数组,用来作为源数组
int[] ArrayInt1 = new int[] { 4, 5, 6, 7 };//声明一个数组,用来作为要添加的数组
Console.WriteLine("源数组:");
foreach (int i in ArrayInt)//遍历源数组
Console.Write(i+" ");//输出源数组元素
Console.WriteLine();//换行
Console.WriteLine("要添加的数组:");
foreach (int i in ArrayInt1)//遍历要添加的数组
Console.Write(i + " ");//输出要添加的数组中的元素
Console.WriteLine();//换行
ArrayInt = AddArray(ArrayInt, ArrayInt1, 3);//向数组中添加数组
Console.WriteLine("添加后的数组:");
foreach (int i in ArrayInt)//遍历添加后的数组
Console.Write(i+" ");//输出添加后的数组中的元素
Console.ReadLine();
}
}
}
//删除数组元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelArrayNo
{
class Program
{
/// <summary>
/// 删除数组中的元素
/// </summary>
/// <param name="ArrayBorn">要从中删除元素的数组</param>
/// <param name="Index">删除索引</param>
/// <param name="Len">删除的长度</param>
static void DeleteArray(string[] ArrayBorn, int Index, int Len)
{
if (Len <= 0)//判断删除长度是否小于等于0
return;//返回
if (Index == 0 && Len >= ArrayBorn.Length)//判断删除长度是否超出了数组范围
Len = ArrayBorn.Length;//将删除长度设置为数组的长度
else if ((Index + Len) >= ArrayBorn.Length)//判断删除索引和长度的和是否超出了数组范围
Len = ArrayBorn.Length - Index - 1;//设置删除的长度
int i = 0;//定义一个int变量,用来标识开始遍历的位置
for (i = 0; i < ArrayBorn.Length - Index - Len; i++)//遍历删除的长度
ArrayBorn[i + Index] = ArrayBorn[i + Len + Index];//覆盖要删除的值
//遍历删除长度后面的数组元素值
for (int j = (ArrayBorn.Length - 1); j > (ArrayBorn.Length - Len - 1); j--)
ArrayBorn[j] = null;//设置数组为空
}
static void Main(string[] args)
{
string[] ArrayStr = new string[] { "m", "r", "s", "o", "f", "t" };//声明一个字符串数组
Console.WriteLine("源数组:");
foreach (string i in ArrayStr)//遍历源数组
Console.Write(i+" ");//输出数组中的元素
Console.WriteLine();//换行
DeleteArray(ArrayStr, 0, 1);//删除数组中的元素
Console.WriteLine("删除元素后的数组:");
foreach (string i in ArrayStr)//遍历删除元素后的数组
Console.Write(i+" ");//输出数组中的元素
Console.ReadLine();
}
}
}
7.4.3 对数组进行排序
int[] ar = new int[]{2,12,4,6,7,98};
Array.Sort(ar);
7.4.4 数组的合并与拆分
//数组的合并
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test07
{
class Program
{
static void Main(string[] args)
{
//定义两个一维数组
int[] arr1 = new int[] { 1, 2, 3, 4, 5 };
int[] arr2 = new int[] { 6, 7, 8, 9, 10 };
int n = arr1.Length + arr2.Length;
//根据定义的两个一维数组的长度的和定义一个新的一维数组
int[] arr3 = new int[n];
//将定义的两个一维数组中的元素添加到新的一维数组中
for (int i = 0; i < arr3.Length; i++)
{
if (i < arr1.Length)
arr3[i] = arr1[i];
else
arr3[i] = arr2[i - arr1.Length];
}
Console.WriteLine("合并后的一维数组:");
foreach (int i in arr3)
Console.Write("{0}", i + " ");
Console.WriteLine();
//定义一个要合并的二维数组
int[,] arr4 = new int[2, 5];
//将两个一维数组循环添加到二维数组中
for (int i = 0; i < arr4.Rank; i++)
{
switch (i)
{
case 0:
{
for (int j = 0; j < arr1.Length; j++)
arr4[i, j] = arr1[j];
break;
}
case 1:
{
for (int j = 0; j < arr2.Length; j++)
arr4[i, j] = arr2[j];
break;
}
}
}
Console.WriteLine("合并后的二维数组:");
//显示合并后的二维数组
for (int i = 0; i < arr4.Rank; i++)
{
for (int j = 0; j < arr4.GetUpperBound(arr4.Rank - 1) + 1; j++)
Console.Write(arr4[i, j] + " ");
Console.WriteLine();
}
}
}
}
//数组的拆分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test08
{
class Program
{
static void Main(string[] args)
{
int[,] arr1 = new int[2, 3] { { 1, 3, 5 }, { 2, 4, 6 } }; //定义一个二维数组,并赋值
//定义两个一维数组,用来存储拆分的二维数组中的元素
int[] arr2 = new int[3];
int[] arr3 = new int[3];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
switch (i)
{
case 0: arr2[j] = arr1[i, j]; break; //判断如果是第一行中的元素,则添加到第一个中
case 1: arr3[j] = arr1[i, j]; break; //判断如果是第二行中的元素,则添加到第二个中
}
}
}
Console.WriteLine("数组一:");
foreach (int n in arr2) //输出拆分后的第一个一维数组
Console.Write(n + " ");
Console.WriteLine();
Console.WriteLine("数组二:"); //输出拆分后的第二个一维数组
foreach (int n in arr3)
Console.Write(n + " ");
Console.WriteLine();
}
}
}
//= 7.5数组排序算法
7.5.1 冒泡排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test04
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 63, 4, 24, 1, 3, 15 }; //定义一个一维数组,并赋值
//定义两个int类型的变量,分别用来表示数组下标和存储新的数组元素
int j, temp;
for (int i = 0; i < arr.Length - 1; i++) //根据数组下标的值遍历数组元素
{
j = i + 1;
id: //定义一个标识,以便从这里开始执行语句
if (arr[i] > arr[j]) //判断前后两个数的大小
{
temp = arr[i]; //将比较后大的元素赋值给定义的int变量
arr[i] = arr[j]; //将后一个元素的值赋值给前一个元素
arr[j] = temp; //将int变量中存储的元素值赋值给后一个元素
goto id; //返回标识,继续判断后面的元素
}
else
if (j < arr.Length - 1) //判断是否执行到最后一个元素
{
j++; //如果没有,则再往后判断
goto id; //返回标识,继续判断后面的元素
}
}
foreach (int n in arr) //循环遍历排序后的数组元素并输出
Console.Write(n + " ");
Console.WriteLine();
Console.ReadLine();
}
}
}
7.5.2 直接插入排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test05
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 63, 4, 24, 1, 3, 15 };//定义一个一维数组,并赋值
for (int i = 0; i < arr.Length; ++i)//循环访问数组中的元素
{
int temp = arr[i];//定义一个int变量,并使用获得的数组元素值赋值
int j = i;
while ((j > 0) && (arr[j - 1] > temp))//判断数组中的元素是否大于获得的值
{
arr[j] = arr[j - 1];//如果是,则将后一个元素的值提前
--j;
}
arr[j] = temp;//最后将int变量存储的值赋值给最后一个元素
}
Console.WriteLine("排序后结果为:");
foreach (int n in arr)//循环访问排序后的数组元素并输出
Console.Write("{0}", n + " ");
Console.WriteLine();
Console.ReadLine();
}
}
}
7.5.3 选择排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test06
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 63, 4, 24, 1, 3, 15 }; //定义一个一维数组,并赋值
int min; //定义一个int变量,用来存储数组下标
for (int i = 0; i < arr.Length - 1; i++) //循环访问数组中的元素值(除最后一个)
{
min = i; //为定义的数组下标赋值
for (int j = i + 1; j < arr.Length; j++) //循环访问数组中的元素值(除第一个)
{
if (arr[j] < arr[min]) //判断相邻两个元素值的大小
min = j;
}
int t = arr[min]; //定义一个int变量,用来存储比较大的数组元素值
arr[min] = arr[i]; //将小的数组元素值移动到前一位
arr[i] = t; //将int变量中存储的较大的数组元素值向后移
}
Console.WriteLine("排序后结果为:");
foreach (int n in arr) //循环访问排序后的数组元素并输出
Console.Write("{0}", n + " ");
Console.WriteLine();
Console.ReadLine();
}
}
}
//= 7.6 ArrayList类
高级的动态数组
容量是固定的
有各种添加删除插入的方法
将只读和固定大小包装返回的集合方法
只能是一维的。
ArrayList List = new ArrayList(10);
for(int i =0; i<List.Count; i++){
//
}
7.6.1 ArrayList 类概述
ArrayList List = new ArrayList(10);
for(int i =0; i<List.Count; i++){
//
}
7.6.2 ArrayList 元素的添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test09
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
ArrayList List = new ArrayList(arr); //使用声明的一维数组实例化一个ArrayList对象
Console.WriteLine("原ArrayList集合:");
foreach (int i in List) //遍历ArrayList集合并输出
{
Console.Write(i + " ");
}
Console.WriteLine();
for (int i = 1; i < 5; i++)
{
List.Add(i + arr.Length); //为ArrayList集合添加元素
}
Console.WriteLine("使用Add方法添加:");
foreach (int i in List) //遍历添加元素后的ArrayList集合并输出
{
Console.Write(i + " ");
}
Console.WriteLine();
List.Insert(6, 6); //在ArrayList集合的指定位置添加元素
Console.WriteLine("使用Insert方法添加:");
foreach (int i in List) //遍历最后的ArrayList集合并输出
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
}
7.6.3 ArrayList 元素的删除
1,Clear()
2,Remove()
3,RemoveAt()
4,RemoveRange()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test10
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ArrayList List = new ArrayList(arr); //使用声明的一维数组实例化一个ArrayList对象
Console.WriteLine("原ArrayList集合:");
foreach (int i in List) //遍历ArrayList集合中的元素并输出
{
Console.Write(i.ToString() + " ");
}
Console.WriteLine();
List.RemoveRange(0, 5); //从ArrayList集合中移除指定下标位置的元素
Console.WriteLine("删除元素后的ArrayList集合:");
foreach (int i in List) //遍历删除元素后的ArrayList集合并输出其元素
{
Console.Write(i.ToString() + " ");
}
Console.WriteLine();
}
}
}
7.6.4 ArrayList 元素的遍历
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test11
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList(); //实例化一个ArrayList对象
list.Add("TM"); //向ArrayList集合中添加元素
list.Add("C#从入门到精通");
foreach (string str in list) //遍历ArrayList集合中的元素并输出
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
}
7.6.5 ArrayList 元素的查找
//= 7.7 Hashtable (哈希表)
哈希表(HashTable)在C#中是一种使用哈希函数的数据结构,它可以提供快速的插入和查找操作。
哈希表的作用主要体现在以下几点:
快速的插入和查找:哈希表使用哈希函数来计算键(Key)的哈希代码,然后根据这个代码来决定值(Value)的存储位置,因此在插入和查找时可以实现近乎常数时间的复杂度(O(1))。
存储大量的键值对:哈希表可以存储大量的键值对,因为它的存储方式是基于数组的,所以可以有效地存储大量的数据。
键的唯一性:在哈希表中,键是唯一的,不允许重复,这一特性可以用于去重等场景。
无序存储:哈希表中的数据是无序存储的,如果需要顺序,需要额外的排序操作。
7.7.1 Hashtable 元素的添加
Hashtable hashtable = new Hashtable();
7.7.2 Hashtable 元素的添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test12
{
class Program
{
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable(); //实例化Hashtable对象
hashtable.Add("id", "BH0001"); //向Hashtable哈希表中添加元素
hashtable.Add("name", "TM");
hashtable.Add("sex", "男");
Console.WriteLine(hashtable.Count); //获得Hashtable哈希表中的元素个数
}
}
}
7.7.3 Hashtable 元素的删除
Clear();
Remove();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test14
{
class Program
{
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable(); //实例化Hashtable对象
hashtable.Add("id", "BH0001"); //向Hashtable哈希表中添加元素
hashtable.Add("name", "TM");
hashtable.Add("sex", "男");
hashtable.Remove("sex"); //移除Hashtable哈希表中的指定元素
Console.WriteLine(hashtable.Count);
}
}
}
7.7.4 Hashtable 元素的遍历
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test15
{
class Program
{
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable(); //实例化Hashtable对象
hashtable.Add("id", "BH0001"); //向Hashtable哈希表中添加元素
hashtable.Add("name", "TM");
hashtable.Add("sex", "男");
Console.WriteLine("\t 键\t 值");
//遍历Hashtable哈希表中的元素并输出其键值对
foreach (DictionaryEntry dicEntry in hashtable)
{
Console.WriteLine("\t " + dicEntry.Key + "\t " + dicEntry.Value);
}
Console.WriteLine();
}
}
}
7.7.5 Hashtable 元素的查找
Contains();
ContainsValue();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test16
{
class Program
{
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable(); //实例化Hashtable对象
hashtable.Add("id", "BH0001"); //向Hashtable哈希表中添加元素
hashtable.Add("name", "TM");
hashtable.Add("sex", "男");
Console.WriteLine(hashtable.Contains("id")); //判断Hashtable哈希表中是否包含指定的键
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Test17
{
class Program
{
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable(); //实例化Hashtable对象
hashtable.Add("id", "BH0001"); //向Hashtable哈希表中添加元素
hashtable.Add("name", "TM");
hashtable.Add("sex", "男");
Console.WriteLine(hashtable.ContainsValue("男")); //判断Hashtable哈希表中是否包含指定的键值
}
}
}
//====第8章属性和方法
public
protected
internal
private
public int name{
//........
}
拥有get 和 set 的访问器;
public class Date{
private int Day = 7;
public int day{
get{};
set{
if(value>0)&&(value<8)){
Day = value;
}
}
}
}
//== 属性
8.1.1
8.1.2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test01
{
class MyClass
{
private string id = ""; //定义一个string类型的变量,用来记录用户编号
/// <summary>
///定义用户编号属性,该属性为可读可写属性
/// </summary>
public string ID
{
get
{
return id;
}
set
{
id = value;
}
}
private string name = ""; //定义一个string类型的变量,用来记录用户姓名
/// <summary>
///定义用户姓名属性,该属性为可读可写属性
/// </summary>
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
class Program
{
static void Main(string[] args)
{
MyClass myclass = new MyClass(); //实例化MyClass类对象
myclass.ID = "BH001"; //为用户编号属性赋值
myclass.Name = "TM1"; //为用户姓名属性赋值
Console.WriteLine(myclass.ID + " " + myclass.Name); //输出用户编号和用户姓名
myclass.ID = "BH002"; //重新为用户编号属性赋值
myclass.Name = "TM2"; //重新为用户姓名属性赋值
Console.WriteLine(myclass.ID + " " + myclass.Name); //再次输出用户编号和用户姓名
}
}
}
//== 方法
public void name(){
//没有返回值的方法
}
方法的参数类型
1, params
static void Us(params String[] list){
for(){
//......
}
}
static void Main(){
string[] strName = new string[5] {"我","你","他","它"};
Us(strName);
}
2,ref
ref 方法中的变量值,在 调用中 值也是 44
public static void Method(ref int i){
i = 44;
}
public static void Main(){
int val = 0;
Method(ref val); // 44
}
3,out
public static void Method(out int i){
i = 44;
}
public static void Main(){
Method(out val); // 44
}
方法的分类
1,静态方法
属于类本身:静态方法不属于类的某一个具体实例,而是属于类本身。因此,不需要首先创建一个类的实例即可调用23。
内存占用:静态方法在内存中通常只有一份副本,无论创建多少类的实例,静态方法都占用同一块内存区域13。
调用方式:静态方法通过类名直接调用,格式为ClassName.StaticMethodName()23。
访问权限:静态方法只能访问类的静态成员,不能直接访问类的实例成员12。
效率与资源:静态方法的效率较高,因为它不依赖于任何实例,但其缺点是不自动进行销毁,可能会占用不必要的内存
主要是:
静态方法通过类名直接调用
静态方法只能访问类的静态成员
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test02
{
class Program
{
public static int Add(int x, int y)//定义一个静态方法
{
return x + y;
}
static void Main(string[] args)
{
Console.WriteLine("结果为:" + Program.Add(3, 5));//使用类名调用静态方法
}
}
}
2,非静态方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test03
{
class Program
{
public int Add(int x, int y) //定义一个非静态方法
{
return x + y;
}
static void Main(string[] args)
{
Program program = new Program(); //实例化类对象
Console.WriteLine("结果为:" + program.Add(3, 5)); //使用类对象调用定义的非静态方法
}
}
}
类.非静态方法
8.2.1
8.2.2
8.2.3
8.2.4
8.2.5
//====第9章结构和类
//==9.1结构
9.1.2结构的使用
//==9.2 面向对象的概述
9.2.1
9.2.2
9.2.3
9.2.4
9.2.5
//==9.3 类
9.3.1
9.3.2
9.3.3
9.3.4
9.3.5
//==9.4类的面向对象特性
9.4.1
9.4.2
9.4.3
//========第2篇核心技术
//====第10章Windows窗体
//====第11章Windows应用程序常用控件
//====第12章Windows应用程序高级控件
//====第13章数据访问技术
//====第14章DataGridView数据控件
//====第15章LINQ数据访问技术
//====第16章程序调试与异常处理
//========第3篇高级应用
//====第17章面向对象技术高级应用
//====第18章迭代器和分部器
//====第19章泛型的使用
//====第20章文件及数据流技术
//====第21章GDI + 图形图象技术
//====第22章Windows打印技术
//====第23章网络编程技术
//====第24章注册表技术
//====第25章线程的使用
//========第4篇项目实际
//====第26章企业人事管理系统
====================================================================================
以下是第1次学习文档
====================================================================================
ctrl+k+d 快速对齐代码
ctrl+z 撤消
ctrl+s 保存
ctrl+j 快速弹出智能提示
shift+End shift+Home
ctrl+k+c 注释所选代码
ctrl+k+u 取消对所选代码的注释
F1 转到帮助文档
折叠冗余代码 #Region #EndRegion
//==================== 开始C#之旅行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace c_xuexi
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("第一个c#");
}
}
}
//命名空间
using 命名空间;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using N1;
namespace c_xuexi
{
internal class Program
{
static void Main(string[] args)
{
//调用命名空间,实列类,调用方法
Mmkj Mmkj = new Mmkj();
Mmkj.XxMmkj();
Console.WriteLine("第一个c#");
}
}
}
namespace N1 {
class Mmkj {
public static void XxMmkj() {
Console.WriteLine("学习命名空间");
}
}
}
//类
class L{
public void Xx(){
}
}
//标识符及关键词
//c#语句
//注释
//
/**/
//==================== 变量
//声明变量
int a;
string str;
//变量赋值
int a =123;
string str = "这是字符串";
//变量的作用域
1,成员变量 类中定义的变量
2,局部变量 方法体中定义的变量
class L{
int a;
string str = "这是字符串";
}
class L{
int a;
string str = "这是字符串";
static void main(){
Console.Write();
}
}
//==================== 数据类型
值类型
引用类型
枚举类型
1,值类型
整数 int用的最多 sbyte short int long byte ushort uint ulong
浮点 float
布尔 bool
2,引用类型
类,接口,数组,委托
3,枚举类型
//枚举类型
enum 枚举名{
list1 = value1;
list2 = value2;
list3 = value3;
}
//值类型和引用类型的区别
值在栈,引用在堆;
//类型转换
1,隐式转换
2,显式转换(强制转换)
1,隐式转换
int i = 927;
long b = i;
2,显式转换(强制转换)
double i = 10.5465;
int a = (int)i;
//常量
const int A = 1;
const double Abc = 10.11;
//==================== 表达式与运算符
//表达式
//运算符
//赋值运算符
//关系运算符
//逻辑运算符
//位运算符
//其它特殊运算符
//运算符优先级
//==================== 字符与字符串
//字符Char类的使用
char st = 'a';
//字符Char类的方法 可以灵活的操作字符
char.IsControl
..................
//转义字符
\n
\t
\"
\b
\r
\f
\\
\'
//字符串String类的使用
声明赋值
String str = "这是字符串";
//字符串连接
String = str1+str2+str3;
//比较字符串
String str1 ="1111";
String str2 ="1111";
Contsole.Write(str1 == str2); //true;
字符串的方法比较多,可以查阅官方手册 String类
//格式化字符串 String类提供了Format方法 将字符串的数据格式化成指定的格式。
String newstr = String.Format("{0},{1}",str1,str2);
//截取字符串方法
//分割字符串方法
//插入和填充字符串方法
//删除字符串方法
//复制字符串方法
//替换字符串方法
字符串的方法比较多,可以查阅官方手册 String类
//可变字符串类 StringBuilder类
极大的减少了字符串方法的效率问题。效率更大
https://learn.microsoft.com/zh-cn/dotnet/standard/base-types/stringbuilder
//==================== 流程控制语句
//条件判断
if(){
}
if(){
}else{
}
switch(){
case1:
Console.Write();
break;
case2:
Console.Write();
break;
case3:
Console.Write();
break;
case4:
Console.Write();
break;
}
//循环语句
while(){
}
do{
}while()
for(int i =0; i<100; i++){
}
ArrayList lis = new ArrayList();
lis.Add("str1");
lis.Add("str2");
lis.Add("str3");
foreach(String value in listar){
Console.Write(value);
}
//跳转语句
break;
continue;
goto name;
return 0;
//==================== 数组和集合
注意:
1 不能越标 不能<0 不能大于数组的长度。
2 处理数组的数组只有循环。
3 加强逻辑思维学习。
一维数组声明同时初始化
int[] arr1 = new int[]{1,2,3,4,5};
或
int[] arr2 = {1,2,3,4,5};
arr1[0];
二维数组的创建和使用
inf[,] arr = new int[2,4];
arr[0] = new int[2];
arr[1] = new int[3];
二维数组的初始化
int[,] arr1 = new int[,]{{1,2},{3,5}};
int[,] arr2 = {{1,2},{3,5}};
//数组的基本操作。
1,遍历数组
int[] Arr = { 1, 2, 3, 4, 5 };
foreach (int i in Arr)
{
Console.WriteLine(i.ToString());
}
Console.ReadKey();
2,添加删除数组元素
指定索引添加元素:
v 1 2 | 3 4 5 6
k 0 1 | 2 3 4 5
v 1 2 9 | 3 4 5 6
k 0 1 2 | 3 4 5 7
addark($ar, $k, $v);
addark(arr, 2, 9);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace c_xuexi
{
internal class Program
{
/// <summary>
/// 根据索引值,向数组中填加一个元素。
/// </summary>
/// <param name="ar"></param>
/// <param name="k"></param>
/// <param name="v"></param>
/// <returns></returns>
public static int[] arraaa(int[] ar, int k, int v)
{
//int[] ar = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// 11 22 33 44 55 66 77 88 99
// 11 22 33 678 44 55 66 77 88 99
// 0 1 2 3 | 4 5 6 7 8 9
//新建一个新数组
//老数组的索引最大值 等于 老数组-1
//如果k大于等于0 并且 k小于等于数组的长度-1
//循环 把老数组的值 加到 新数组。
//循环时 判断 把填加的值 加到新数组中去。
//否则 提示插入的数组索引 不符合要求
//循环时 如果 k <= i 3<= 0 1 2 3 新数组[i] = 老数组的[i]
//否则 如果 k == i 新数组[i] = v
//否则 如果 i>k 4 5 6 7 8 9 新数组[i] = 老数组[i]
//否则 提示插入的索引超出数组的限标。
//arraaa(ar,3,678);
int[] newar = new int[ar.Length+1];
int lar = ar.Length - 1;
int larage = ar.Length + 1;
if (k >= 0 && k <= ar.Length + 1) //3>=0 3<=9
{
for (int i = 0; i < newar.Length; i++)
{
if (k > i) //3 > i 0 1 2 3
{ // 3 < 0 1 2
newar[i] = ar[i];
}
else if (k == i) //3 == 3
{
newar[i] = v;
}
else if (k < i ) // 3 < 4 5 6 7 8 9 3==ar.length+1
{
newar[i] = ar[i-1]; //新换新数组
//Console.WriteLine(newar[i]);
}
}
}
else {
Console.WriteLine("插入的索引超过数组的下标");
}
return newar;
}
static void Main(string[] args)
{
//
//int[] ar = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//int k = 2;
//int v = 99;
//int[] ara = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//ara[0] = 111;
//for (int i = 0; i < ara.Length; i++)
//{
// Console.WriteLine(ara[i] + "\n");
//}
int[] ar = { 11, 22, 33, 44, 55, 66,77, 88, 99 };
int[]nArray = arraaa(ar, 3, 698);
for (int i = 0;i < nArray.Length;i++)
{
Console.WriteLine(nArray[i] + "\n");
}
}
}
}
---------------------------------------------
指定索引添加数组:
3,对数组进行排序
4,数组的合并与拆分
//数组的排序算法
1,冒泡排序
2,直接插入排序
3,选择排序
//ArrayList
1,ArrayList元素添加
ArrayList元素删除
ArrayList元素遍历
ArrayList元素查找
//Hashtable 哈希表
Hashtable元素删除
Hashtable元素遍历
Hashtable元素查找
//==================== Windows 窗体 form窗体
//MDI窗体
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
Form3 form3 = new Form3();
form3.MdiParent = this;
form3.Show();
Form4 form4 = new Form4();
form4.MdiParent = this;
form4.Show();
//排列MDI子窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace c_demochuangti
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
Form3 form3 = new Form3();
form3.MdiParent = this;
form3.Show();
Form4 form4 = new Form4();
form4.MdiParent = this;
form4.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void 加载子窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
Form3 form3 = new Form3();
form3.MdiParent = this;
form3.Show();
Form4 form4 = new Form4();
form4.MdiParent = this;
form4.Show();
}
private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
}
private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.Cascade);
}
}
}
//继承窗体 以编程方式继承子窗体
public partial class Form1 : Form{ }
public partial class Form2 : Test03.Form1{ }
-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test03
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入内容");
}
else
{
label1.Text ="您输入的文本:"+ textBox1.Text;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
label1.Text = "";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
以下是修改后的继承:
using System;
using System.Text;
using System.Windows.Forms;
namespace Test03
{
public partial class Form2 : Test03.Form1
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
继承后的窗体和父窗体是属性都相同的。
namespace Test04
{
public partial class Form2 : Test04.Form1
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
---------------------------------------------------------------------------
//==================== Windows 应用程序常用控件
添加控件,对齐控件,锁定控件,删除控件。
//文本类控件:
label1.Text = "这是第一个label控件";
显示与隐藏
private void button1_Click_2(object sender, EventArgs e)
{
label1.Visible = false;
}
//按钮控件
按钮事件
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("按钮响应事件");
}
单击Enter 相当于 单击 按钮。
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "这是第一个label控件";
this.AcceptButton = button3;
MessageBox.Show("回车单击按钮");
}
单击Enter 相当于 单击 按钮。
private void button4_Click(object sender, EventArgs e)
{
this.CancelButton = button4;
}
//文本框控件 TextBox
1,创建只读文本框
private void button5_Click(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
textBox1.Text = "只读文本";
}
2,创建密码文本框
private void button1_Click_1(object sender, EventArgs e)
{
textBox1.PasswordChar = '@';
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
3,创建多行显示文本框
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Multiline = true;
textBox2.Text = "这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容";
textBox2.Height = 150;
}
4,突出显示文本框的文本
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Multiline = true;
textBox2.Text = "这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容";
textBox2.Height = 150;
textBox2.SelectionStart = 5;
textBox2.SelectionLength= 5;
}
5,响应文本框的文本更改事件
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text=textBox1.Text;
}
//文本框控件 RichTextBox
1,显示滚动条
2,设置字体属性
3,显示为超链接样式
4,设置段落式格式