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

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.

主站蜘蛛池模板: 平舆县| 繁昌县| 华亭县| 云和县| 百色市| 泉州市| 娱乐| 韶关市| 南郑县| 蚌埠市| 洞口县| 三原县| 东乌珠穆沁旗| 清水县| 洮南市| 霍山县| 舞阳县| 万宁市| 岑巩县| 乐清市| 定远县| 武安市| 卓资县| 兴隆县| 九龙城区| 贵德县| 拉萨市| 涟源市| 信丰县| 手游| 德江县| 安丘市| 台安县| 沅江市| 吐鲁番市| 南充市| 昔阳县| 济宁市| 甘泉县| 会同县| 马鞍山市|