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

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.
主站蜘蛛池模板: 祁东县| 祁阳县| 巴南区| 和林格尔县| 城口县| 岗巴县| 富平县| 会东县| 汨罗市| 牡丹江市| 诸城市| 宿松县| 淮南市| 凯里市| 信宜市| 岳池县| 枣阳市| 土默特左旗| 丘北县| 禹城市| 鄂尔多斯市| 临潭县| 邹平县| 镇雄县| 天台县| 宜宾市| 浦东新区| 徐闻县| 和硕县| 乐清市| 庐江县| 京山县| 拉萨市| 霍州市| 普宁市| 旬阳县| 疏附县| 库车县| 梅河口市| 庆云县| 宁海县|