When learning C # Anonymous class, write demo, start is just two code, and then some ideas gradually in my mind, put these ideas written down, On the one hand is to witness their progress, on the other hand also share with you how to write an "elegant" program.
class program { public delegate void say (Object word); Public delegate int add (int x, int y); public delegate int sub (int x, int y); public delegate int mul (int x, int y); public delegate int div (int x, int y); static void main (String[] args) { say sayresult = delegate (Object word) { console.writeline (word); }; &nbsP; add add = delegate (Int x, int y) { return x + y; }; sub sub = delegate (int x, int y) { return x - y; }; mul mul = delegate (int x, int y) { return x * y; }; div div = delegate (int x, int y) { return x / y; }; sayresult (Add (5,6)); sayresult (Sub (5,6)); sayresult (Mul (5,6)); &NBsp; sayresult (Div (5,6)); } }
5 4 A delegate in the form of the same signature, the same return type. c# provides some good delegate types ( Span style= "Font-family:times New Roman" >action func ), actually does not need me to define what.
action<object> sayresult = delegate (Object word) { Console.WriteLine (word); }; func<int, int, int> add = delegate (int x, int y) { return x + y; }; func<int, int, int> sub = delegate (int x, int y) { return x - y; }; func<int, int, int> mul = delegate (int x, int y) { return x * y; }; func<int, int, int> div = delegate (int x, int y) { return x / y; }; &nbSp; sayresult (Add (5,6)); sayresult (Sub (5,6)); sayresult (Mul (5,6)); sayresult (Div (5,6));
This can be less than 5 lines on the amount of code , but 4 anonymous functions have a certain relevance in both form and function, thinking about integrating the 4 and anonymous functions.
This is the idea that I started:
List<func<int, int, int>> caculate = new List<func<int, int, int>> (); Caculate.addrange (new func<int, int, int>[] {Add, sub, Mul, Div}); Sayresult (Caculate[0] (5, 6));
It is obvious that the length of the code has increased and new data members have been added, in the code that is not so strict in efficiency requirements, sacrificing the length of the code in exchange for a better structure, I think it's a good deal. But list
dictionary<string, func<int, int , int>> cacular = new dictionary<string, func<int, int, Int>> (); cacular.add ("Add", add); cacular.add ("Sub", sub); cacular.add ("Mul", mul); cacular.add ("div", div); sayresult (cacular["Add"] (5, 6);
code again longer, dictionary does not provide addrange this method. If you need to expand more operations, you can dictionary The extension method addrange demo class dictionary It's too weak in this. First in the code of the amount of concessions, also can not provide intelligent prompt support, still need relevant document support, in order to Ming Xiao "key" meaning. It's time to upgrade it into a class.
public static class caculate { public static func<int, int, int> add = delegate (int x, int y) { return x + y; }; public static Func<int, int, Int> sub = delegate (int x, int y) { return x - y; }; public static Func<int, int, int> mul = delegate (int x, int y) { return x * y; }; public static func<int, int, int> div = delegate (Int x, int y) { return x / y; };
Here I put the op 4 action put to caculate sayresult
public static class caculate<t> { public static func<t, t, t> add = delegate (t x, t y) { return x + y; }; public static func<t, t, t > sub = delegate (t x, t y) { return x - y; }; public static Func<T, T, T> mul = delegate (t x, t y) { return x * y; }; public static Func<T, T, T> div = delegate (T x, t y) { return x / y; };
It's good to write like this, but the generics can't be used for operators. To get a similar implementation through Baidu.
public static class cacular<t> where t : struct { public static func<t, t, t > add = delegate (t x, t y) { t result = default (T); type type = x.gettype () ; string s = type. ToString (); switch (type. ToString ()) { default: break; case "System.Int32": result= (T) ( Object) ((Int32) (object) x + (Int32) (object) y); break; case "System.Single": result = (T) (object) ((single) (object) x + (object) y); break; case "System.Double": result = (T) ( Object) ((double) (object) x + (double) (object) y); break; } return result; }; public static func<t, t, t> sub = delegate (T x , t y) { t result = default (T); type type = x.gettype (); switch (type. ToString ()) { default: break; case "System.Int32": result = (T) (object) ((Int32) (object) x - (Int32) (object) y); break; case " System.Single ": result = (T) (object) ((single) (object) x - (object) y); &nbSp; break ; case " System.Double ": result = (T) (object) ((double) (object) x - (double) (object) y); break; } return result; }; public static Func<T, T, T> mul = delegate (t x, t y) { &nbsP; t result = default (T); type type = x.gettype (); switch (type. ToString ()) { default: break; case "System.Int32": result = (T) (object) ((Int32) (object) x * (Int32) (object) y); break; case "System.Single": result = (T) (object) ((single) ( Object) x * (single) (object) y); break; case "System.Double": result = (T) ( Object) ((double) (object) x * (double) (object) y); break; } return result; &Nbsp; }; public static func<t, t, t> div = delegate (t x, t y) { T Result = default (T); type type = x.gettype (); switch (type. ToString ()) { default: break; case "System.Int32": result = (T) (object) ((Int32) (object) x / (Int32) (object) y); break; case "System.Single": result = (T) (object) ((single) (object) x / (object) y); break; case " System.Double ": result = (T) (object) ((double) (object) x / (double) (object) y); break; } return result; }; }
As he said, this is really a stupid method, if there is a suitable way, want to contact me. This kind of problem is more old, extensibility, maintenance is very bad.
Now found that the establishment of a good class is still quite difficult, not only need to fully consider the current needs to implement the function, but also to consider the future may need to expand the function, on the compatibility, the next expansion, good structure, streamlined code.
There is a demo that wants to go and what programmers should think about when they write code.