Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common features in C # network programming

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about the common features in C# network programming. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Feature 1: delegate

Delegate is a unique concept in C # language, which is equivalent to the function pointer in Cpicket +. The difference between delegate and function pointer in Cpicket + is that delegate is object-oriented, type-safe and safe, and it is a reference type. Therefore, the use of delegates should be

Define, declare, instantiate, and then pass as parameters to the method before you can use it.

1. Define the keyword delegate for delegation:

Delegate void SomeDelegate (type1 para1,.typen paran)

2. Declare the delegation:

SomeDelegate d

3. Instantiate delegate: d=new SomeDelegate (obj.InstanceMethod)

Where obj is the object and InstanceMethod is its instance method.

4. Pass it as a parameter to the method someMethod (d). 5. Finally, use the delegate private void someMethod (SomeDelegate someDelegate) {. / / use delegated someDelegate (arg1,arg2,....,argn);. }

The call to the method InstanceMethod is implemented by delegating SomeDelegate, and the call must also have a prerequisite: the method InstanceMethod has parameters and is the same as the parameter that defines the SomeDelegate, and the return type is the same (in this case, void). Definition of method InstanceMethod:

Private void InstanceMethod (type1 para1,type2 para2,.,typen paran) {/ / method body.}

The parameters in the instantiation of a delegate can be either instance methods or static methods.

Mini Program, which uses delegation to implement "text scribe", has the following interface:

Edit the text in the text box below, check the "text area 1" and / or "text area 2" check boxes in the "write to" group box, and then click the "submit" button. The program will automatically "copy" the text in the text box to the corresponding text area checked by the user.

