- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 238字
- 2021-07-02 15:29:15
Changing the data types in our generics class
Now, to make the code efficiency more obvious, take both of the preceding lines, copy them (Ctrl + C) and paste them (Ctrl + V) beneath these and just change it to double, as follows:
GenericsClass<double> dubs = new GenericsClass<double>(new double[] {1, 2, 3, 4, 5});
sampLabel.Text = ints.DisplayValues();
We'll call this one dubs and change the name here to double: it's the same code, the same class, and the same generic class that you can operate on the doubles. Again, to emphasize this one more time, and to see that flexibility and code reuse is really the purpose here; that is, the ability to reuse code, we'll now take both of these new lines, copy and paste them below once more, and just change double to decimal, as follows:
GenericsClass<decimal> decs = new GenericsClass<decimal>(new decimal[] { 1, 2, 3, 4, 5 });
sampLabel.Text = ints.DisplayValues();
Let's call this one decs. Now, of course, if you want to make things a little more interesting, you can throw in some decimals:
GenericsClass<double> dubs = new GenericsClass<double>(new double[] { 1.0, -2.3, 3, 4, 5 });
sampLabel.Text = ints.DisplayValues();
GenericsClass<decimal> decs = new GenericsClass<decimal>(new decimal[] { 1, 2.0M, 3, 4, 5.79M });
sampLabel.Text = ints.DisplayValues();
With decimals, just make sure that you put the M suffix in there, because you need the M suffix at the end to indicate that it's a decimal.
推薦閱讀
- OpenStack Cloud Computing Cookbook(Third Edition)
- Mobile Application Development:JavaScript Frameworks
- Spring Boot開發(fā)與測(cè)試實(shí)戰(zhàn)
- Mastering Adobe Captivate 2017(Fourth Edition)
- Java高手真經(jīng)(高級(jí)編程卷):Java Web高級(jí)開發(fā)技術(shù)
- Python自動(dòng)化運(yùn)維快速入門
- Learning Data Mining with Python
- Learning AndEngine
- C++程序設(shè)計(jì)基礎(chǔ)教程
- Linux:Embedded Development
- Working with Odoo
- 0 bug:C/C++商用工程之道
- ArcGIS for Desktop Cookbook
- 小型編譯器設(shè)計(jì)實(shí)踐
- Image Processing with ImageJ