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

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.

主站蜘蛛池模板: 叶城县| 镇远县| 专栏| 九台市| 南丹县| 南汇区| 隆德县| 耿马| 肥城市| 昆明市| 宁阳县| 广安市| 安国市| 西乌珠穆沁旗| 左云县| 建宁县| 许昌县| 丹棱县| 宁强县| 略阳县| 青铜峡市| 西贡区| 高唐县| 清流县| 宁武县| 舟曲县| 东兰县| 平湖市| 安新县| 湟源县| 惠东县| 德惠市| 合川市| 南京市| 金阳县| 广饶县| 敦煌市| 兰州市| 平舆县| 台湾省| 黑龙江省|