Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

String Literal เทียบ String Object ใน C#

String Literals (สตริงลิเทอรัล)

ค่าสตริงที่เขียน "ตรงๆ" ในโค้ด ใช้ Double Quotes หรือ Verbatim String (@)

รูปแบบ String Literals

// Regular String Literal
string s1 = "Hello, World";
string s2 = "Path: C:\\Windows\\System32"; // ต้อง Escape Backslash

// Verbatim String Literal (ด้วย @) - ไม่ต้อง Escape
string s3 = @"Hello, World";
string s4 = @"Path: C:\Windows\System32"; // อ่านง่าย
string s5 = @"Line 1
Line 2
Line 3"; // Multi-line String

// Escape Sequences ใน Regular String
string tab = "Col1\tCol2\tCol3";
string newline = "Line1\nLine2";
string unicode = "\u0041\u0042"; // "AB"

ตัวอย่างการใช้งาน String Literals

using System;

namespace Demo {
    class Program {
        static void Main(string[] args) {
            // Regular string
            string str1 = "Hello, World";
            Console.WriteLine(str1);
            
            // Multi-line verbatim string
            string str2 = @"Welcome,
Hope you are doing great!";
            Console.WriteLine(str2);
        }
    }
}

String Object (อ็อบเจ็กต์สตริง)

อ็อบเจ็กต์ String ที่สร้างขึ้นใน Runtime ด้วยวิธีต่างๆ

วิธีสร้าง String Object

วิธี ตัวอย่าง คำอธิบาย
1. Assign Literal string s = "hello"; Compiler สร้าง Object ให้อัตโนมัติ
2. Constructor string s = new string('*', 10); สร้างจาก Char Array หรือ Char ซ้ำ
3. Concatenation string s = "Hello" + " " + "World"; รวมสตริง (สร้าง Object ใหม่)
4. Property/Method string s = DateTime.Now.ToString(); ดึงจาก Property หรือ Method
5. Formatting string s = string.Format("{0:C}", 123.45); Format ค่าเป็น String
6. StringBuilder string s = sb.ToString(); สร้างจาก StringBuilder
7. Interpolation (C# 6+) string s = $"Hello {name}"; สะดวก อ่านง่าย ประสิทธิภาพดี

String Literal vs String Object - จริงๆ แล้วเหมือนกัน!

ใน C# String Literal ก็คือ String Object คอมไพเลอร์จะสร้าง Object ให้อัตโนมัติ

// ทั้งคู่นี้คือ String Object (System.String)
string literal = "hello";
string fromConstructor = new string('h', 1) + "ello";
string fromFormat = string.Format("h{0}", "ello");

// ทั้งหมด Type เหมือนกัน
Console.WriteLine(literal.GetType());        // System.String
Console.WriteLine(fromConstructor.GetType()); // System.String
Console.WriteLine(fromFormat.GetType());      // System.String

String Interning (การจัดเก็บสตริงซ้ำ)

.NET มี Mechanism ชื่อ String Interning - String Literals ที่เหมือนกันจะชี้ไปที่ Object เดียวกันใน Memory Pool

string a = "hello";
string b = "hello";
string c = string.Intern(new string('h', 1) + "ello");

Console.WriteLine(object.ReferenceEquals(a, b)); // True - Same Interned Object
Console.WriteLine(object.ReferenceEquals(a, c)); // True - Interned

// แต่ Constructor สร้าง Object ใหม่เสมอ (เว้นแต่ Intern)
string d = new string('h', 1) + "ello";
Console.WriteLine(object.ReferenceEquals(a, d)); // False - Different Objects

ตัวอย่างเปรียบเทียบสตริง

using System;

namespace Demo {
    class Program {
        static void Main(string[] args) {
            string str1 = "John";
            string str2 = "Andy";
            
            // ใช้ String.Compare (Static Method) - แนะนำ
            if (String.Compare(str1, str2) == 0) {
                Console.WriteLine("{0} and {1} are equal strings.", str1, str2);
            } else {
                Console.WriteLine("{0} and {1} are not equal strings.", str1, str2);
            }
            
            // หรือใช้ Equals
            Console.WriteLine(str1.Equals(str2)); // False
            Console.WriteLine(str1 == str2);      // False (Operator Overload)
            
            Console.ReadKey();
        }
    }
}

สรุป

  • String Literal = Syntax สำหรับเขียนค่าสตริงในโค้ด
  • String Object = Instance ของ System.String ใน Memory
  • Literal จะกลายเป็น Object อัตโนมัติตอน Compile/Runtime
  • ใช้ String.Compare หรือ Equals สำหรับเปรียบเทียบ (ไม่ใช่ == สำหรับ Culture-sensitive)
  • String เป็น Immutable - เปลี่ยนค่า = สร้าง Object ใหม่