服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#网络编程中常用特性介绍

C#网络编程中常用特性介绍

2023-02-14 15:20.NET开发菜鸟 C#

这篇文章介绍了C#网络编程中常用特性,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

特性一:委托

委托是C#语言中特有的概念,相当于C/C++中的函数指针,与C/C++中函数指针的不同之处是:委托是面向对象的、类型安全的和保险的,是引用类型。因此,对委托的使用要

“先定义、后声明,接着实例化、然后作为参数传递给方法,最后才能使用”。

1、定义委托使用关键字delegate:

delegate  void SomeDelegate(type1 para1,......typen paran);

2、声明委托:

SomeDelegate  d;

3、实例化委托:

?
1
d=new SomeDelegate(obj.InstanceMethod);

其中obj是对象,InstanceMethod是它的实例方法。

4、作为参数传递给方法

?
1
someMethod(d);

5、最后在此方法的实现代码中使用委托

?
1
2
3
4
5
6
7
private  void  someMethod(SomeDelegate  someDelegate)
{
   .....
   //使用委托
  someDelegate(arg1,arg2,....,argn);
  ...... 
}

通过委托SomeDelegate实现对方法InstanceMethod的调用,调用还必须有一个前提条件:方法InstanceMethod有参数且和定义SomeDelegate的参数一致,并且返回类型相同(本例中为void)。方法InstanceMethod的定义:

?
1
2
3
4
5
private  void  InstanceMethod(type1 para1,type2 para2,......,typen paran)
{
   //方法体
  .....
}

委托的实例化中的参数既可以是实例方法,也可以是静态方法。

使用委托实现“文字抄写员”的小程序,界面如下:

在下方文本框中编辑文字,勾选“书写到”组框中的“文本区1”和(或)“文本区2”复选框后单击“提交”按钮,程序会自动将文本框中的文字“抄写”到对应的用户勾选的文本区中去。

C#网络编程中常用特性介绍

代码实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 DelegateDemo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
 
        //1、定义委托
        private delegate void WriteToTextBox(string strTxt);
        //2、声明委托
        private WriteToTextBox writeToTextBox;
 
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (chbOne.Checked)
            {
                gbJobOne.Text = "运行中......";
                gbJobOne.Refresh();
                txtJobOne.Clear();
                //3、实例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox1);
                //4、将委托作为方法的参数进行传递
                WriteText(writeToTextBox);
                gbJobOne.Text = "任务1完成";
            }
            if (chbTwo.Checked)
            {
 
                gbJobTwo.Text = "运行中......";
                gbJobTwo.Refresh();
                txtJobTwo.Clear();
                //3、实例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox2);
                //4、将委托作为方法的参数进行传递
                WriteText(writeToTextBox);
                gbJobTwo.Text = "任务2完成";
            }
        }
 
 
        private void WriteText(WriteToTextBox writeMethod)
        {
            string strData = this.txt_Input.Text;
            writeMethod(strData);
        }
        private void WriteTextBox1(string strTxt)
        {
            this.txtJobOne.Text = strTxt;
        }
 
        private void WriteTextBox2(string strTxt)
        {
            this.txtJobTwo.Text = strTxt;
        }
 
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //设置文本框获取焦点
            this.ActiveControl = this.txt_Input;
            //this.txt_Input.Focus();
        }
    }
}

特性2:多线程

多线程的具体介绍请参考博文:http://www.tuohang.net/article/263015.html

使用多线程实现上一节的程序,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
using System.Threading;//引入多线程的命名空间
 
