- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 318字
- 2021-07-02 15:29:14
Displaying the values
Of course, you should be able to display these values. so, enter the following line beneath the closed curly brace under the vals = input; line:
public string DisplayValues()
To display these values, you'll enter the following between a set of curly braces beneath the preceding line.
First, put in a string, as follows:
string str = null;
Next, declare the string and initialize the value to null.
Then, enter the following directly below this line:
foreach ( T t in vals)
As you can see, the foreach loop here is going to operate. The T object will be a different data type, depending on how we choose to make the object. The t variable, of course, is each specific value inside the vals array.
Next, you will enter the following between a set of curly braces beneath the preceding line:
str += $"<br>Value={t}";
Remember, we use the += operator to accumulate and <br> to push down to the next line. To get the value, of course, we will put in the t variable.
At the end, you want to return this, so you will type the following beneath the closed curly brace under the preceding line:
return str;
That's it. The final version of the GenericsClass.cs file for this chapter, including comments, is shown in the following code block:
//<T> means this class can operate on many different data types
public class GenericsClass<T>
{
//generic array instance variable
private T[] vals;//array of T inputs
public GenericsClass(T[] input)
{
//set value of instance variable
vals = input;
}
public string DisplayValues()
{
string str = null;//create string to build up display
foreach(T t in vals)
{
//actually accumulate stuff to be displayed
str += $"<br>Value={t}";
}
//return string of outputs to calling code
return str;
}
}
Notice that we have a single block of code; this will now operate on integers, doubles, and so on.
- Modular Programming with Python
- LabVIEW2018中文版 虛擬儀器程序設計自學手冊
- Mastering Objectoriented Python
- 跟“龍哥”學C語言編程
- 用Flutter極速構(gòu)建原生應用
- Unity Shader入門精要
- SSM輕量級框架應用實戰(zhàn)
- Python:Master the Art of Design Patterns
- Building RESTful Python Web Services
- 大學計算機基礎實驗指導
- Natural Language Processing with Java and LingPipe Cookbook
- 軟件測試教程
- R語言數(shù)據(jù)可視化:科技圖表繪制
- 區(qū)塊鏈國產(chǎn)化實踐指南:基于Fabric 2.0
- SQL Server 入門很輕松(微課超值版)