Type Casting or Type Conversion

กฤตนัย วันดี 650710212

Type Casting คือ

การแปลงประเภทข้อมูลเกิดขึ้นเมื่อเรานำค่าจากชนิดข้อมูลหนึ่งไปใส่ในอีกชนิดหนึ่ง หากชนิดข้อมูลสามารถเข้ากันได้ C# จะทำการแปลงอัตโนมัติให้ แต่หากไม่สามารถเข้ากันได้ จะต้องทำการแปลงโดยผู้ใช้เอง ซึ่งเรียกว่า Explicit Type Conversion เช่น การนำค่า int ไปใส่ในตัวแปรชนิด long

ตัวอย่างเช่น ใน C# ประเภทข้อมูลตัวเลขจะเข้ากันได้ แต่ไม่มีการรองรับการแปลงอัตโนมัติจากประเภทตัวเลขเป็น char หรือ boolean นอกจากนี้ char และ boolean ยังไม่เข้ากันได้ ก่อนที่จะทำการแปลง คอมไพเลอร์จะตรวจสอบความเข้ากันได้ตามรูปต่อไปนี้ก่อน จากนั้นจึงตัดสินใจว่าเข้ากันได้หรือไม่หรือมีข้อผิดพลาดเกิดขึ้น

ตารางที่แสดงถึงการแปลงประเภทข้อมูลโดยอัตโนมัติ (Implicit Type Casting) ที่ C# รองรับนั้นอธิบายถึงชนิดข้อมูลที่สามารถแปลงกันได้โดยไม่ต้องใช้การแปลงแบบกำหนดเอง (Explicit) เช่น การแปลงจาก int ไปเป็น long หรือจาก float ไปเป็น double ชนิดข้อมูลเหล่านี้สามารถแปลงได้โดยไม่มีการสูญเสียข้อมูล เนื่องจากขนาดหรือความแม่นยำของชนิดข้อมูลเป้าหมายมีมากกว่าหรือเท่ากับข้อมูลต้นทาง
แปลงจากชนิตข้อมูล
แปลงเป็นชนิดข้อมูล

byte

short, int, long, float, double

short

int, long, float, double

int

long, float, double

long

float, double

float

double

ตัวอย่างโค้ด Implicit Type Conversion

// Some code
// C# program to demonstrate the 
// Implicit Type Conversion 
using System; 
namespace Casting{ 
  
class GFG { 
  
        // Main Method 
        public static void Main(String []args) 
        { 
            int i = 57;  
              
            // automatic type conversion 
            long l = i;  
               
            // automatic type conversion 
            float f = l; 
              
            // Display Result 
            Console.WriteLine("Int value "  +i); 
            Console.WriteLine("Long value "  +l); 
            Console.WriteLine("Float value "  +f); 
        } 
} 
} 

ผลลัพท์

Int value 57
Long value 57
Float value 57

ตัวอย่างเมื่อประเภทข้อมูลไม่เข้ากัน

// C# program to illustrate incompatible data 
// type for explicit type conversion 
using System; 
namespace Casting{
class GFG {
	// Main Method 
	public static void Main(String []args) 
	{ 
		double d = 765.12; 

		// Incompatible Data Type 
		int i = d; 
		
		// Display Result	 
		Console.WriteLine("Value of i is ", +i); 
	} 
     } 
}

ผลลัพท์

prog.cs(14,21): error CS0266: Cannot implicitly convert type `double' to `int'.
An explicit conversion exists (are you missing a cast?)

ดังนั้น หากเราต้องการกำหนดค่าประเภทข้อมูลขนาดใหญ่ให้กับประเภทข้อมูลขนาดเล็ก เราจะดำเนินการแปลงประเภทโดยชัดเจน

  • สิ่งนี้มีประโยชน์สำหรับประเภทข้อมูลที่ไม่เข้ากันซึ่งไม่สามารถทำการแปลงอัตโนมัติได้

  • ในที่นี้ ชนิดข้อมูลเป้าหมาย (target-type) จะระบุชนิดที่ต้องการแปลงค่าที่กำหนดไปยังชนิดนั้น โดยการแปลงนี้สามารถใช้กับข้อมูลที่ไม่เข้ากันและไม่สามารถแปลงได้โดยอัตโนมัติ ซึ่งอาจทำให้เกิดการสูญเสียข้อมูลได้ในบางกรณี

  • การแปลงชนิดข้อมูลบางครั้งอาจส่งผลให้เกิดการสูญเสียข้อมูล (lossy conversion) ซึ่งหมายถึงเมื่อข้อมูลจากชนิดที่ใหญ่กว่าถูกแปลงไปยังชนิดที่เล็กกว่า จะมีข้อมูลบางส่วนหายไปหรือไม่สามารถเก็บรักษาทุกค่าได้ เช่น การแปลงจาก float หรือ double เป็น int อาจทำให้สูญเสียทศนิยมและมีผลต่อความถูกต้องของข้อมูล

ตัวอย่างโค้ด Explicit Type Casting

using System;

namespace TypeConversionApplication {
   class ExplicitConversion {
      static void Main(string[] args) {
         double d = 5673.74; 
         int i;
         
         // cast double to int.
         i = (int)d;
         Console.WriteLine(i);
         Console.ReadKey();
      }
   }
}

ผลลัพท์

d = 5673.74 
i = int(d)
print(i)

วิธีการแปลงประเภท C#

C# ให้วิธีการแปลงประเภทในตัวดังต่อไปนี้

ลำดับที่
วิธีการและคำอธิบาย

1

ToBoolean

แปลงชนิดข้อมูลเป็นค่า Boolean หากทำได้

2

ToByte

แปลงชนิดข้อมูลเป็น byte

3

ToChar

แปลงชนิดข้อมูลเป็นตัวอักษร Unicode

4

ToDateTime

แปลงชนิดข้อมูล (ตัวเลขหรือสตริง) เป็น DateTime

5

ToDecimal

แปลงชนิดข้อมูลเป็น decimal

6

ToDouble

แปลงชนิดข้อมูลเป็น double

7

ToInt16

แปลงเป็น 16-bit integer

8

ToInt32

แปลงเป็น 32-bit integer

9

ToInt64

แปลงเป็น 64-bit integer

10

ToSbyte

แปลงเป็น signed byte

11

ToSingle

แปลงเป็นตัวเลขจุดทศนิยมเล็ก.

12

ToString

แปลงเป็นสตริง

13

ToType

แปลงเป็นชนิดข้อมูลที่กำหนด.

14

ToUInt16

แปลงเป็น unsigned 16-bit integer.

15

ToUInt32

แปลงเป็น unsigned 32-bit integer

16

ToUInt64

แปลงเป็น unsigned 64-bit integer

ตัวอย่างแปลงประเภทค่าต่างๆ ให้เป็นประเภทสตริง

using System;

namespace TypeConversionApplication {
   class StringConversion {
      static void Main(string[] args) {
         int i = 75;
         float f = 53.005f;
         double d = 2345.7652;
         bool b = true;

         Console.WriteLine(i.ToString());
         Console.WriteLine(f.ToString());
         Console.WriteLine(d.ToString());
         Console.WriteLine(b.ToString());
         Console.ReadKey();
            
      }
   }
}

ผลลัพท์

75
53.005
2345.7652
True

Presentation

Video Clip

Reference

เนื้อหา Type Casting และโค้ด Implicit Type Conversion

เนื้อหา วิธีการแปลงประเภทของ C# และ โค้ด explicit type conversion กับ การแปลงค่าต่างๆให้เป็นสตริง

Last updated