- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 158字
- 2021-07-02 15:29:17
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.
推薦閱讀
- 深入理解Android(卷I)
- LabVIEW Graphical Programming Cookbook
- 小創(chuàng)客玩轉(zhuǎn)圖形化編程
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- Python機(jī)器學(xué)習(xí)經(jīng)典實(shí)例
- QGIS By Example
- 小學(xué)生C++創(chuàng)意編程(視頻教學(xué)版)
- Linux C編程:一站式學(xué)習(xí)
- The Professional ScrumMaster’s Handbook
- 智能搜索和推薦系統(tǒng):原理、算法與應(yīng)用
- Vue.js 2 Web Development Projects
- Programming with CodeIgniterMVC
- MINECRAFT編程:使用Python語言玩轉(zhuǎn)我的世界
- 零基礎(chǔ)學(xué)HTML+CSS第2版
- NGUI for Unity