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

Download Report in Excel in JSP

Report in Excel Format in JSP FLIGHT CODE FROM STA ETA Notes T3 4264 ISLE OF MAN 11:40 11:42 LANDED AT 11:43 BA 4081 PARIS-CDG 11:45 11:57 LANDED AT 11:58 BE 843 BELFAST CITY 11:45 11:40 LANDED AT 11:41 Call the above jsp by using any of the click functionality from another .jsp

Basic Java Interview Questions

1. Why java does not support multiple inheritance ? Answer :- To say why java doesn't support inheritance directly like c++ should be the diamond problem faced by c++,(Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden this method differently, then via which class does it inherit: B, or C?) 2. JVM crash is an Error or Exception.? Answer:- JVM crash is an Error as a normal developer as we cannot handle it, but its exception for JVM developer as they can fix it. 3. Print “Hello World!” Without Main Method in Java? Answer:- public class Testing {    static    {        System.out.println("Hello World");    }    public static void main(String[] args)    {    } } ...

Collections in Java

Collections in Java Introduction to Collections Framework The Collections Framework provides a well defined set of interfaces and classes for storing and manipulating the groups of data into a single unit. Introduction to Collections Framework Collections Framework: The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating the groups of data into a single unit. The collections framework is a unified architecture which is used to represent and manipulate collections. The framework allows the collections to get manipulated independently, additionally it reduces the programming efforts and increases performance. It includes implementation of interfaces and algorithms. Basically it is a unified architecture that consists the following collections: Interfaces: These are the abstract data types that represent collections. With the help of interfaces we manipulate collections independently. A hierarchy is generally formed ...