วันพฤหัสบดีที่ 9 กุมภาพันธ์ พ.ศ. 2560

จาก Python สู่ C#

สวัสดีครับ หลังจากที่ผมได้เขียน Python มาได้ค่อนข้างจะยาวนาน ผมเริ่มสนใจภาษาอื่น ๆ ที่นอกจาก Python หนึ่งในนั้นคือ C# และผมได้ลองฝึกเขียน C# บน .NET Core ผมพบว่าผมรู้จักถูกใจกับภาษานี้ ผมจึงเขียนบล็อกขึ้นมา เพื่อจดบันทึกการเรียนรู้ภาษา C# และภาษาอื่น ๆ บน .NET Core ครับ

Python to C# Cheat Sheet


เดติด : https://gist.github.com/DanielKoehler/606b022ec522a67a0cf3 
ตัวแปร

Python:
foo = 1
bar = "hi"
something_that_varies = 2 # Though strict immutability in Python isn't common.
something_that_varies += 1
view raw 1.py hosted with ❤ by GitHub

C#:
var foo = 1; // Equivalent to: int foo
var bar = "hi"; // Equivalent to: String bar
var somethingThatVaries = 2; // Equivalent to: int somethingThatVaries
somethingThatVaries++;
view raw 1.cpp hosted with ❤ by GitHub

ฟังก์ชัน
 Python:
# Function definition: takes an argument
def do_domething(some_argument):
return some_argument + 1
# Function use
results = do_something(3)
view raw 2.py hosted with ❤ by GitHub

C#:
// Function definition: takes an integer argument, returns an integer
static int DoSomething(int some_argument)
{
return some_argument + 1;
}
// Function use
var results = DoSomething(3);
view raw 2.cpp hosted with ❤ by GitHub

ตัวดำเนินการเงื่อนไข (Conditionals)
Python:
if x == 3:
# ...
elif x == 0:
# ...
else:
# ...
view raw 3.py hosted with ❤ by GitHub

C#:
if (x == 3)
{
// ...
}
else if (x == 0)
{
// ...
}
else
{
// ...
}
view raw 3.cpp hosted with ❤ by GitHub

switch C# :
// Or using a switch:
switch(x) {
case 3:
// ...
break;
case 0:
// ...
break;
default:
// ...
break;
}
view raw 3_1.cpp hosted with ❤ by GitHub

แสดงผล
Python:
x = 5
print("x has the value %s" % x)
view raw 4.py hosted with ❤ by GitHub

C#:
var x = 5;
Debug.Log("x has the value {0}", x);
view raw 4.cpp hosted with ❤ by GitHub

Lists of variable size
Python:
i = ["a", "b", "c"]
i.append("d")
print(i[1]) # outputs b
view raw 5.py hosted with ❤ by GitHub

C#:
var i = new List<string>() { "a", "b", "c" };
i.Add("d");
Debug.Log(i[1]); // outputs b
view raw 5.cpp hosted with ❤ by GitHub
Iterating over the elements in a list
Python:

C#:

0 ความคิดเห็น:

แสดงความคิดเห็น