C#```

TryParse

cs 复制代码
int x;
bool b = int.TryParse("100", out x);
Console.WriteLine(b);

格式转换

cs 复制代码
//浮点型转decimal,必须强转
double f = 0.1;
decimal m = (decimal)f;
//转float是ToSingle
Convert.ToSingle(f);
DateTime d = DateTime.Now;
Console.WriteLine(d.ToString("yyyy-MMMM-dddd"));

空值

可以判断三种情况,长度 text1.length == 0,会分配空间

==或equals默认只能比较"值类型"或"字符串"类型,不能比较对象类型

equals,不区分大小写

string属于引用类型,需要当值类型来使用

cs 复制代码
string text1 = "";
string text2 = string.Empty;

空对象

没有长度,不会分配空间

cs 复制代码
string text2 = null;

字符串拼接(StringBuilder)

cs 复制代码
 StringBuilder sb = new StringBuilder("本周课程信息:");
 string message = "语文";
 sb.AppendFormat($"周一:{message}");
 string str = sb.ToString();
 Console.WriteLine(str);//本周课程信息:周一:语文

转义字符

在App.config文件中,不需要转义,读取字符串,\ 打断点的时候会自动变成 \\

字符串分割、连接、替换

cs 复制代码
string str = "AB EF HU 00";
string[] s = str.Split();//默认空格分割 
str = string.Join("_", s);
Console.WriteLine(str);
Console.WriteLine(string.Join("+",s));
Console.WriteLine(str.Replace(' ','_'));

只读自动属性

自动属性:编译器自动生成 getset 访问器及其背后的字段,不需要显式地写出这些代码

编译器为自动属性创建一个隐藏的私有字段,并且你无法直接访问该字段

cs 复制代码
public Classroom()
{
    a = 20;
}
public int a{get;} = 10;

只读字段

cs 复制代码
private readonly int id = 10;
public Classroom()
{
    id = 20;
}

构造器复用

cs 复制代码
public A(int a,int b)
{
    this.a = a;
    this.b = b;
}
public A(int a,int b,int c):this(a,b)
{
    this.c = c;
}

对象(属性)初始化器

只能初始化属性,可以和构造器配合使用

cs 复制代码
Student s = new Student()
{
    id = 1;
    name = "tom";
};

泛型

List<T>

集合赋值,不能直接new_list = old_list,都是指向同一个地址

cs 复制代码
List<string> old_list = new List<string>();
List<string> new_list = new List<string>(old_list);

集合、数组转换,ToList()、ToArray()

list.AddRange(array);将数组直接添加到集合

list.RemoveAt(2);按照索引删除

Dictionary<K,V>

初始化,通过key获得对象,遍历key,遍历value

cs 复制代码
Dictionary<string, Student> s = new Dictionary<string, Student>
{
    ["first"] = new Student("tom", 1),
    ["second"] = new Student("jack", 2)
};

Console.WriteLine(s["first"]);//输出的是对象
Console.WriteLine(s["first"].name);

foreach (string item in s.Keys)
    Console.Write(item + " ");

Console.WriteLine();

foreach (Student item in s.Values)
    Console.Write(item.ToString() + " ");

事件

1、定义事件 2、处理事件 3、关联事件

单击一个按钮,就输出对应的一段话

可以在属性Tag存数据,从sender取出来

优化

会先把控件加到Controls集合中

要选择三个按钮,根据Tag判断,加上btn_

遍历Controls集合,可能其他控件的Tag为空,加判断

cs 复制代码
public Form1()
{
    InitializeComponent();
    //this.btn1.Click += new System.EventHandler(this.btn_Click);
    //this.btn2.Click += new System.EventHandler(this.btn_Click);
    //this.btn3.Click += new System.EventHandler(this.btn_Click);
    foreach(Control item in Controls)
    {
        if(item.Tag != null && item.Tag.ToString().Contains("btn_"))
        {
            item.Click += new System.EventHandler(this.btn_Click);
        }
    }
}

private void btn_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    MessageBox.Show($"{btn.Text}    {btn.Tag.ToString()}");
}

新增两个按钮,关联事件,移除事件

cs 复制代码
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Event(true);
        }
        public void Event(bool isAdd)
        {
            foreach (Control item in Controls)
            {
                if (item.Tag != null && item.Tag.ToString().Contains("btn_"))
                {
                    if (isAdd)
                        item.Click += new System.EventHandler(this.btn_Click);
                    else
                        item.Click -= new System.EventHandler(this.btn_Click);
                }
            }
        }
        private void btn4_Click(object sender, EventArgs e)
        {
            Event(true);
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            Event(false);
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            MessageBox.Show($"{btn.Text}    {btn.Tag.ToString()}");
        }

ShowDialog

ShowDialog关闭后有返回值

在旧窗体中点击按钮开启新窗体

cs 复制代码
private void button1_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    DialogResult result = f.ShowDialog();
    if(result == DialogResult.OK)
    {
        MessageBox.Show(f.Tag.ToString());
    }
}

在新窗体中点击按钮关闭新窗体

cs 复制代码
private void button1_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
    this.Tag = "123";
    this.Close();
}
相关推荐
secondyoung19 小时前
Markdown转换为Word:Pandoc模板使用指南
开发语言·经验分享·笔记·c#·编辑器·word·markdown
andyguo1 天前
AI模型测评平台工程化实战十二讲(第五讲:大模型测评分享功能:安全、高效的结果展示与协作)
人工智能·安全·c#
大飞pkz1 天前
【设计模式】访问者模式
开发语言·设计模式·c#·访问者模式
LateFrames1 天前
用 【C# + Winform + MediaPipe】 实现人脸468点识别
python·c#·.net·mediapipe
R-G-B2 天前
【14】C#实战篇——C++动态库dll 接口函数将char* strErr字符串 传给C# ,并且在winform的MessageBox和listbox中显示。C++ string 日志传给 C#
c++·c#·strerr字符串传给c#·动态库dll传递字符串给c#·string日志传给c#·c++ string传给 c#·c++底层函数日志传给c#显示
我是唐青枫2 天前
深入掌握 FluentMigrator:C#.NET 数据库迁移框架详解
数据库·c#·.net
tiankongdeyige2 天前
Unity学习之C#的反射机制
学习·unity·c#
绿荫阿广2 天前
用纯.NET开发并制作一个智能桌面机器人(六):使用.NET开发一个跨平台功能完善的小智AI客户端
c#·.net·asp.net core·maui·winui
周杰伦fans2 天前
c#设计模式—访问者模式
c#·.net
疯狂的Alex2 天前
【C#避坑实战系列文章15】C# WinForm 上位机开发:解决串口粘包+LiveCharts卡顿+InfluxDB存储(免费代码+仿真工具)
sqlite·c#·上位机·串口通信·livechars·c#硬件对接