C # Use lambda expressions to pass functions as parameters or attributes across classes

Source: Internet
Author: User

During coding, since we started to perform simple testing and development under winform, we wanted to divide the code into different classes because we had more code, however, because the original test is in the same form, it is very convenient for function calling. Once the cross-class is passed, we will find that the coupling degree of this function is too high, so that I don't know how to decouple it into the class. In this case, you may wish to use the Delegate-type Func and Action.

Below is the simple code written in winform during the initial test.

 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Threading.Tasks;using System.Windows.Forms;namespace FunctionPass{    public partial class FormMain : Form    {        public FormMain()        {            InitializeComponent();        }        private void buttonTest_Click(object sender, EventArgs e)        {            test();        }        public void test()        {            string sourceStr = collectData();            string digitStr = fetchDigit(sourceStr);            MessageBox.Show("source:" + sourceStr + "\r\n digit:" + digitStr);        }        public string collectData()        {            return Guid.NewGuid().ToString();        }        public string fetchDigit(string sourceStr)        {            Regex regex = new Regex(@"\d*");            MatchCollection digitCollection=regex.Matches(sourceStr);            string digitStr = "";            foreach (Match digitMatch in digitCollection)        {                digitStr += digitMatch;        }                       return digitStr;        }    }}

Here we use the colloectData function to collect data, extract the numbers in the data through fetchDigit, and then use the test function to display the results.

 

Now I want to make the display result into a separate class (such as the Test class) for better extension.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace FunctionPass{    public class Test    {        public void test()        {            string sourceStr = collectData();            string digitStr = fetchDigit(sourceStr);            MessageBox.Show("source:" + sourceStr + "\r\n digit:" + digitStr);        }    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.