欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#網(wǎng)絡(luò)編程中常用特性介紹

 更新時(shí)間:2022年02月24日 17:05:20   作者:.NET開發(fā)菜鳥  
這篇文章介紹了C#網(wǎng)絡(luò)編程中常用特性,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

特性一:委托

委托是C#語言中特有的概念,相當(dāng)于C/C++中的函數(shù)指針,與C/C++中函數(shù)指針的不同之處是:委托是面向?qū)ο蟮?、類型安全的和保險(xiǎn)的,是引用類型。因此,對委托的使用要

“先定義、后聲明,接著實(shí)例化、然后作為參數(shù)傳遞給方法,最后才能使用”。

1、定義委托使用關(guān)鍵字delegate:

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

2、聲明委托:

SomeDelegate  d;

3、實(shí)例化委托:

d=new SomeDelegate(obj.InstanceMethod);

其中obj是對象,InstanceMethod是它的實(shí)例方法。

4、作為參數(shù)傳遞給方法

someMethod(d);

5、最后在此方法的實(shí)現(xiàn)代碼中使用委托

private? void? someMethod(SomeDelegate? someDelegate)
{
?? .....
?? //使用委托
? someDelegate(arg1,arg2,....,argn);
? ......?
}

通過委托SomeDelegate實(shí)現(xiàn)對方法InstanceMethod的調(diào)用,調(diào)用還必須有一個(gè)前提條件:方法InstanceMethod有參數(shù)且和定義SomeDelegate的參數(shù)一致,并且返回類型相同(本例中為void)。方法InstanceMethod的定義:

private? void? InstanceMethod(type1 para1,type2 para2,......,typen paran)
{
?? //方法體
? .....
}

委托的實(shí)例化中的參數(shù)既可以是實(shí)例方法,也可以是靜態(tài)方法。

使用委托實(shí)現(xiàn)“文字抄寫員”的小程序,界面如下:

在下方文本框中編輯文字,勾選“書寫到”組框中的“文本區(qū)1”和(或)“文本區(qū)2”復(fù)選框后單擊“提交”按鈕,程序會自動將文本框中的文字“抄寫”到對應(yīng)的用戶勾選的文本區(qū)中去。

代碼實(shí)現(xiàn)如下:

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 = "運(yùn)行中......";
                gbJobOne.Refresh();
                txtJobOne.Clear();
                //3、實(shí)例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox1);
                //4、將委托作為方法的參數(shù)進(jìn)行傳遞
                WriteText(writeToTextBox);
                gbJobOne.Text = "任務(wù)1完成";
            }
            if (chbTwo.Checked)
            {

                gbJobTwo.Text = "運(yùn)行中......";
                gbJobTwo.Refresh();
                txtJobTwo.Clear();
                //3、實(shí)例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox2);
                //4、將委托作為方法的參數(shù)進(jìn)行傳遞
                WriteText(writeToTextBox);
                gbJobTwo.Text = "任務(wù)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)
        {
            //設(shè)置文本框獲取焦點(diǎn)
            this.ActiveControl = this.txt_Input;
            //this.txt_Input.Focus();
        }
    }
}

特性2:多線程

多線程的具體介紹請參考博文:http://www.dbjr.com.cn/article/238731.htm

