- 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.
推薦閱讀
- SQL Server 從入門到項目實踐(超值版)
- Java 9 Programming Blueprints
- 云原生Spring實戰
- 信息安全技術
- 網店設計看這本就夠了
- Expert Data Visualization
- Haxe Game Development Essentials
- 學習OpenCV 4:基于Python的算法實戰
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Natural Language Processing with Java and LingPipe Cookbook
- 現代C:概念剖析和編程實踐
- JQuery風暴:完美用戶體驗
- Game Development Patterns and Best Practices
- Alfresco for Administrators
- iOS程序員面試筆試真題與解析