namespace DelegateDemo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
 
        //1、定义委托
        private delegate void WriteToTextBox(string strTxt);
        //2、声明委托
        private WriteToTextBox writeToTextBox;
 
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
           //创建线程1
            Thread thread1 = new Thread(new ThreadStart(ExecuteTsk1));
            //启动线程1
            thread1.Start();
 
            //创建线程2
            Thread thread2 = new Thread(new ThreadStart(ExecuteTsk2));
            //启动线程2
            thread2.Start();
 
        }
 
 
        private void ExecuteTsk1()
        {
            if (chbOne.Checked)
            {
                gbJobOne.Text = "运行中......";
                gbJobOne.Refresh();
                txtJobOne.Clear();
                //3、实例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox1);
                //4、将委托作为方法的参数进行传递
                WriteText(writeToTextBox);
                gbJobOne.Text = "任务1完成";
            }
        }
 
        private void ExecuteTsk2()
        {
            if (chbTwo.Checked)
            {
 
                gbJobTwo.Text = "运行中......";
                gbJobTwo.Refresh();
                txtJobTwo.Clear();
                //3、实例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox2);
                //4、将委托作为方法的参数进行传递
                WriteText(writeToTextBox);
                gbJobTwo.Text = "任务2完成";
            }
        }
 
 
        private void WriteText(WriteToTextBox writeMethod)
        {
            string strData = this.txt_Input.Text;
            writeMethod(strData);
        }
        private void WriteTextBox1(string strTxt)
        {
            this.txtJobOne.Text = strTxt;
        }
 
        private void WriteTextBox2(string strTxt)
        {
            this.txtJobTwo.Text = strTxt;
        }
 
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //设置文本框获取焦点
            this.ActiveControl = this.txt_Input;
            //允许跨线程调用
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}

特性3:C#方法回调

C#回调的具体介绍请参照博文:http://www.tuohang.net/article/263015.html

使用委托、多线程和C#的方法回调机制实现上一节的程序,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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;
using System.Threading;//引入多线程的命名空间
 
namespace DelegateDemo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
 
        //1、定义委托
        private delegate void WriteToTextBox(string strTxt);
        //2、声明委托
        private WriteToTextBox writeToTextBox;
 
        //定义并声明操作文本区1的回调
        private delegate void WriteTxtJobOneCallBack(string strValue);
        WriteTxtJobOneCallBack writeTxtJobOneCallBack;
 
        //定义并声明操作文本区2的回调
        private delegate void WriteTxtJobTwoCallBack(string strValue);
        WriteTxtJobOneCallBack writeTxtJobTwoCallBack;
 
        //定义并声明操作"任务1"分组框的回调
        private delegate void ShowGroupOneCallBack(string strValue);
        ShowGroupOneCallBack showGroupOneCallBack;
 
        //定义并声明操作"任务2"分组框的回调
        private delegate void ShowGroupTwoCallBack(string strValue);
        ShowGroupOneCallBack showGroupTwoCallBack;
 
 
 
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
           //创建线程1
            Thread thread1 = new Thread(new ThreadStart(ExecuteTsk1));
            //启动线程1
            thread1.Start();
 
            //创建线程2
            Thread thread2 = new Thread(new ThreadStart(ExecuteTsk2));
            //启动线程2
            thread2.Start();
 
        }
 
 
        private void ExecuteTsk1()
        {
            if (chbOne.Checked)
            {
                //3、实例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox1);
                //4、将委托作为方法的参数进行传递
                WriteText(writeToTextBox);
                //使用回调
                this.gbJobOne.Invoke(showGroupOneCallBack, "任务1");
            }
        }
 
        private void ExecuteTsk2()
        {
            if (chbTwo.Checked)
            {
                //3、实例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox2);
                //4、将委托作为方法的参数进行传递
                WriteText(writeToTextBox);
                //使用回调
                this.gbJobTwo.Invoke(showGroupTwoCallBack, "任务2");
            }
        }
 
        /// <summary>
        /// 执行自定义委托
        /// </summary>
        /// <param name="writeMethod"></param>
        private void WriteText(WriteToTextBox writeMethod)
        {
            string strData = this.txt_Input.Text;
            writeMethod(strData);
        }
 
        /// <summary>
        /// 给文本区1赋值
        /// </summary>
        /// <param name="strTxt"></param>
        private void WriteTextBox1(string strTxt)
        {
            //使用回调
            this.txtJobOne.Invoke(writeTxtJobOneCallBack, strTxt);
        }
 
        /// <summary>
        /// 给文本区2赋值
        /// </summary>
        /// <param name="strTxt"></param>
        private void WriteTextBox2(string strTxt)
        {
            //使用回调
            this.txtJobTwo.Invoke(writeTxtJobTwoCallBack, strTxt);
        }
 
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //设置文本框获取焦点
            this.ActiveControl = this.txt_Input;
 
            //实例化回调
            writeTxtJobOneCallBack = new WriteTxtJobOneCallBack(WriteToTextJobOne);
            writeTxtJobTwoCallBack = new WriteTxtJobOneCallBack(WriteToTextJobTwo);
            showGroupOneCallBack = new ShowGroupOneCallBack(ShowGroupOne);
            showGroupTwoCallBack = new ShowGroupOneCallBack(ShowGroupTwo);
 
        }
 
        /// <summary>
        /// 操作文本区1的回调要执行的方法
        /// </summary>
        /// <param name="strValue"></param>
        private void WriteToTextJobOne(string strValue)
        {
            this.txtJobOne.Text = strValue;
        }
 
        /// <summary>
        /// 操作文本区2的回调要执行的方法
        /// </summary>
        /// <param name="strValue"></param>
        private void WriteToTextJobTwo(string strValue)
        {
            this.txtJobTwo.Text = strValue;
        }
 
        /// <summary>
        /// 操作"任务1"分组框的回调要执行的方法
        /// </summary>
        /// <param name="strValue"></param>
        private void ShowGroupOne(string strValue)
        {
            this.gbJobOne.Text = strValue;
        }
 
        /// <summary>
        /// 操作"任务2"分组框的回调要执行的方法
        /// </summary>
        /// <param name="strValue"></param>
        private void ShowGroupTwo(string strValue)
        {
            this.gbJobTwo.Text = strValue;
        }
    }
}