使用多線程實(shí)現(xiàn)上一節(jié)的程序,代碼如下:

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)
        {
           //創(chuàng)建線程1
            Thread thread1 = new Thread(new ThreadStart(ExecuteTsk1));
            //啟動線程1
            thread1.Start();

            //創(chuàng)建線程2
            Thread thread2 = new Thread(new ThreadStart(ExecuteTsk2));
            //啟動線程2
            thread2.Start();

        }


        private void ExecuteTsk1()
        {
            if (chbOne.Checked)
            {
                gbJobOne.Text = "運(yùn)行中......";
                gbJobOne.Refresh();
                txtJobOne.Clear();
                //3、實(shí)例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox1);
                //4、將委托作為方法的參數(shù)進(jìn)行傳遞
                WriteText(writeToTextBox);
                gbJobOne.Text = "任務(wù)1完成";
            }
        }

        private void ExecuteTsk2()
        {
            if (chbTwo.Checked)
            {

                gbJobTwo.Text = "運(yùn)行中......";
                gbJobTwo.Refresh();
                txtJobTwo.Clear();
                //3、實(shí)例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox2);
                //4、將委托作為方法的參數(shù)進(jìn)行傳遞
                WriteText(writeToTextBox);
                gbJobTwo.Text = "任務(wù)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)
        {
            //設(shè)置文本框獲取焦點(diǎn)
            this.ActiveControl = this.txt_Input;
            //允許跨線程調(diào)用
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}

特性3:C#方法回調(diào)

C#回調(diào)的具體介紹請參照博文:http://www.dbjr.com.cn/article/238731.htm#_label3

使用委托、多線程和C#的方法回調(diào)機(jī)制實(shí)現(xiàn)上一節(jié)的程序,代碼如下:

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;

        //定義并聲明操作文本區(qū)1的回調(diào)
        private delegate void WriteTxtJobOneCallBack(string strValue);
        WriteTxtJobOneCallBack writeTxtJobOneCallBack;

        //定義并聲明操作文本區(qū)2的回調(diào)
        private delegate void WriteTxtJobTwoCallBack(string strValue);
        WriteTxtJobOneCallBack writeTxtJobTwoCallBack;

        //定義并聲明操作"任務(wù)1"分組框的回調(diào)
        private delegate void ShowGroupOneCallBack(string strValue);
        ShowGroupOneCallBack showGroupOneCallBack;

        //定義并聲明操作"任務(wù)2"分組框的回調(diào)
        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)
        {
           //創(chuàng)建線程1
            Thread thread1 = new Thread(new ThreadStart(ExecuteTsk1));
            //啟動線程1
            thread1.Start();

            //創(chuàng)建線程2
            Thread thread2 = new Thread(new ThreadStart(ExecuteTsk2));
            //啟動線程2
            thread2.Start();

        }


        private void ExecuteTsk1()
        {
            if (chbOne.Checked)
            {
                //3、實(shí)例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox1);
                //4、將委托作為方法的參數(shù)進(jìn)行傳遞
                WriteText(writeToTextBox);
                //使用回調(diào)
                this.gbJobOne.Invoke(showGroupOneCallBack, "任務(wù)1");
            }
        }

        private void ExecuteTsk2()
        {
            if (chbTwo.Checked)
            {
                //3、實(shí)例化委托
                writeToTextBox = new WriteToTextBox(WriteTextBox2);
                //4、將委托作為方法的參數(shù)進(jìn)行傳遞
                WriteText(writeToTextBox);
                //使用回調(diào)
                this.gbJobTwo.Invoke(showGroupTwoCallBack, "任務(wù)2");
            }
        }

        /// <summary>
        /// 執(zhí)行自定義委托
        /// </summary>
        /// <param name="writeMethod"></param>
        private void WriteText(WriteToTextBox writeMethod)
        {
            string strData = this.txt_Input.Text;
            writeMethod(strData);
        }

        /// <summary>
        /// 給文本區(qū)1賦值
        /// </summary>
        /// <param name="strTxt"></param>
        private void WriteTextBox1(string strTxt)
        {
            //使用回調(diào)
            this.txtJobOne.Invoke(writeTxtJobOneCallBack, strTxt);
        }

        /// <summary>
        /// 給文本區(qū)2賦值
        /// </summary>
        /// <param name="strTxt"></param>
        private void WriteTextBox2(string strTxt)
        {
            //使用回調(diào)
            this.txtJobTwo.Invoke(writeTxtJobTwoCallBack, strTxt);
        }

        /// <summary>
        /// 窗體加載事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //設(shè)置文本框獲取焦點(diǎn)
            this.ActiveControl = this.txt_Input;

            //實(shí)例化回調(diào)
            writeTxtJobOneCallBack = new WriteTxtJobOneCallBack(WriteToTextJobOne);
            writeTxtJobTwoCallBack = new WriteTxtJobOneCallBack(WriteToTextJobTwo);
            showGroupOneCallBack = new ShowGroupOneCallBack(ShowGroupOne);
            showGroupTwoCallBack = new ShowGroupOneCallBack(ShowGroupTwo);

        }

        /// <summary>
        /// 操作文本區(qū)1的回調(diào)要執(zhí)行的方法
        /// </summary>
        /// <param name="strValue"></param>
        private void WriteToTextJobOne(string strValue)
        {
            this.txtJobOne.Text = strValue;
        }

        /// <summary>
        /// 操作文本區(qū)2的回調(diào)要執(zhí)行的方法
        /// </summary>
        /// <param name="strValue"></param>
        private void WriteToTextJobTwo(string strValue)
        {
            this.txtJobTwo.Text = strValue;
        }

        /// <summary>
        /// 操作"任務(wù)1"分組框的回調(diào)要執(zhí)行的方法
        /// </summary>
        /// <param name="strValue"></param>
        private void ShowGroupOne(string strValue)
        {
            this.gbJobOne.Text = strValue;
        }

        /// <summary>
        /// 操作"任務(wù)2"分組框的回調(diào)要執(zhí)行的方法
        /// </summary>
        /// <param name="strValue"></param>
        private void ShowGroupTwo(string strValue)
        {
            this.gbJobTwo.Text = strValue;
        }
    }
}

