官术网_书友最值得收藏!

Modifying the program for different data types

The nice thing now is this: imagine you want to redo this; you can just type int as an example and change the data type to an integer or decimal type and the methods. The code that we wrote in this chapter operates equally well on these things:

int x = 25, y = 34;
sampLabel.Text = $"x={x}, y={y}";
GenMethods.Swap<int> (ref x, ref y);
sampLabel.Text += $"<br>x={x}, y={y}";
sampLabel.Text += GenMethods.Compare<int>(x, y);

The only thing is, of course, if you right-click on int and select Go To Definition in the drop-down menu (F12), you'll see that it says public struct Int32 and it implements IComparable:

Figure 2.2.6: Definition for public struct Int32

This will work due to the fact that our function has a constraint where it says where T should be comparable, as shown here:

public static string Compare<T>(T x, T y) where T : IComparable

These are the basics.

主站蜘蛛池模板: 仪陇县| 新巴尔虎左旗| 洪雅县| 上杭县| 青河县| 明溪县| 三河市| 逊克县| 辉县市| 陇川县| 峨山| 太谷县| 上高县| 东阿县| 红河县| 和硕县| 航空| 姚安县| 图片| 嘉峪关市| 库尔勒市| 沙雅县| 东源县| 阿拉善盟| 全椒县| 垣曲县| 会理县| 宜春市| 保定市| 全州县| 洪洞县| 祁东县| 成安县| 成安县| 冷水江市| 辽阳市| 习水县| 常熟市| 荆门市| 陵川县| 天等县|