1 / 29

3. ชนิดข้อมูล และการดำเนินการกับข้อมูล

3. ชนิดข้อมูล และการดำเนินการกับข้อมูล. การดำเนินกันของข้อมูล จะต้องทราบชนิดข้อมูลนั้นก่อน. เป้าหมายการเรียนรู้. ชนิดข้อมูล (Data Type) และ Common Type System (CTS) การประกาศตัวแปร อโนนิมัสไทป์ และการแปลงชนิดข้อมูล ขอบเขตการใช้ตัวแปร ค่าคงที่ และค่าอ่านได้อย่างเดียว

Télécharger la présentation

3. ชนิดข้อมูล และการดำเนินการกับข้อมูล

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3. ชนิดข้อมูล และการดำเนินการกับข้อมูล การดำเนินกันของข้อมูล จะต้องทราบชนิดข้อมูลนั้นก่อน C# Programming with Visual C# 2010 Express

  2. เป้าหมายการเรียนรู้ • ชนิดข้อมูล (Data Type) และ Common Type System (CTS) • การประกาศตัวแปร อโนนิมัสไทป์ และการแปลงชนิดข้อมูล • ขอบเขตการใช้ตัวแปร • ค่าคงที่ และค่าอ่านได้อย่างเดียว • การดำเนินการของข้อมูลทางคณิตศาสตร์ • การดำเนินการของข้อมูลทางตรรกศาสตร์ • ลำดับการทำงานของตัวถูกดำเนินการ • การกำหนดชนิดข้อมูลใช้เอง C# Programming with Visual C# 2010 Express

  3. ชนิดข้อมูล ในการเขียนโปรแกรมต้องมีการดำเนินการกับข้อมูลในรูปแบบของภาษาซีชาร์ป นักเขียนโปรแกรมต้องรู้จักชนิดข้อมูลเพื่อดำเนินกับตัวแปร ชนิดข้อมูลอาจเรียกทับศัพท์ว่า ไทป์ (Type) ไทป์มีหลายลักษณะ คือ • Dynamic Type • Static • Type Safety • Strong Type C# Programming with Visual C# 2010 Express

  4. ชนิดข้อมูลใน C# C# Programming with Visual C# 2010 Express

  5. Common Type System (CTS) Type Reference Type Built-in Type Value Type User- defined Type C# Programming with Visual C# 2010 Express

  6. Int32:55 สัญลักษณะที่ใช้ Char: A “Hello” Value Type String: Reference Type Heap Int16:21 Stack การเก็บชนิดข้อมูลในหน่วยความจำ C# Programming with Visual C# 2010 Express

  7. ตาราง 3.2 คีย์เวิร์ด ที่เป็นชื่อพ้องกับชนิดข้อมูล C# Programming with Visual C# 2010 Express

  8. ตัวอย่าง การประกาศใช้ชนิดข้อมูลแบบต่างๆ • static void Main(string[] args) { • int _int = 0; • string _str = null; • bool _bool= true; • decimal _dec = 0.000001M; • double _dou = 0.00d; • char _chr = 'a'; • Console.WriteLine("_int = {0}", _int); • Console.WriteLine("_str = {0}", _str); • Console.WriteLine("_bool = {0}", _bool); • Console.WriteLine("_dec = {0}", _dec); • Console.WriteLine("_dou = {0}", _dou); • Console.WriteLine("_chr = {0}", _chr); • Console.ReadLine(); • } C# Programming with Visual C# 2010 Express

  9. การประกาศตัวแปร กฏข้อห้ามสำหรับการประกาศตัวแปร คือ • ใช้ตัวเลขขึ้นต้นชื่อตัวแปรไม่ได้ • ใช้คีย์เวิร์ดแทนชื่อตัวแปรไม่ได้ คำแนะนำสำหรับการประกาศชื่อตัวแปร ซึ่งไม่ผิดกฏ • ควรใช้อักษรพิมพ์เล็ก เป็นอักษรเริ่มต้น และอักษรในคำความหมายใหม่ ใช้อักษรพิมพ์ใหญ่เช่น myInt • ไม่ควรใช้คีย์เวิร์ด ที่เลี่ยงไปใช้อักษรพิมพ์เล็กหรือใหญ่ เพื่อให้เกิดความแตกต่าง • สำหรับชื่อตัวแปรที่เป็นชื่อ เมทธอด หรือชื่อคลาส นิยมใช้อักษรพิมพ์ใหญ่ขึ้นต้น เช่น MyClass • ควรใช้ชื่อตัวแปรที่มีความหมายเพื่อความเข้าใจง่าย • ชื่อตัวแปรค่าคงที่ ควรเป็นอักษรพิมพ์ใหญ่ C# Programming with Visual C# 2010 Express

  10. Anonymous Type Value Type varmyInt = 2; Class var v = new { Amount=108, Message="Hello" }; Array var anonArray = new[] { new { name = “A"}, new { name = “B" }}; LINQ var productQuery = from prod in products selectnew { prod.Color, prod.Price }; C# Programming with Visual C# 2010 Express

  11. การแปลงชนิดชนิดไทป์ การแปลงชนิดข้อมูลมีสองลักษณะที่ทำได้ • Casting ใช้แปลงชนิดข้อมูลในกลุ่มเดียวกัน • Boxing ใช้แปลชนิดข้อมูลข้ามกลุ่มกัน แปลงชนิดข้อมูลมีสองวิธีคือ • Implicit เป็นการแปลงทางอ้อม CLR จะทำให้อัตโนมัติ • Explicit เป็นการแปลงทางตรง ที่จะเขียนชนิดข้อมูลที่แปลง int a = 4; double b; b = a //Implicit conversion of int to doubleb = (int) a; //Explicit conversion (casting) of int to double C# Programming with Visual C# 2010 Express

  12. ตาราง 3.4 การแปลงค่าแบบอิมพลิสิท C# Programming with Visual C# 2010 Express

  13. เมธอด TryParse( ) int number = 0; string numberString = "1234"; if (int.TryParse(numberString, out number)) { Console.WriteLine("Conversion succeeded"); Console.WriteLine(number); } else { Console.WriteLine("failed"); } C# Programming with Visual C# 2010 Express

  14. ขอบเขตของตัวแปร • Block เป็นระดับที่เล็กที่สุด เช่น บล็อกของ ของคำสั่ง if, while • Procedure เป็นระดับเมทธอด พล็อบเพอร์ตี้ ตัวแปรภายในโพรซีเตอร์จะใช้ได้เฉพาะโพรซีเดอร์นี้เท่านั้น if (x > 10) { int x = x * x; } void WhatYourName( ) { string name = Console.Read( ); Consoel.Writeln("Hello " + name); } C# Programming with Visual C# 2010 Express

  15. ขอบเขตของตัวแปร (ต่อ) • Module เป็นการอ้างอิงในการใช้สมาชิกในโมดูล ที่มีทั้ง สมาชิกของโมดูล โพรซีเดอร์ บล็อก และคลาส มักใช้กับตัวแปรที่ประกาศเป็น private public class Person { private string name; public void SetName(string x) { name = x; } public string GetName( ) { return name; } } C# Programming with Visual C# 2010 Express

  16. ขอบเขตของตัวแปร (ต่อ) • Namespace เป็นการอ้างอิงในระดับที่กว้างที่สุด ที่มีทั้งระดับ บล็อก โพรซีเดอร์ และโมดูล มักใช้คีย์เวิร์ด public ที่ทำให้คลาสอื่นๆ สามารถใช้งานตัวแปรในทุกส่วนภายในเนมสเปสเดียวกันได้ namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Person p1; // สามารถใช้ Person ได้ เพราะ มีคีย์เวิร์ด public นำหน้า Person } } public class Person { private string name; public void SetName(string x) { name = x; } } } C# Programming with Visual C# 2010 Express

  17. ค่าคงที่ และตัวแปรอ่านได้อย่างเดียว • ตัวแปรค่าคงที่ (Constant variable) จะต้องระบุค่าเริ่มต้นด้วย ณ ตอนประกาศค่าตัวแปร • ตัวแปรอ่านได้อย่างเดียว (Read-only variable)จะต้องระบุค่าเริ่มต้นด้วย ณ ตอนประกาศค่าตัวแปร หรือระบุตอนสร้างคอนสตรักเตอร์ (constructor) เมทธอดคอนสตรักเตอร์จะทำงานตอนสร้างวัตถุ class Program { readonly int y; //read-only variable Program()//constructor { y=1; } static void Main(string[] args) { Program p = new Program(); const int x=2; //constant variable } } C# Programming with Visual C# 2010 Express

  18. การดำเนินการของข้อมูลการดำเนินการของข้อมูล • การดำเนินการกับตัวแปร หรือที่เรียกว่า เอ็กเพรสชัน (Expression) ประกอบด้วย ตัวถูกดำเนินการ (Operands) ตัวดำเนินการ (Operator) ตัวอย่างเช่น a + b โดยที่ a, b เป็น Operands และ + เป็น Operator C# Programming with Visual C# 2010 Express

  19. การดำเนินการทางคณิตศาสตร์อย่างลัดการดำเนินการทางคณิตศาสตร์อย่างลัด var += expression; // var = var + expression var -= expression; // var = var - expression var *= expression; // var = var * expression var /= expression; // var = var / expression var %= expression; // var = var % expression item[(index + 1) % 32] = item[( idex + 1) % 32 ] + 1 = item[(index + 1) % 32] += 1; C# Programming with Visual C# 2010 Express

  20. ตาราง 3.5 ตารางความจริง • สำหรับการดำเนินทางตรรกะ จะใช้ความพื้นฐานของตารางความจริง เช่น การใช้ตรรกะเปรียบเทียบการใช้คำพูดว่า (คนเป็นสัตว์เลือดเย็น) และ (คนเป็นสัตย์เลี้ยงลูกด้วยนม) มีเท่ากับ FalseAND True = False C# Programming with Visual C# 2010 Express

  21. ตัวอย่างการดำเนินการทางตรรกะ ทดสอบการใช้ตรรกะ OR bool a, b, c; a = true; b = true; c = false; Console.WriteLine(a | b | c);// Print True ทดสอบการใช้ตรรกะ Short OR bool a, b, c; a = true; b = true; c = false; Console.WriteLine(a || b || c);// Print True C# Programming with Visual C# 2010 Express

  22. ~ 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0- 1 1 1 1 0 1 1 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 1 0 1 1 ^ | & การดำเนินการกับบิท • การดำเนินการทางตรรกะ ยังประยุกต์ได้กับบิท ซึ่ง มีตัวดำเนินการ ^ แทน XOR, ~ แทน Complement, & แทน AND, และ| แทน OR C# Programming with Visual C# 2010 Express

  23. ตัวอย่างการดำเนินการทางตรรกะ ทดสอบการ bitwise AND int a, b; a = 7; //00000111 b = 85;//01010101 Console.WriteLine(a & b);//print 5 //0000 0101 ทดสอบการ bitwise XOR int a, b; a = 7; //00000111 b = 85;//01010101 Console.WriteLine(a ^ b);//print 82 //0101 0010 C# Programming with Visual C# 2010 Express

  24. ลำดับการทำงานของของตัวดำเนินการ • ลำดับการดำเนินการมีความสำคัญ เพราะหากเข้าใจผิด อาจทำให้เขียนโปรแกรมผิดพลาดโดยไม่รู้ตัวได้ หรือที่เรียกว่า logic error • ตัวอย่างเช่น 5 – 2 * 4 = 5 – 8 = -3 C# Programming with Visual C# 2010 Express

  25. การสร้างชนิดข้อมูลใช้เอง • อีนัม (enum) เป็นชนิดข้อมูลที่เหมาะกับการใช้กับข้อมูลที่ได้กำหนดเป็นชุดๆ หรือประเภท การกำหนดของอีนัม ใช้คีย์เวิร์ด enum แล้วตามด้วยชื่อตัวแปร และค่าเริ่มต้น เช่น enum Color{Blue, Yellow, Red} • สตรัก (struct) เป็นชนิดข้อมูลที่เหมาะกับการเก็บโครงสร้าง อย่าง เป็นแถว การใช้งานสตรัก เริ่มต้นจากประกาศชนิดข้อมูลด้วยคีย์เวิร์ด stuct แล้วตัวแปรชื่อตัวแปร ภายในบล็อก จะประกาศโครงสร้างย่อยๆ สำหรับการเรียกใช้งาน ทำได้เพียงประกาศตัวแปร และเรียกใช้ด้วยเรียกชื่อตัวแปรและจุดตามด้วยชื่อข้อมูลในโครงสร้าง C# Programming with Visual C# 2010 Express

  26. ตัวอย่างการใช้อีนัม • private enum Color {Blue, Yellow, Red} • static void Main(string[] args){ • Color Color1, Color2, Color3; • Color1 = Color.Blue; • Color2 = (Color)1;//Casting to Color enum • Color3 = (Color)2; • Console.WriteLine("myColor:{0}", Color1); • Console.WriteLine("myColor:{0}", Color2); • Console.WriteLine("myColor:{0}", Color3); • } C# Programming with Visual C# 2010 Express

  27. ตัวอย่างการใช้สตรักต์ตัวอย่างการใช้สตรักต์ • public struct Customer { • public string name; • public string lastName; • public string tel; • } •  static void Main(string[] args) { • Customer Ae; • Ae.name = "Vilasinee"; • Ae.lastName = "Choti"; • Ae.tel = "01-234-5678"; • Console.WriteLine("Name:{0} {1} tel: {2}", • Ae.name, Ae.lastName, Ae.tel); • } C# Programming with Visual C# 2010 Express

  28. ทำถามทบทวน • ภาษาซีชาร์ป มีระบบไทป์(Type System) เป็นแบบใด และมีความหมายไทป์นั้นเป็นอย่างไร • จะใช้ชนิดข้อมูลอะไรเก็บอักษรเพียงตัวเดียว • การประกาศตัวแปรในข้อใด ไม่สามารถทำได้ และข้อใดไม่ควรใช้ • char 5Letter; • int _int; • float This; • string MYSTRING • double $double; • ภายในเนมสเปสเดียวกัน ประกอบด้วยสองคลาส จะทำอย่างไรที่จะประกาศตัวแปรให้อีกคลาสหนึ่งใช้ตัวแปรของอีกคลาสหนึ่งได้ จะต้องประกาศตัวแปรเป็นอย่างไร C# Programming with Visual C# 2010 Express

  29. ทำถามทบทวน (ต่อ) • ให้หาผลลัพธ์ของ การดำเนินการของข้อย่อยต่อไปนี้ และเมื่อกำหนดให้ a = 5 ก. –a ข. a-- ค. 5 / 2 ง. 5.0 / 2 จ. “99” + “1” ฉ. 5==3?1:2 ช. 3 * 5 + 2 * 2 • จากการโค้ดต่อไปนี้ ส่วนใดเป็นแปลงข้อมูลแบบ อิมพลิสิท และเป็นการแปลงข้อมูลแบบ เอ็กพลิสิท และโปรแกรมนี้มีความผิดพลาด ให้อธิบายความผิดพลาดในการแปลงข้อมูลด้วย int a = 4; float b; b = (float)a + 1.2; • ในซีชาร์ปใช้เมทธอดอะไรเพื่อทดสอบความผิดพลาดในแปลงชนิดข้อมูลหนึ่งไปเป็นอีกชนิดข้อมูลหนึ่ง • อะไรคือความเหมือนและต่างที่สำคัญของตัวแปรประเภท read-only และ const • ในกรณีใดที่ควรใช้การกำหนดชนิดข้อมูลใช้เอง C# Programming with Visual C# 2010 Express

More Related