The code is implemented as follows:

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, define delegate private delegate void WriteToTextBox (string strTxt) / / 2. Declare that private WriteToTextBox writeToTextBox; / submit / private void btn_OK_Click (object sender, EventArgs e) {if (chbOne.Checked) {gbJobOne.Text = "running." GbJobOne.Refresh (); txtJobOne.Clear (); / / 3, instantiate the delegate writeToTextBox = new WriteToTextBox (WriteTextBox1); / / 4, pass the delegate as a parameter to the method WriteText (writeToTextBox); gbJobOne.Text = "Task 1 completed" } if (chbTwo.Checked) {gbJobTwo.Text = "running."; gbJobTwo.Refresh (); txtJobTwo.Clear (); / / 3, instantiation delegate writeToTextBox = new WriteToTextBox (WriteTextBox2) / / 4. Pass WriteText (writeToTextBox) as a parameter of the method; gbJobTwo.Text = "Task 2 completed";}} 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 } / form load event / private void FrmMain_Load (object sender, EventArgs e) {/ / set text box to get focus this.ActiveControl = this.txt_Input; / / this.txt_Input.Focus () Feature 2: multithreading

For a specific introduction to multithreading, please refer to the blog post: https://www.yisu.com/article/238731.htm

Use multithreading to implement the program in the previous section, the code is as follows:

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;// introduces the multithreaded namespace namespace DelegateDemo {public partial class FrmMain: Form {public FrmMain () {InitializeComponent () } / / 1. Define delegate private delegate void WriteToTextBox (string strTxt); / / 2, declare delegate private WriteToTextBox writeToTextBox / submit / private void btn_OK_Click (object sender, EventArgs e) {/ / create thread 1 Thread thread1 = new Thread (new ThreadStart (ExecuteTsk1)); / / start thread 1 thread1.Start () / / create thread 2 Thread thread2 = new Thread (new ThreadStart (ExecuteTsk2)); / / start thread 2 thread2.Start ();} private void ExecuteTsk1 () {if (chbOne.Checked) {gbJobOne.Text = "running."; gbJobOne.Refresh () TxtJobOne.Clear (); / / 3. Instantiate delegate writeToTextBox = new WriteToTextBox (WriteTextBox1); / / 4. Pass delegate as a parameter to the method WriteText (writeToTextBox); gbJobOne.Text = "Task 1 completed" }} private void ExecuteTsk2 () {if (chbTwo.Checked) {gbJobTwo.Text = "running."; gbJobTwo.Refresh (); txtJobTwo.Clear (); / / 3. Instantiate delegate writeToTextBox = new WriteToTextBox (WriteTextBox2) / / 4. Pass WriteText (writeToTextBox) as a parameter of the method; gbJobTwo.Text = "Task 2 completed";}} 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 } / form load event / private void FrmMain_Load (object sender, EventArgs e) {/ / set text box to get focus this.ActiveControl = this.txt_Input; / / allow cross-thread calls to Control.CheckForIllegalCrossThreadCalls = false } property 3:C# method callback

For more information on C# callback, please refer to the blog post: https://www.yisu.com/article/238731.htm#_label3

Use delegate, multithreading and C# method callback mechanism to implement the program in the previous section, the code is as follows:

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;// introduces the multithreaded namespace namespace DelegateDemo {public partial class FrmMain: Form {public FrmMain () {InitializeComponent () } / / 1. Define delegate private delegate void WriteToTextBox (string strTxt); / / 2, declare delegate private WriteToTextBox writeToTextBox; / / define and declare callback private delegate void WriteTxtJobOneCallBack (string strValue) for operating text area 1; WriteTxtJobOneCallBack writeTxtJobOneCallBack; / / define and declare callback private delegate void WriteTxtJobTwoCallBack (string strValue) for operating text area 2; WriteTxtJobOneCallBack writeTxtJobTwoCallBack / / define and declare the callback private delegate void ShowGroupOneCallBack (string strValue) that operates the Task 1 grouping box; ShowGroupOneCallBack showGroupOneCallBack; / / define and declare the callback private delegate void ShowGroupTwoCallBack (string strValue) that operates the Task 2 grouping box; ShowGroupOneCallBack showGroupTwoCallBack / submit / private void btn_OK_Click (object sender, EventArgs e) {/ / create thread 1 Thread thread1 = new Thread (new ThreadStart (ExecuteTsk1)); / / start thread 1 thread1.Start () / / create thread 2 Thread thread2 = new Thread (new ThreadStart (ExecuteTsk2)); / / start thread 2 thread2.Start ();} private void ExecuteTsk1 () {if (chbOne.Checked) {/ / 3, instantiate delegate writeToTextBox = new WriteToTextBox (WriteTextBox1) / / 4. Pass WriteText (writeToTextBox) as a parameter of the method; / / use callback this.gbJobOne.Invoke (showGroupOneCallBack, "Task 1") } private void ExecuteTsk2 () {if (chbTwo.Checked) {/ / 3, instantiate the delegate writeToTextBox = new WriteToTextBox (WriteTextBox2); / / 4, pass the delegate as a parameter to the method WriteText (writeToTextBox) / / use callback this.gbJobTwo.Invoke (showGroupTwoCallBack, "Task 2");}} / execute custom delegate / private void WriteText (WriteToTextBox writeMethod) {string strData = this.txt_Input.Text; writeMethod (strData) } / assign / private void WriteTextBox1 (string strTxt) {/ / use callback this.txtJobOne.Invoke (writeTxtJobOneCallBack, strTxt) to text area 1 } / assign to text area 2 / private void WriteTextBox2 (string strTxt) {/ / use callback this.txtJobTwo.Invoke (writeTxtJobTwoCallBack, strTxt) } / form load event / private void FrmMain_Load (object sender, EventArgs e) {/ / set text box to get focus this.ActiveControl = this.txt_Input; / / instantiate callback writeTxtJobOneCallBack = new WriteTxtJobOneCallBack (WriteToTextJobOne) WriteTxtJobTwoCallBack = new WriteTxtJobOneCallBack (WriteToTextJobTwo); showGroupOneCallBack = new ShowGroupOneCallBack (ShowGroupOne); showGroupTwoCallBack = new ShowGroupOneCallBack (ShowGroupTwo);} / operate the callback of text area 1 to execute the method / private void WriteToTextJobOne (string strValue) {this.txtJobOne.Text = strValue } / private void WriteToTextJobTwo (string strValue) {this.txtJobTwo.Text = strValue to operate the callback of text area 2 } / private void ShowGroupOne (string strValue) {this.gbJobOne.Text = strValue to be executed by the callback of the "Task 1" grouping box The method / private void ShowGroupTwo (string strValue) {this.gbJobTwo.Text = strValue;}} to be executed in the callback of the "Task 2" grouping box. Thank you for reading! This is the end of this article on "what features are commonly used in C# network programming". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report