Data Types
ณัฏฐพล เรืองวราพิชญ์ 650710077
Data Types คืออะไร
Data types คือ ขนิดของข้อมูล โดยภาษา C# เป็น Strongly typed programming language แบ่งย่อยเป็น 3 ประเภทหลักๆ คือ Value Types, Reference Types และ Pointer Types
Value Types
Values Types คือ Data Type ที่สามารถเก็บค่าที่เป็น Instance หรือ ค่าชนิดที่เป็นไปได้โดยตรง แบ่งออกเป็น Built-in Value Types และ Non-Built-in Value Types
> Built-in Value Types ประกอบด้วย
Integral numeric types (ชนิดจำนวนเต็ม)
Floating-point numeric types (ชนิดทศนิยม)
Boolean (ชนิดตรรกะ)
char (ชนิดตัวอักษร)
> Non-Built-in Value Types ประกอบด้วย
Enumeration types
Structure types
Tuple types
Nullable value types
ในที่นี้ จะอธิบายโดยละเอียดเพียง Built-in Value Types
==> Built-in Value Types
1) Integral numeric types (ชนิดจำนวนเต็ม) มีหลากหลายแบบดังนี้
sbyte
signed-integer
-128 to 127
8
byte
unsigned-integer
0 to 255
8
short
signed-integer
-32,768 to 32,767
16
ushort
unsigned-integer
0 to 65,535
16
int
signed-integer
-2,147,483,648 to 2,147,483,647
32
uint
unsigned-integer
0 to 4,294,967,295
32
nint
int or long based on system
(int value) or (long value)
32 or 64
nuint
uint or ulong based on system
(uint value) or (ulong value)
32 or 64
long
signed-integer
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
64
ulong
unsigned-integer
0 to 18,446,744,073,709,551,615
64
ตัวอย่างการใช้งาน และ เปรียบเทียบกับภาษาอื่นๆ
using System;
public class IntegralExample
{
public static void Main(string[] args)
{
sbyte a_max = 127, a_min = -128;
byte b_max = 255, b_min = 0;
short c_max = 32767, c_min = -32768;
ushort d_max = 65535, d_min = 0;
int e_max = 2147483647, e_min = -2147483648;
uint f_max = 4294967295, f_min = 0;
nint g_max1 = 2147483647, g_min1 = -2147483648; // 32 bit
// if (OS is 64bit) then nint is 64bits
// if (OS is 32bit) then nint is 32bits
uint h_max1 = 2147483647, h_min1 = 0; // 32 bit
// if (OS is 64bit) then nuint is 64bits
// if (OS is 32bit) then nuint is 32bits
long i_max = 9223372036854775807, i_min = -9223372036854775808;
ulong j_max = 18446744073709551615, j_min = 0;
}
}
- C# : มี nint
และ nuint
ช่วยให้ช่วงค่าเปลี่ยนแปลงไปตาม System ที่กำลังรัน
- C : มีความสามารถในการจัดการ Memory ของตัวแปรเหมือน C#
- Java : มีความสามารถในการจัดการ Memory เหมาะสมกับคุณสมบัติการใช้งานของ Java
- Python : ค่าที่เป็นไปได้ไม่จำกัด แต่ ไม่สนใจ Memory ของตัวแปร
2) Floating-point numeric types (ชนิดทศนิยม) มี 3 ชนิดหลักๆ ดังนี้
float
32 bit - floating-point
ทศนิยม 6 - 8 ตำแหน่ง (7, 8 ค่าอาจเพื้ยน)
32
double
64 bit - floating-point
ทศนิยม 15 - 16 ตำแหน่ง (16 ค่าอาจเพื้ยน)
64
decimal
128 bit - BCD
ทศนิยม 28 - 29 ตำแหน่ง (29 ค่าอาจเพื้ยน)
128
ตัวอย่างการใช้งาน และ เปรียบเทียบกับภาษาอื่นๆ
using System;
public class FloatingTest
{
public static void Main(string[] args)
{
float x = 1.01234f;
Console.WriteLine(x); // 1.01234
double y = 1.0123456789;
Console.WriteLine(y); // 1.0123456789
decimal z = 1.012345678901234567890123m;
Console.WriteLine(z); // 1.012345678901234567890123
}
}
- C# : มี float
, double
, decimal
ในการจัดการเลขทศนิยม ตามความละเอียดที่ได้กล่าวมาข้างต้น
- C : มี float
, double
เหมือน C# แต่หากต้องการความละเอียดเพิ่ม ต้องเป็น long double
(ทศนิยมสูงสุด 18 ตำแหน่ง)
- Java : มี float
, double
เหมือน C# และใช้ java.math.BigDecimal
(ทศนิยมสูงสุดไม่จำกัด) แทน decimal
- Python : float
เป็น Double precision
3) Boolean (ชนิดตรรกะ)
โดยช่วงค่าที่เป็นไปได้คือ จริง/เท็จ
ตัวอย่างการใช้งาน และ เปรียบเทียบกับภาษาอื่นๆ
using System;
public class BooleanTest {
public static void Main(string[] args) {
bool x = true;
bool y = false;
}
}
- C# : มี bool
ในการใช้ boolean
- C : เนื่องจากว่า C ไม่มี Boolean จึงต้อง #include <stdbool.h>
จึงจะสามารถใช้ bool
ได้
- Java : มี boolean
ในการใช้ boolean
- Python : มีชนิดตรรกะ แต่ค่าต้องเป็น Uppercase ex. True, False
4) char (ชนิดตัวอักษร)
โดยช่วงค่าที่เป็นไปได้ของ C# คือ Character ที่จัดอยู่ใน Unicode (มีภาษาไทย)
ตัวอย่างการใช้งาน และ เปรียบเทียบกับภาษาอื่นๆ
using System;
public class CharTest {
public static void Main(string[] args) {
char c = 'ก';
}
}
- C#, Java, Python มี char เป็น Unicode
- C - ไม่สามารถเก็บค่าที่นอกเหนือจาก ASCII code ได้
Reference Types
Reference Types คือ Data Type ที่ตัวมันไม่ได้เก็บค่า แต่เก็บการอ้างอิงไปหาค่า แบ่งออกเป็น Built-in Reference Types และ Non-Built-in Reference Types
> Built-in Reference Types ประกอบด้วย
Object (ชนิดวัตถุ)
String (ชนิดสายอักขระ)
Delegate
Dynamic
> Non-Built-in Reference Types ประกอบด้วย
Record
Class
Interface
Nullable Reference types
Collection and Array
ในที่นี้ จะอธิบายโดยละเอียดเพียง String
==> String (ชนิดสายอักขระ)
โดยช่วงค่าที่เป็นไปได้ของ C# คือ ชุดของ Character ที่จัดอยู่ใน Unicode (มีภาษาไทย)
ตัวอย่างการใช้งาน และ เปรียบเทียบกับภาษาอื่นๆ
using System;
public class StringTest {
public static void Main(string[] args) {
string c = "Hello String.";
}
}
- C#, Python มี string เป็น Unicode
- Java มี class String เป็น Unicode
- C ไม่มี string ต้องใช้ Array of Character แทน
Pointer Types
Pointer คือ ตัวแปรที่เก็บ ตำแหน่งของข้อมูล ในหน่วยความจำ
ตัวอย่างการใช้งาน และ เปรียบเทียบกับภาษาอื่นๆ
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
int x = 5;
int* ptr = &x;
Console.WriteLine((IntPtr)ptr); // Address
Console.WriteLine(*ptr); // Value at the address
}
}
- C#, C มี Pointer สำหรับจัดการการเข้าถึงตำแหน่งของตัวแปร
- Java, Python ไม่มี Pointer
Presentation
Video Clip
แหล่งอ้างอิง
C#
Microsoft 2024. (2022, September 9). Value types (C# reference). Value Types
Microsoft 2024. (2024, March 30). Reference types (C# reference). Reference Types
loyart. (2007, January 2). ตอน 23 ตัวแปร และตัวแปรแบบ pointer ในภาษา C#. Pointer in C#
C
The Open Group Base Specifications Issue 6. (2004). stdint.h - integer types. <stdint.h> library
The Open Group Base Specifications Issue 6. (2004). stdbool.h - boolean type and values. <stdbool.h> library
Piravit Chenpittaya. (2020, October 18). C — Pointer คืออะไร | ฉบับเด็กมหาลัย. Pointer in C
Java
Java™ Platform Standard Ed. 8. (2004). Class BigDecimal. bigdecimal
peterdevpl. (2021, Febuary 11). All you need to know about Java’s BigDecimal. max precision in java bigdecimal
Python
senderle. (2011, September 30). Maximum and Minimum values for ints. Max and Min values for integer in python 3
Last updated