到此这篇关于C#网络编程中常用特性的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/dotnet261010/p/6206068.html

延伸 · 阅读

精彩推荐
  • C#深入学习C#网络编程之HTTP应用编程(下)

    深入学习C#网络编程之HTTP应用编程(下)

    这篇文章主要介绍了深入学习C#网络编程之HTTP应用编程的相关知识,文中讲解的非常详细,帮助大家更好的学习c#网络编程,感兴趣的朋友可以了解下...

    一线码农8252022-09-14
  • C#c# 动态加载dll文件,并实现调用其中的简单方法

    c# 动态加载dll文件,并实现调用其中的简单方法

    下面小编就为大家带来一篇c# 动态加载dll文件,并实现调用其中的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来...

    C#教程网11672021-12-22
  • C#C#6.0中你可能不知道的新特性总结

    C#6.0中你可能不知道的新特性总结

    C# 6 已经出来很久了,但最近发现真的有必要整理下,下面这篇文章主要给大家介绍了关于C#6.0中一些你可能不知道的新特性的相关资料,文中通过示例代码...

    码农阿宇6122022-02-23
  • C#C#之set与get方法的用法案例

    C#之set与get方法的用法案例

    这篇文章主要介绍了C#之set与get方法的用法案例,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    悬弧11872022-11-28
  • C#C#串口编程实例代码

    C#串口编程实例代码

    这篇文章主要为大家详细介绍了C#串口编程实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    温暖的小明7492021-12-03
  • C#Unity3D Shader实现动态屏幕遮罩

    Unity3D Shader实现动态屏幕遮罩

    这篇文章主要为大家详细介绍了Unity3D Shader实现动态屏幕遮罩效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    星空不语11942022-07-08
  • C#WinForm使用DataGridView实现类似Excel表格的查找替换功能

    WinForm使用DataGridView实现类似Excel表格的查找替换功能

    这篇文章主要介绍了WinForm使用DataGridView实现类似Excel表格的查找替换功能,现在小编通过本文给大家分享查找替换实现过程,需要的朋友可以参考下...

    XSpringSun8622022-11-27
  • C#详解c# 深克隆与浅克隆

    详解c# 深克隆与浅克隆

    这篇文章主要介绍了c# 深克隆与浅克隆的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    团队buff工具人6722022-09-23