c sharp

C# List Contains

C# list contains means we check whether the list contains the number or element which we want to check. It will check the specified element, string, or number in the list. If the list contains that number or element, it will return that number or element. It will also return true if the list has the stated element, and it will return false if the list does not hold the stated element. In this tutorial, we will discover the concept of a “list contain” in C# programming. We have different examples in this guide in which we will check the specified elements in the list.

Example # 1

We are going to perform the given examples in Ubuntu 20.04. In Ubuntu 20.04, first, we open the text editor and type the code which is given in the image below. After this, we save this with the file name of our choice and the extension “.cs”.

In this program, we use the “using” keyword, which will create an alias for a namespace. The “using System” is here to give us useful classes and functions. It means that we are using system libraries in our code. Then, we have “System.Collections.Generic” for using interfaces and classes. We use this namespace here for accessing the list. It will provide better performance. The “System.Linq” is used for retrieving the data.

We must have the “main” function in our C# program. It is the entry point of our C# program. Our code starts execution from here. Here “var” is a data type named “list”. The “new” is a keyword for creating a new list here. It will generate a new list of strings. Now we will add different string variables to our list with the help of the “Add” method. The “Add” is the method in C# for adding diverse elements to the list. The “list.Add” will add the given strings to our list. The “list.Add “(“Computer”)” will add the “Computer” to our list. Same as the “list.Add(“Laptop”)” and “list.Add(“printer”)” will add “Laptop” and “Printer” to our list, which we have created in this program.

After adding all these elements to our list, we will check whether our list contains a laptop or not. So, we use the “if” statement, which will check this. Inside this “if” statement, we use the “Contain” method, which returns a value that indicates whether the list contains “laptop” or not. Then, we have “Console.WriteLine” for printing. If the “if” statement is true, then it will print the line of code which is written below this “if” statement. Then we close all the brackets and save this code.

We perform this example in Ubuntu 20.04, so for output, we open the Ubuntu terminal and write the given commands on this terminal. The first command is the “mcs” command to compile our C# code. When using this “mcs” command, we must use the “.cs” file extension with the filename. If the code is error-free, then it will create an executable file for us with the same name. After this, we will execute this code with the help of the “mono” command. When using this “mono” command, we must use the file extension of “.exe” with the file name and hit enter. It will give the output of our code.

In this output, you can understand that it first compiles our code. As our code is error-free, then it executes our code and gives the output. It checks whether the list contains the specified element or not. Our list contains “laptop”, so it prints the line on the terminal screen.

Example # 2

Now, we will explore another example for you so that you can easily understand this concept with the help of different examples. In this instance, we use the list of integers. Look at the second example, which is given below in the image.

In this first line, we have the “using System” library for accessing classes and functions. Then we have “System.Collections.Generic” which allows us to create strongly typed collections. We created a class named “Csharp” and invoked a “main” function. The “string [] args” is a variable. Inside the “main” function, we create a list of integers. The “new” keyword creates a new list, and “int” is used for integers. We must have the integers values in our list. So, for adding integer numbers to our list, we will utilize the “Add” function. The “numbers.Add(33)” is used for adding “33” to our list. Here “numbers” represent the name of the list which we have created above, and “Add” will add “33” to the list. By using “numbers.Add(56)” and “numbers.Add(84)”, we will add “56” and “84” respectively.

After that, we have “bool” which is the Boolean data type, and it returns the “true” and “false” results of the given statement. We declare “num” with this “bool” data type and assign a “Contain” method to this “bool num”. The “numbers.Contains(56)” checks “56” in the list. Here “numbers” represent the list in which we check the given number. If the list contains “56” then this “bool num” will return “true”; otherwise, it will return “false”. We will also check “30” in this list by using the same method and will return “true” or “false”. We use the “Console.WriteLine” statement to print the result.

Here, in this output, it returns “True” when it checks “56” in the list because our list contains “56”. And it returns “false” in the case of “30”, as our list does not contain “30”.

Example # 3

Here, we have one more example of the C# “list contain”. In this code, first, we print all the list elements and then check the specified number in the list.

We have “using System” and “using System.Collections.Generic” which we discussed in detail in previous examples. Then we declare a public class called “Program” and invoke a “main” function inside this. Then we create a list of integers with the name “num” and add the numbers by using the “Add” method. First, we have to print all the numbers which we add to our list. This “Console.WriteLine” will print the line on the screen, then we use the “foreach” loop. Inside this “foreach” loop, we are going to initialize an integer “int” with the name “res” which reads the numbers in the list named “num” and stores each number in “res”.

After this, we will print all these numbers on the terminal by using “Console.WriteLine(res)”. Then, we are going to check “400” and “202” in the list and print the result on the screen using “Console.WriteLine”.

The output of this code is like that in the above image. First, it prints all the numbers on the list and then checks both numbers and return the “true” and “false” result.

Conclusion

This guide explains how to use the “list Contain” method in C# programming. We discussed the codes in detail. We explain every line of the code so that you can simply comprehend how it works in C# programming. We demonstrate three different examples for your better understanding of this concept. After reading this guide and performing these examples by yourself, you will be able to use this “list Contain” method in your complex codes of C# programming as well.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.