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

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.

主站蜘蛛池模板: 绥阳县| 平塘县| 广水市| 灵台县| 昭平县| 海丰县| 张掖市| 屏东市| 卢湾区| 包头市| 花莲县| 舞阳县| 扶沟县| 博客| 盈江县| 炉霍县| 盐边县| 龙海市| 东光县| 堆龙德庆县| 洪泽县| 莫力| 永胜县| 友谊县| 拉萨市| 时尚| 兴安县| 前郭尔| 泉州市| 阆中市| 军事| 青阳县| 渭源县| 滦南县| 嘉荫县| 石棉县| 夏邑县| 阳江市| 隆昌县| 阳朔县| 格尔木市|