|   | 
| ปรับปรุง : 2567-02-07 (ปรับ code) |  | 
|   ::odbc:: ::windowsform:: ::csharp-station::   แบทไฟล์ทดสอบใน console + batch: helloworld.cs แนะนำเว็บ(web guides) + ภาษาซี (thaiall.com) + csharp-station.com * + thaicreate.com + asp101.com ** + visual studio + C# โดย ลาภลอย 
 | 
| short 1. เรียกใช้ x.dll ที่เป็น code behind ไว้ใน /bin + มีรายละเอียดเพิ่มเติม ที่เคยเขียน code ลักษณะนี้ แต่ใช้ VB ทำงานกับ web application เช่นเดียวกัน + DOS>explorer http://www.thaiabc.com/csharp/callxdll.aspx <%@ Page Language="C#" Inherits="x" %>
<script runat="server">
void Page_Load() {
  n1.Text="12";
}</script>
<body><form runat="server">
<asp:Textbox ID="n1" runat="server" />
<asp:Textbox ID="n2" runat="server" />
<asp:Button ID="b1" text="hello" runat="server" onclick="myclick" />
</form> | 
| 1. โครงสร้างโปรแกรม (Program Structure) using System;
namespace hello {
public class helloworld {
public static void main(string[] args) {
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1 {
class Program  {
static void Main(string[] args) {
 | 
| 2. หมายเหตุ (Comments) // บรรทัดเดียว /* หลายบรรทัด หลายบรรทัด หลายบรรทัด */ | ///  หมายเหตุสำหรับ XML บรรทัดเดียว /** หมายเหตุสำหรับ XML หลายบรรทัด หมายเหตุสำหรับ XML หลายบรรทัด หมายเหตุสำหรับ XML หลายบรรทัด */ | 
| 3. แบบข้อมูล (Data Types) Value Types Reference Types Initializing 
Type Information Type Conversion | 
| 4. ค่าคงที่ (Constants)
const int MAX_MONTHS = 12; const int MAX_DAY = 31; readonly float TAX = 10.31f; readonly float INTEREST = 2.5f; | 
| 5. อีนิวเมอเรชั่น (Enumerations)
enum Action {Start, Stop, Rewind, Forward}; enum Status {Flunk = 50, Pass = 70, Excel = 90}; Action a = Action.Stop; if (a != Action.Start) Console.WriteLine(a + " is " + (int) a); // Prints "Stop is 1" Console.WriteLine((int) Status.Pass); // Prints 70 Console.WriteLine(Status.Pass); // Prints Pass | 
| 6. ตัวดำเนินการ (Operators) Comparison Arithmetic Assignment Bitwise Logical String Concatenation | 
| 7. ตัวเลือก (Choices) greeting = age < 20 ? "What's up?" : "Hello"; 
if (age < 20) 
// Multiple statements must be enclosed in {} if (x > 5) x *= y; else if (x == 5) x += y; else if (x < 10) x -= y; else x /= y; // Every case must end with break or goto case switch (color) { // Must be integer or string case "pink": case "red": r++; break; case "blue": b++; break; case "green": g++; break; default: other++; break; // break necessary on default } | 
| 8. ทำซ้ำ (Loops)
Pre-test Loops: // no "until" keyword while (c < 10) c++; for (c = 2; c <= 10; c += 2) Console.WriteLine(c); Post-test Loop: 
// Breaking out of loops 
// Continue to next iteration | 
| 9. อาร์เรย์ (Arrays) int[] nums = {1, 2, 3}; float[,] twoD = new float[rows, cols]; int[][] jagged = new int[3][] { | 
| 10. ฟังก์ชัน (Functions)
// Pass by value (in, default),
reference (in/out), and reference (out) void TestFunc(int x, ref int y, out int z) { x++; y++; z = 5; } int a = 1, b = 1, c; // c doesn't need initializing // Accept variable number of arguments int total = Sum(4, 3, 2, 1); // returns 10 /* C# doesn't support optional
arguments/parameters. Just create two different versions of the same
function. */ | 
| 11. สตริงค์ (Strings) Escape sequences  // String concatenation // Chars // String literal // String comparison Console.WriteLine(mascot.Substring(2, 3)); // Prints "son" 
// String matching // My birthday: Oct 12, 1973 // Mutable string | 
| 12. การจัดการข้อยกเว้น (Exception Handling) 
// Throw an exception // Catch an exception | 
| 13. เนมสเปซ (Namespaces) namespace Harding.Compsci.Graphics { // or namespace Harding { using Harding.Compsci.Graphics; | 
| 14. คลาส / อินเทอเฟส (Classes / Interfaces) Accessibility keywords // Inheritance 
 // Extending an interface  
 | 
| 15. คอนสตักเตอร์ (Constructors / Destructors) class SuperHero { | 
| 16. การใช้วัตถุ (Using Objects) SuperHero hero = new SuperHero(); 
// No "With" construct hero.Defend("Laura Jones"); 
SuperHero hero2 = hero; // Both reference the same
object hero = null ; // Free the object if (hero == null) Object obj = new SuperHero();  using (StreamReader reader = File.OpenText("test.txt")) { string line; while ((line = reader.ReadLine()) != null) Console.WriteLine(line); } | 
| 17. โครงสร้าง (Structs)
struct StudentRecord { public string name; public float gpa; public StudentRecord(string name, float gpa) { this.name = name; this.gpa = gpa; } } StudentRecord stu = new StudentRecord("Bob", 3.5f); | 
| 18. คุณสมบัติ (Properties)
private int _size; public int Size { get { return _size; } set { if (value < 0) _size = 0; else _size = value; } } foo.Size++; | 
| 19. ดีรีเกท (Delegates / Events) delegate void MsgArrivedEventHandler(string message); event MsgArrivedEventHandler MsgArrivedEvent; // Delegates must be used with events in C# using System.Windows.Forms; Button MyButton = new Button();  private void MyButton_Click(object sender, System.EventArgs e) { | 
| 20. คอนโซลไอ/โอ (Console I/O) Console.Write("What's your name? "); 
int c = Console.Read(); // Read single char | 
| 21. แฟ้มไอ/โอ (File I/O) using System.IO; // Write out to text file // Read all lines from text file // Write out to binary file // Read from binary file | 
| hello world on another computer + helloexe_2case.rar + sharpdevelop_test1test2.rar + socket_listener_client.rar + myshutdown.rar 
public static void StartServer()  
{   
	string HostName = Dns.GetHostName();  
	IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
	Console.WriteLine(HostName);			
	IPHostEntry host;
	IPAddress ipAddress = null;
	foreach(IPAddress ip in ipaddress)  
	{  
		Console.WriteLine(ip.ToString());
		host = Dns.GetHostEntry(ip); // localhost
		ipAddress = ip;            	
	}         	
	// use only last ipaddress in server            
	IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11111);
	try {   
		Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);    
		listener.Bind(localEndPoint); 
		listener.Listen(10);  
		Console.WriteLine("Waiting for a connection...");  
		Socket handler = listener.Accept();  
	}  
	catch (Exception e)  
	{  
		Console.WriteLine(e.ToString());  
	}
	Process cmd = new Process();
	cmd.StartInfo.FileName = @"cmd.exe";
	cmd.StartInfo.Arguments = @"/K shutdown /s /t 180"; // dir  
	cmd.Start();
	cmd.WaitForExit();			    	    
	Console.WriteLine("\n to stop process by DOS> shutdown /a");  
	Console.ReadKey();  
}        	
 | 
| แนะนำเว็บ (Web Guides) + http://www.harding.edu/fmccown/vbnet_csharp_comparison.html Reference : Frank McCown, Ph.D. ( Blog ) Assistant Professor of Computer Science at Harding University. | 
| "ไม่เริ่มต้นในวันนี้ จะไม่มีทางสำเร็จในวันพรุ่ง" โดย โยฮัน ว็อล์ฟกัง ฟ็อน เกอเทอ |