hi guys i was interested to know how list collection work and i tried to create my own collection like list please guide me if this could the way that list collection would have been created .its obvious that original list class must have huge coding.
using System;
namespace ConsoleApplication12
{
class customer
{
public string name;
public int custID;
}
class MyList
{
public T[] MyListElement = new T[3];
private int index=0;
public void Add(T cust)
{
if (index == MyListElement.Length)
{
Array.Resize(ref MyListElement,MyListElement.Length+3);
MyListElement[index] = cust;
index ++;
return;
}
MyListElement[index] = cust;
index ++;
}
}
class Program
{
static void Main(string[] args)
{
customer c = new customer() {name="prashant",custID=2 };
customer c1 = new customer() { name = "amit", custID = 3 };
customer c2 = new customer() { name = "ajit", custID = 4 };
customer c3 = new customer() { name = "pritesh", custID = 5 };
customer c4 = new customer() { name = "jo", custID = 33 };
customer c5 = new customer() { name = "ko", custID = 43 };
customer c6 = new customer() { name = "do ", custID = 53 };
customer c7 = new customer() { name = "fo", custID = 53 };
MyList customers = new MyList();
customers.Add(c);
customers.Add(c1);
customers.Add(c2);
customers.Add(c3);
customers.Add(c4);
customers.Add(c5);
customers.Add(c6);
customers.Add(c7);
Console.WriteLine(customers.MyListElement.Length);
Console.ReadLine();
}
}
}
VulpesPosted Aug 3, 2014, 10:06 AM
Prashant JadhavPosted Aug 3, 2014, 8:45 AM
i expected those functionality provide by list collection my main concern is how list work internally
Dipankar BiswasPosted Aug 3, 2014, 7:30 AM
using System.Collections.Generic;
class Program
{
static void Main()
{
List
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
}
}
2.(Loops)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
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 < list.Count; i++) // Loop through List with for
{
Console.WriteLine(list[i]);
}
}
}
Output
(Repeated twice)
2
3
7
3)Count, Clear :
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List
list.Add(true);
list.Add(false);
list.Add(true);
Console.WriteLine(list.Count); // 3
list.Clear();
Console.WriteLine(list.Count); // 0
}
}
Output
3
0
4.(Copy array)
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
Console.WriteLine(list.Count); // 3 elements in List
}
}
Output
(Indicates number of elements.)
3
5.(Find)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// New list for example
List
// See if List contains 3
foreach (int number in primes)
{
if (number == 3) // Will match once
{
Console.WriteLine("Contains 3");
}
}
}
}
Output
Contains 3
6.(The biggest advantage of Join here is that no trailing comma is present on the resulting string)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// List of cities we need to join
List
cities.Add("New York");
cities.Add("Mumbai");
cities.Add("Berlin");
cities.Add("Istanbul");
// Join strings into one CSV line
string line = string.Join(",", cities.ToArray());
Console.WriteLine(line);
}
}
Output
New York,Mumbai,Berlin,Istanbul
7.(Keys in Dictionary)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Populate example Dictionary
var dict = new Dictionary
dict.Add(3, true);
dict.Add(5, false);
// Get a List of all the Keys
List
foreach (int key in keys)
{
Console.WriteLine(key);
}
}
}
Output
3, 5
8.(Insert)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List
dogs.Add("spaniel"); // Contains: spaniel
dogs.Add("beagle"); // Contains: spaniel, beagle
dogs.Insert(1, "dalmatian"); // Contains: spaniel, dalmatian, beagle
foreach (string dog in dogs) // Display for verification
{
Console.WriteLine(dog);
}
}
}
Output
spaniel
dalmatian
beagle
9.(GetRange)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List
{
"nile",
"amazon", // River 2
"yangtze", // River 3
"mississippi",
"yellow"
});
// Get rivers 2 through 3
List
foreach (string river in range)
{
Console.WriteLine(river);
}
}
}
Output
amazon
yangtze