Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Beginning C# 7 Hands-On ??? Advanced Language Features

You're reading from   Beginning C# 7 Hands-On ??? Advanced Language Features Learn the advanced-level features of C# 7 using Visual Studio 2017

Arrow left icon
Product type Paperback
Published in Oct 2017
Publisher Packt
ISBN-13 9781788294263
Length 310 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Tom Owsiak Tom Owsiak
Author Profile Icon Tom Owsiak
Tom Owsiak
Arrow right icon
View More author details
Toc

Table of Contents (30) Chapters Close

Preface 1. Creating a Simple Generics Class 2. Creating a Generic Method FREE CHAPTER 3. Implementing a Generic Interface to Accomplish Sorting 4. Making Delegates More Flexible with Generics 5. Creating and Using Generic Dictionaries 6. Connection Between Delegates and Lambda Expressions 7. Expression-Bodied Lambdas and Expression-Bodied Members 8. Anonymous Methods and Objects That Run Their Own Delegates 9. C# with LINQ and Built-In Types 10. C# with LINQ and Custom Data Types 11. Using Query Syntax to Construct Queries 12. Queries That Perform Aggregation Functions 13. Using LINQ to Summarize Tuples 14. Summarizing Results with Grouping 15. Joining Datasets with Inner Joins 16. Downloading, Installing, and Running SQL Server 2017 17. Writing Code to Manually Connect to a Table and Retrieve Records 18. Inserting Records into Tables Using Stored Procedures 19. Using the Nullable Feature to Make Apps More Stable 20. Connecting a Chart Control to SQL Server 21. Using LINQ to Operate on Tables from SQL Server 22. Creating a Page That Saves Text to Disk 23. Creating a Page That Uses the File Upload Control 24. Serializing and Deserializing Objects 25. Having a Little Fun with Images with Pixel Manipulations 26. Saving an Image to SQL Server 27. Creating and Using an XML File 28. Creating XML Files with C# 29. Querying XML Documents with LINQ

Creating a generics class

Bring up a project, and go to Solution Explorer; right-click, select Add, and click on Class. Name the class GenericsClass; a simple generics class. Then, click on OK. When the Visual Studio message comes up, click on Yes.

For our purposes, you don't need any of the using System lines at the top, nor any of the comments underneath, so delete them. Your initial screen should look like Figure 1.1.1:

Figure 1.1.1: The initial GenericsClass.cs screen

Working with different data types

Now, let's put a <T> symbol after where it says public class GenericsClass, as follows:

public class GenericsClass<T>

This means that this single class can work equally well with several different data types. Next, enter the following beneath the open curly brace under the preceding line:

private T[] vals;

Enter the following comment directly above this line:

//generic array instance variable

In other words, this will operate equally well on doubles, decimals, integers, and so on.

Making parameters that are generic

Now, in the following line, enter the following:

public GenericsClass(T[] input)

As you can see, you can also make parameters that are generic like this one. This is a parameter, input is the name of it, and the type is T. So, it's a generic array.

Next, enter the following between a set of curly braces beneath the preceding line:

vals = input;

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime