ภาษาซีชาร์ป (C# Programming Language) เป็นภาษาโปรแกรมระดับสูง ที่ใช้ระบบชนิดข้อมูลแบบรัดกุม (strong typing) และสนับสนุนการเขียนโปรแกรมเชิงคำสั่ง การเขียนโปรแกรมเชิงประกาศ การเขียนโปรแกรมเชิงฟังก์ชัน การเขียนโปรแกรมเชิงกระบวนการ การเขียนโปรแกรมเชิงวัตถุ (แบบคลาส) และการเขียนโปรแกรมเชิงส่วนประกอบ

C#
กระบวนทัศน์Multi-paradigm: structured, imperative, object-oriented, event-driven, task-driven, functional, generic, reflective, concurrent
ตระกูลC
ผู้ออกแบบAnders Hejlsberg (Microsoft)
ผู้พัฒนาMads Torgersen (Microsoft)
เริ่มเมื่อ2000; 24 ปีที่แล้ว (2000)[1]
รุ่นเสถียร
12.0[2] แก้ไขบนวิกิสนเทศ / 14 พฤศจิกายน 2023; 3 เดือนก่อน (14 พฤศจิกายน 2023)
ระบบชนิดตัวแปรStatic, dynamic,[3] strong, safe, nominative, partially inferred
แพลตฟอร์มCommon Language Infrastructure
สัญญาอนุญาต
นามสกุลของไฟล์.cs, .csx
เว็บไซต์learn.microsoft.com/en-us/dotnet/csharp/
ตัวแปลภาษาหลัก
Visual C#, .NET, .NET Framework (discontinued), Mono, DotGNU (discontinued), Universal Windows Platform
ภาษาย่อย
, Polyphonic C#, Enhanced C#
ได้รับอิทธิพลจาก
C++,[6] , Eiffel, F#,[a] Haskell, Scala, Icon, J#, J++, Java,[6] ML, Modula-3, Object Pascal,[7] VB
ส่งอิทธิพลต่อ
Chapel,[8] Clojure,[9] Crystal,[10] D, J#, Dart,[11] F#, Hack, Java,[12][13] Kotlin, Nemerle, Oxygene, Rust,[14] Swift,[15] Vala, TypeScript

ภาษานี้พัฒนาเริ่มแรกโดยโดยมีอนัส ไฮลส์เบิร์ก (Anders Hejlsberg) จากบริษัทไมโครซอฟท์ ในปีพ.ศ. 2543 ต่อมามีการรับรองให้เป็นมาตรฐานโดยเอ็กมาอินเตอร์เนชันแนล (ECMA-334) ในปีพ.ศ. 2545 และองค์การระหว่างประเทศว่าด้วยการมาตรฐาน (ISO/IEC 23270) ในปีพ.ศ. 2546 ไมโครซอฟท์ได้เปิดตัวภาษาซีชาร์ปพร้อมกับดอตเน็ตเฟรมเวิร์ก และVisual Studio ซิ่งเป็นผลิตภัณฑ์โคลสซอร์ส (closed-source) ทั้งหมด เนื่องจากตอนนั้นไมโครซอฟต์ไม่มีผลิตภัณฑ์ที่เป็นโอเพ่นซอร์ส. ต่อมาไมโครซอฟท์ได้เปิดตัว Visual Studio Code, Roslyn และ ดอตเน็ตคอร์ ซี่งทั้งหมตนั้นรองรับภาษาซีชาร์ป เป็นซอฟต์แวร์ฟรีและโอเพ่นซอร์ส และทำงานแบบครอสแพลตฟอร์ม

ปัจจุบันภาษาซีชาร์ปมีรุ่นล่าสุดคือ C# 11.0 ที่ออกมาพร้อมกับ .NET 7.0 ในปี พ.ศ. 2565[16][17]

ตัวอย่าง แก้

ตัวอย่างต่อไปนี้ คือตัวอย่างโปรแกรม Hello world ใน C# ซึ่งใช้ฟีเจอร์ top-level statement ที่เริ่มมีให้ใช้ได้ตั้งแต่ C# 9.0

using System;

Console.WriteLine("Hello, world!");

แต่สำหรับ C# 8.0 หรือก่อนหน้านั้น การเขียนจะต้องเป็นฟังก์ชันที่อยู่ในคลาสดังทำนองนี้

public class ExampleClass
{
    public static void Main()
    {
        System.Console.WriteLine("Hello, world!");
    }
}

ผลของการทำงานคือมีการแสดงคำว่า Hello, world! ในหน้าต่างคอนโซล โดยในแต่ละบรรทัดมีความหมายดังนี้:

public class ExampleClass

บรรทัดนี้คือการประกาศคลาส โดย public หมายถึงวัตถุที่สร้างในโครงการ (project) อื่น ๆ สามารถเข้าใช้งานคลาสนี้ได้ ไม่จำกัด. ข้อมูลต่างๆ ที่อยู่ผ่านใต้หน่วยของบรรทัดนี้ จะใช้ในการทำงานของคลาสนี้

public static void Main()

บรรทัดนี้เป็นจุดที่ใช้ในการเริ่มการทำงานของโปรแกรม เมื่อโปรแกรมทำงาน โดยสามารถเรียกใช้จากโปรแกรมอื่นได้โดยการใช้ไวยากรณ์ ExampleClass.Main(). (public static void เป็นส่วนที่ใช้ในการทำงาน ซึ่งต้องเรียนรู้ในการเขียนขั้นสูง)

System.Console.WriteLine("Hello, world!");

ในบรรทัดนี้ เป็นการทำงาน เพื่อแสดงผลออกมา Console คือโปรแกรมระบบ, ซึ่งก็คือ โปรแกรมระบบแบบสั่งคำสั่งที่ละบรรทัด (เช่น DOS) ที่สามารถรับข้อมูลและแสดงผลเป็นข้อความได้. จากที่เราเขียนโปรแกรมจะทำการเรียก Console โดยใช้คำสั่ง WriteLine, ซึ่งทำให้สามารถส่งค่าข้อความออกมาแสดงผลได้

มาตรฐาน แก้

ไมโครซอฟท์ส่งมาตรฐานภาษาซีชาร์ปให้กับ Ecma และได้รับการยอมรับเป็นมาตรฐาน ECMA ในเดือนธันวาคม ค.ศ. 2001 ในชื่อว่า ECMA-334 C# Language Specification ใน ค.ศ. 2003 ภาษาซีชาร์ปได้รับการยอมรับเป็นมาตรฐาน ISO (ISO/IEC 23270).

มาตรฐาน ISO/IEC 23270:2003 ระบุรูปแบบ และกำหนดการแปล (ตีความ) โปรแกรมที่เขียนด้วยภาษาซีชาร์ป. โดยตัวมาตรฐานได้ระบุ:

  • รูปแบบการนำเสนอ (the representation of C# programs)
  • ไวยากรณ์ (the syntax and constraints of the C# language)
  • กฎการตีความสำหรับแปลโปรแกรมภาษาซีชาร์ป (the semantic rules for interpreting C# programs)
  • ข้อห้าม และข้อจำกัด ของเครื่องมือที่สร้างตามข้อกำหนดของซีชาร์ป (the restrictions and limits imposed by a conforming implementation of C#)

ISO/IEC 23270:2003 ไม่ได้ระบุ:

  • กลไกในการแปลงโปรแกรมภาษาซีชาร์ป เพื่อใช้ในระบบประมวลผลข้อมูล (the mechanism by which C# programs are transformed for use by a data-processing system)
  • กลไกในการเรียกให้โปรแกรมภาษาซีชาร์ปทำงาน เพื่อใช้ในระบบประมวลผลข้อมูล (the mechanism by which C# applications are invoked for use by a data-processing system)
  • กลไกในการแปลงข้อมูลเข้า เพื่อใช้กับโปรแกรมภาษาซีชาร์ป (the mechanism by which input data are transformed for use by a C# application)
  • กลไกในการแปลงข้อมูลออก หลังจากถูกประมวลผลโดยโปรแกรมภาษาซีชาร์ป (the mechanism by which output data are transformed after being produced by a C# application)
  • the size or complexity of a program and its data that will exceed the capacity of any specific data-processing system or the capacity of a particular processor;
  • all minimal requirements of a data-processing system that is capable of supporting a conforming implementation.

นอกจากนี้ตัวมาตรฐานไม่ได้กล่าวถึงโครงสร้างข้อมูล และตัวไลบรารีกลางของ .NET Framework ซึ่งเป็นหัวใจสำคัญของการเขียนโปรแกรมด้วยภาษาซีชาร์ปเลย[18]

หมายเหตุ แก้

  1. for async

อ้างอิง แก้

  1. "InfoQ eMag: A Preview of C# 7".
  2. "Announcing C# 12". สืบค้นเมื่อ 18 พฤศจิกายน 2023.
  3. อ้างอิงผิดพลาด: ป้ายระบุ <ref> ไม่ถูกต้อง ไม่มีการกำหนดข้อความสำหรับอ้างอิงชื่อ dynamic
  4. "The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.: dotnet/roslyn". November 13, 2019 – โดยทาง GitHub.
  5. "CoreCLR is the runtime for .NET Core. It includes the garbage collector, JIT compiler, primitive data types and low-level classes.: dotnet/coreclr". November 13, 2019 – โดยทาง GitHub.
  6. 6.0 6.1 อ้างอิงผิดพลาด: ป้ายระบุ <ref> ไม่ถูกต้อง ไม่มีการกำหนดข้อความสำหรับอ้างอิงชื่อ influenced by CPP
  7. อ้างอิงผิดพลาด: ป้ายระบุ <ref> ไม่ถูกต้อง ไม่มีการกำหนดข้อความสำหรับอ้างอิงชื่อ influenced by op
  8. "Chapel spec (Acknowledgments)" (PDF). Cray Inc. 2015-10-01. คลังข้อมูลเก่าเก็บจากแหล่งเดิม (PDF)เมื่อ 2016-02-05. สืบค้นเมื่อ 2016-01-14.
  9. "Rich Hickey Q&A by Michael Fogus". คลังข้อมูลเก่าเก็บจากแหล่งเดิมเมื่อ 2017-01-11. สืบค้นเมื่อ 2017-01-11.
  10. Borenszweig, Ary (June 14, 2016). "Crystal 0.18.0 released!". It's heavily inspired by Ruby, and other languages (like C#, Go and Python).
  11. "Web Languages and VMs: Fast Code is Always in Fashion. (V8, Dart) - Google I/O 2013". YouTube. เก็บจากแหล่งเดิมเมื่อ 2021-12-21. สืบค้นเมื่อ 22 December 2013.
  12. Java 5.0 added several new language features (the enhanced for loop, autoboxing, varargs and annotations), after they were introduced in the similar (and competing) C# language [1] [2]
  13. อ้างอิงผิดพลาด: ป้ายระบุ <ref> ไม่ถูกต้อง ไม่มีการกำหนดข้อความสำหรับอ้างอิงชื่อ influenced
  14. "Influences - The Rust Reference". The Rust Reference. สืบค้นเมื่อ 2023-04-18.
  15. Lattner, Chris (2014-06-03). "Chris Lattner's Homepage". Chris Lattner. สืบค้นเมื่อ 2020-05-12. The Swift language is the product of tireless effort from a team of language experts, documentation gurus, compiler optimization ninjas, and an incredibly important internal dogfooding group who provided feedback to help refine and battle-test ideas. Of course, it also greatly benefited from the experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.
  16. "Welcome to C# 11". November 11, 2022.
  17. "Announcing .NET 7 -- the Fastest .NET Yet". November 11, 2022.[ลิงก์เสีย]
  18. "ISO/IEC 23270:2003 Information technology".

แหล่งข้อมูลอื่น แก้