到此這篇關(guān)于C#網(wǎng)絡(luò)編程中常用特性的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c#中使用自動屬性減少代碼輸入量

    c#中使用自動屬性減少代碼輸入量

    .Net 3.0中的自動屬性可以大幅度降低我們輸入的代碼量,需要的朋友可以參考下
    2012-12-12
  • C#實(shí)現(xiàn)圖片上傳(PC端和APP)保存及 跨域上傳說明

    C#實(shí)現(xiàn)圖片上傳(PC端和APP)保存及 跨域上傳說明

    這篇文章主要介紹了C#實(shí)現(xiàn)圖片上傳(PC端和APP)保存及 跨域上傳說明的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • C#語法糖(Csharp Syntactic sugar)大匯總

    C#語法糖(Csharp Syntactic sugar)大匯總

    首先需要聲明的是“語法糖”這個(gè)詞絕非貶義詞,它可以給我?guī)矸奖悖且环N便捷的寫法,編譯器會幫我們做轉(zhuǎn)換;而且可以提高開發(fā)編碼的效率,在性能上也不會帶來損失。這讓java開發(fā)人員羨慕不已,呵呵。
    2010-06-06
  • C#使用ODBC與OLEDB連接數(shù)據(jù)庫的方法示例

    C#使用ODBC與OLEDB連接數(shù)據(jù)庫的方法示例

    這篇文章主要介紹了C#使用ODBC與OLEDB連接數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式分析了C#基于ODBC與OLEDB實(shí)現(xiàn)數(shù)據(jù)庫連接操作簡單操作技巧,需要的朋友可以參考下
    2017-05-05
  • C#的winform如何嵌套另一個(gè)exe程序

    C#的winform如何嵌套另一個(gè)exe程序

    這篇文章主要介紹了C#的winform如何嵌套另一個(gè)exe程序問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • C# 如何合并和拆分PDF文件

    C# 如何合并和拆分PDF文件

    這篇文章主要介紹了C# 如何合并和拆分PDF文件,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • C#位移的介紹與例子

    C#位移的介紹與例子

    很多人提問,不知道C#位移,可能有些人在面試中也遇到過
    2013-04-04
  • Unity實(shí)現(xiàn)俄羅斯方塊(二)

    Unity實(shí)現(xiàn)俄羅斯方塊(二)

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)俄羅斯方塊的第一部分代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C# WPF 父控件通過使用可視化樹找到子控件的示例代碼

    C# WPF 父控件通過使用可視化樹找到子控件的示例代碼

    這篇文章主要介紹了C# WPF 父控件通過使用可視化樹找到子控件的示例代碼,需要的朋友可以參考下
    2018-08-08
  • C#集合之自定義集合類

    C#集合之自定義集合類

    這篇文章介紹了C#集合之自定義集合類,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評論