Part1 -> Section1 -> Basic -> 012. Collection

     ตัวแปรปกติที่เคยได้กล่าวมาก่อนหน้านี้จะสามารถเก็บข้อมูลได้เพียงค่าเดียวต่อหนึ่งตัวแปรเท่านั้น หากเราต้องการเก็บค่ามากกว่าหนึ่งค่าต่อหนึ่งตัวแปร เราจะต้องใช้ Collection ในการเก็บข้อมูล ซึ่ง Collection นั้นก็มีอยู่หลากหลายชนิด แต่ที่นี้จะกล่าวเพียงแค่สองชนิดที่ใช้บ่อย คือ Arrays และ Lists

1. Arrays การประกาศนั้นจะเหมือนกับตัวแปรปกติ ต่างนิดเดียวที่มี brackets [] อยู่ที่หลังชนิดข้อมูล โปรดดูตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare arrays example 1
            string[] names1 = new string[3];
            names1[0] = "Somchai";
            names1[1] = "Somsri";
            names1[2] = "SomSak";

            Console.WriteLine(names1[0]);

            // Declare arrays example 2
            string[] names2 = new string[3] { "Somchai", "Somsri", "SomSak" };
            Console.WriteLine(names2[1]);

            // Declare arrays example 3
            string[] names3 = { "Somchai", "Somsri", "SomSak" };
            Console.WriteLine(names3[2]);

            Console.ReadLine();
        }
    }
}

ได้ผลลัพธ์ดังรูป



การประกาศตัวแปรทำได้หลายแบบ การเลือกใช้งานนั้นก็ต้องเลือกตามความเหมาะสม การเรียกใช้นั้นสามารถเข้าถึงค่าที่เก็บผ่าน Index ซึ่งจะเริ่มต้นจาก 0 เสมอ ( variableName[Index] ) ซึ่งปกติการใช้งาน Collection จะใช้กับ loop ตัวอย่างการใช้งานด้านล่าง

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 5, 1, 7, 4, 9, 2 };
            Array.Sort(numbers);
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.WriteLine(numbers[i]);
            }

            Console.ReadLine();
        }
    }
}

ได้ผลลัพธ์ดังรูป

โปรแกรมนี้ทำงาน เรียงต้วเลข (Array.sort) และปรินท์ออกออกมาโดยใช้ loop

2. Lists การทำงานจะเหมือนกันกับ Arrays แต่รูปแบบการจะแตกต่างกันพอสมควร ดังตัวอย่างด้านล่าง

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //// Declare lists example 1
            //List exam1 = new List();
            //exam1.Add(1);
            //exam1.Add(7);
            //exam1.Add(3);

            // Declare lists example 2
            List numbers = new List() { 5, 1, 7, 4, 9, 2 };

            // Sort lists
            numbers.Sort();

            // Loop through lists
            foreach (var item in numbers)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}

ได้ผลลัพธ์ดังรูป

การประกาศเพื่อใช้งาน lists มีอยู่สองแบบ การใช้งานก็เหมือนเดิมคือเลือกตามความเหมาะสม จากนั้นก็เรียงข้อมูลใน lists แล้วจึง loop เพื่อปรินท์ข้อมูลออกมา ซึ่งใช้งานกับ foreach จะง่ายมากๆ

เราได้เรียนรู้จนจบพาร์ทแรกกันแล้นนะ C# Basic

สอบถามหรือติดตามได้ที่ https://www.facebook.com/learnaspnetmvcjquery

ความคิดเห็น

แสดงความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Part2 -> Section1 The Basic -> 003. MVC Folder Structure

Part2 -> Section1 The Basic -> 001. Introduction

Part2 -> Section1 The Basic -> 005. Controller