Collection C#
Example C# 2018
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
methodDisplay();
}
static string[] methodString()
{
string[] array = new string[3];
array[0] = "Thank";
array[1] = "To";
array[2] = "Array String[]";
return array;
}
static ArrayList methodArrayList()
{
ArrayList arrayList = new ArrayList();
arrayList.Add("ArrayList1");
arrayList.Add("ArrayList2");
arrayList.Add("ArrayList3");
arrayList.Add("ArrayList4");
return arrayList;
}
static List<String> methodList()
{
List<String> list = new List<String>();
list.Add("List1");
list.Add("List2");
list.Add("List3");
list.Add("List4");
return list;
}
static Hashtable methodHashtable()
{
Hashtable hashtable = new Hashtable();
hashtable.Add("key1", "hash1");
hashtable.Add("key2", "hash2");
hashtable.Add("key3", "hash3");
//string str = hashtable["key5"].ToString();
return hashtable;
}
static Dictionary<string, string> methodDictionary()
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("key1", "key11");
dictionary.Add("key2", "key12");
dictionary.Add("key3", "key13");
//string str = dictionary["key5"].ToString();
return dictionary;
}
static Stack<string> methodStack()
{
// Stack LIFO
Stack<string> stack = new Stack<string>();
stack.Push("stack1");
stack.Push("stack2");
stack.Push("stack3");
//string str = stack.Pop().ToString()
return stack;
}
static Queue<string> methodQueue()
{
// Queue FIFO
Queue<string> queue = new Queue<string>();
queue.Enqueue("queue1");
queue.Enqueue("queue2");
queue.Enqueue("queue3");
//string str = queue.Dequeue().ToString();
return queue;
}
static void methodDisplay()
{
// Show Array
Console.WriteLine(string.Join("->", methodString()));
// Show ArrayList
Console.WriteLine(string.Join("->", methodArrayList().ToArray()));
// Show List
Console.WriteLine(string.Join("->", methodList()));
// Show Hashtable
Console.WriteLine(string.Join("->", methodHashtable()["key2"].ToString()));
// Show Dictionary
Console.WriteLine(string.Join("->", methodDictionary()));
// Show Stack
Console.WriteLine(string.Join("->", methodStack()));
// Show Queue
Console.WriteLine(string.Join("->", methodQueue()));
// Endding and Reading
Console.ReadLine();
}
}
}
0 nhận xét:
Post a Comment