Saturday, October 25, 2014

ภาษา ซีชาร์ป เบื้องต้น บทเรียนที่ ๑




มาร่วมด้วยช่วยกัน เรียน โคด ภาษา ซีชาร์บกันครับ ทุก คน สามารถ เรียนได้ ช่วยกัน ถามปัญหาและตอบกันครับ มีวิชาติดตัวไว้ไม่เสียหายครับ

บทเรียนแรก คอนโซล แอปพลิเคชั่น


// เวลาเราสร้าง C# console  แอฟลิเคชั่น จะมีมาดยอัตโนมัติ เปรียบ เสมือนห้องสมุด ที่ จำเป็นในการออกคำสั่ง ของซีชาร์ป
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// ชื่อโปรแกรม
namespace Lesson1
{


 
    class Exampleone
    {
       // ฟังชั่นหลัก
        static void Main(string[] args)
        {
    // var คือตัวกำหนด ค่าของ ตัวแปรว่า ต้องเป็นอะไร โดยที่เราไม่ต้องเจาะจง โปรแกรมจะทราบเองว่า จะตัองเป็นอะไร            // ในตัวอย่างเป็น string หรือ ประโยค
            var message = "hello world!";
           
            //integer variables
            // จำนวนเต็ม
            int x = 5;
            int y = 2;
         
            //output message variable which is string
           // แสดงผลทางหน้าจอ Ctrl+F5 เพื่อที่จะไม่ต้อง ดีบัค โปรแกรม
            Console.WriteLine(message);
         
           //this is an example of place holder format
         //การ ให้รายละเอียดแบบ จองที่ 0 หมายถึง ค่าของ x 1 คือ ค่า ของ y  
          //และ 2คือ ค่าของผลลัพในการ บวกลบคูณหารหรือโมดลูล(%) กัน 

            Console.WriteLine("the result of {0} + {1} = {2}", x, y, x + y);
            Console.WriteLine("the result of {0} - {1} = {2}", x, y, x - y);
            Console.WriteLine("the result of {0} * {1} = {2}", x, y, x * y);
         
            //division how many times that you time the second value to get the first value
            //in this case 2*2 = 4 there fore at most 2 times to get nomore than 5 remainder of 1

           //สูตรของการหารจำนวนเต็ม คือ เราคำนวนว่า จะต้องคูณ ตังแปรหลังกี่ตัวถึงจะได้ ตัวแปรที่ หนึ่ง
           // ดังในตัวอย่าง  2*2 = 4 สองคูณ สอง เท่ากับสี่ เศษ หนึ่ง สองคูณกันสองครั้ง เศษ หนี่ง
          // คำตอบของหาร คือ 2 และคำตอบของ โมดูลคือ 1

            Console.WriteLine("the resule of {0} / {1} = {2}", x, y, x / y);
            //module result is the remainder
            Console.WriteLine("the resule of {0} % {1} = {2}", x, y, x % y);
        }

    }
}

ผลลัพ
hello world!
the result of 5 + 2 = 7
the result of 5 - 2 = 3
the result of 5 * 2 = 10
the resule of 5 / 2 = 2
the resule of 5 % 2 = 1
.