Skip to main content

C# List Examples


C# List Examples

1. Adding values
~~~ Program that adds elements to List (C#) ~~~


using System.Collections.Generic;

class Program
{
      static void Main()
      {
            List list = new List();
            list.Add(2);
            list.Add(3);
            list.Add(5);
            list.Add(7);
      }
}

***********************************************************************************

2. Adding objects.
Using Add method
~~~ Program that uses Add method (C#) ~~~


using System.Collections.Generic;

class Program
{
      static void Main()
      {
            // Add first four numbers to the List.
            List primes = new List();
            primes.Add(2);
            primes.Add(3);
            primes.Add(5);
            primes.Add(7);
      }
}

***********************************************************************************

3. Adding objects
-- Program that adds objects to List (C#) ---


using System.Collections.Generic;

class Program
{
      static void Main()
      {
            // Add three objects to a List.
            List list = new List();
            list.Add(new Test(1, 2));
            list.Add(new Test(3, 4));
            list.Add(new Test(5, 6));
      }
     
      class Test
      {
            int _a;
            int _b;
            public Test(int a, int b)
            {
                  _a = a;
                  _b = b;
            }
      };
}

***********************************************************************************

4. Loops
~~~ Program that loops through List (C#) ~~~


using System;
using System.Collections.Generic;

class Program
{
      static void Main()
      {
            List list = new List();
            list.Add(2);
            list.Add(3);
            list.Add(7);
     
            foreach (int prime in list) // Loop through List with foreach
            {
                  Console.WriteLine(prime);
            }
     
            for (int i = 0; i < null ="="=""> list = new List();
            list.Add(true);
            list.Add(false);
            list.Add(true);
            Console.WriteLine(list.Count); // 3
           
            list.Clear();
            Console.WriteLine(list.Count); // 0
      }
}

===Output of the program ===
3
0

***********************************************************************************

 5. Copying array to List
--- Program that copies array to List (C#) ---


using System;
using System.Collections.Generic;

class Program
{
      static void Main()
      {
            int[] arr = new int[3]; // New array with 3 elements
            arr[0] = 2;
            arr[1] = 3;
            arr[2] = 5;
            List list = new List(arr); // Copy to List
            Console.WriteLine(list.Count); // 3 elements in List
      }
}


=== Output of the program ===
(Indicates number of elements.)

3

***********************************************************************************
6. C# List Find Method
--- Program that uses Find method on List [C#] ---

using System;
using System.Collections.Generic;

class Program
{
      static void Main()
      {
            List list = new List(new int[] { 19, 23, 29 });
           
            // Finds first element greater than 20
            int result = list.Find(item => item > 20);
           
            Console.WriteLine(result);
      }
}

=== Output of the program ===
23


Comments

Popular posts from this blog

Executing executable Jar file from Java Program

Sample code to execute jar file from Java Program. Pre-requisite 1. Need to have executable jar file. 2. Copy the code in your workspace and Run the code. package com.test.reusable.enterprise.service; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.lang.exception.ExceptionUtils; public class ExecuteJarFile { public static void main(String[] args) { try { //Method1 to Execute Jar file From Java Program ProcessBuilder pb = new ProcessBuilder("java", "-jar", "HelloWorld.jar"); // command to execute jar file with fileName.jar pb.redirectErrorStream(true); pb.directory(new File("C:/www/")); // Directory path where your jar file is placed. Process p = pb.start(); ...

Uploading data on Remote Server in Java

This blog demonstrate how to upload data from file to remote server/database. Follow the steps as shown. 1. Create new Dynamic Web Project. 2. Now create all the packages as show in the image below. 3. Create controller with name ImportContact.java 4. Create pojo with name Contact.java 5. Create database layer code with name ImportContactBroker.java 6. Create connectivity with name DBConnection.java 7. Create db connectivity setting file as connectionSetting.java 8. Create jsp page with importcontact.jsp 9. Create supporting files like Constant.java and CsvReader.java 10. You can download the war file and import in your workspace. 11. MultipartRequest in Servlet and inside jsp helps to upload file on the remote server. 12. Download sample csv file from here . 13. Create Table with name Contact in Database as given in configuration file. 14.Just compile and run the code. ImportContact.java package com.uploadfile.controller; import java.io.BufferedReader; impo...

Creating Pdf in multiple Languages in Java

Creating Pdf in multiple Languages in Java This code demonstrate how to create pdf in multiple languages in java by using ITEXT pdf creator. To use this sample code download itext.jar To display different locale on pdf you need to download font known as Arialuni.ttf and added to the project Most important thing is the encoding part set encoding to "Identity-H" (String encoding = "Identity-H";) as well as create font for the same to take effect in pdf create font (Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);) You can download the source code from here import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf....