Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of implementing anonymity in C #?

Shulou Source: shulou.com Published: 2022-06-02 08:55:42 10月27日 Update

This article introduces the knowledge of "what is the method of realizing anonymity in C#". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The C# anonymous method allows us to define blocks of code that are acceptable to the delegate object. This feature leaves out an extra step that we want to pass to a delegate's small block of code when we create the delegate. It also eliminates the confusion of small and medium-sized methods in class code. Let's see: for example, we have a collection of strings named MyCollection. This class has a method: get all the items in the collection that meet the user-provided filtering criteria, and the caller decides whether a particular item in the collection meets the criteria and is retrieved as part of the returned array from this method.

Public class MyCollection {public delegate bool SelectItem (string sItem); public string [] GetFilteredItemArray (SelectItem itemFilter) {List sList = new List (); foreach (string sItem in m_sList) {if (itemFilter (sItem) = = true) sList.Add (sItem);} return sList.ToArray ();} public List ItemList {get {return mechsList;}} private List m_sList = new List ();}

We can use the classes defined above to write the code as follows:

Public class Program {public static void Main (string [] args) {MyCollection objMyCol = new MyCollection (); objMyCol.ItemList.Add ("Aditya"); objMyCol.ItemList.Add ("Tanu"); objMyCol.ItemList.Add ("Manoj"); objMyCol.ItemList.Add ("Ahan"); objMyCol.ItemList.Add ("Hasi"); / / get an array of character items in the set that begin with the letter'A 'string [] AStrings = objMyCol.GetFilteredItemArray (FilterStringWithA) Console.WriteLine ("- Strings starting with letter''Console.WriteLine' -"); foreach (string s in AStrings) {Console.WriteLine (s);} / / get the array of character items in the collection that begin with the letter'T' string [] TStrings = objMyCol.GetFilteredItemArray (FilterStringWithT); Console.WriteLine ("- Strings starting with letter''Tunable' -"); foreach (string s in TStrings) {Console.WriteLine (s) }} public static bool FilterStringWithA (string sItem) {if (sItem [0] = =''Aging') return true; else return false;} public static bool FilterStringWithT (string sItem) {if (sItem [0] = =''tasked') return true; else return false;}} "what is the way for C# to achieve anonymity"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Methods codes characters arrays delegates content letters beginnings more knowledge utility special confusion achievement and then small and medium-sized guidelines functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Huawei Shulou Information vpn OPPO Reno