Python to C# Cheat Sheet
เดติด : https://gist.github.com/DanielKoehler/606b022ec522a67a0cf3
ตัวแปร
Python:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo = 1 | |
bar = "hi" | |
something_that_varies = 2 # Though strict immutability in Python isn't common. | |
something_that_varies += 1 |
C#:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = 1; // Equivalent to: int foo | |
var bar = "hi"; // Equivalent to: String bar | |
var somethingThatVaries = 2; // Equivalent to: int somethingThatVaries | |
somethingThatVaries++; |
ฟังก์ชัน
Python:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function definition: takes an argument | |
def do_domething(some_argument): | |
return some_argument + 1 | |
# Function use | |
results = do_something(3) |
C#:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
ตัวดำเนินการเงื่อนไข (Conditionals)
Python:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if x == 3: | |
# ... | |
elif x == 0: | |
# ... | |
else: | |
# ... |
C#:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (x == 3) | |
{ | |
// ... | |
} | |
else if (x == 0) | |
{ | |
// ... | |
} | |
else | |
{ | |
// ... | |
} |
switch C# :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Or using a switch: | |
switch(x) { | |
case 3: | |
// ... | |
break; | |
case 0: | |
// ... | |
break; | |
default: | |
// ... | |
break; | |
} |
แสดงผล
Python:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = 5 | |
print("x has the value %s" % x) |
C#:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = 5; | |
Debug.Log("x has the value {0}", x); |
Lists of variable size
Python:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i = ["a", "b", "c"] | |
i.append("d") | |
print(i[1]) # outputs b |
C#:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i = new List<string>() { "a", "b", "c" }; | |
i.Add("d"); | |
Debug.Log(i[1]); // outputs b |
Python:
C#:
0 ความคิดเห็น:
แสดงความคิดเห็น