Skip to content

Files

Latest commit

 

History

History
34 lines (32 loc) · 4.13 KB

cheat-sheet.md

File metadata and controls

34 lines (32 loc) · 4.13 KB

C# quick start for Pythonistas

python c# notes
print "hello world" Console.WriteLine("hello world");
import json using Newtonsoft.Json;
myname = 'Tyson' string myName = "Tyson";
-or-
var myName = "Tyson"; (implicit type)
def do_thing():
  # do that thing
void doThing()
{
  //do that thing
}
"void" is the return type
for "returns nothing"
- if the function returned a
string, it would be
string doThing()
# this is a comment // this is a single-line comment
for i in range(0,10):
  print i
for(int i=0; i<10; i++)
{
  Console.WriteLine(i.ToString());
}
for dog in dogs:
  pet(dog)
foreach(Dog dog in dogs) {
  Pet(dog)
}
if test1:
  #do thing
elif test2:
  #do thing2
else test3:
  #do thing3
if(test1) {
  //do thing
} else if (test2) {
  //do thing2
} else {
  //do thing3
}
[dog for dog in dogs if dog.weight > 100] dogs.Where(dog => dog.Weight > 100)
instance = MyCustomClass() MyCustomClass instance = new MyCustomClass(); (var works too)

Big Differences:

Whitespace don't matta!

C# is basically whitespace agnostic - format your lines and indentation however you damn well please. Instead, you'll see more {} brackets to enclose blocks and ; to end statements.

Strong typing

C# is a statically typed language, whereas python is dynamically typed. This means a few things:

  • types are checked at compile-time, rather than at run-time
  • variables can be declared without being assigned a value
  • variables cannot change type, so, whereas python will let you do this:
    myvar = 'a kitten'
    myvar = 45
    myvar = {'bip': 'bop'}
    C# (generally) will not.
  • this also means you have explicitly convert objects from one type to another, often via casting or explicit methods (like .ToString())

Compiled

As a somewhat innacurate generalization, Python is an interpreted language whereas C# is a compiled language. (in reality it is more complicated than this and both languages are kind of both but ¯\_(ツ)_/¯ ) For our purposes we can just say that in order to run c#, you must always "Build" it into a .dll or .exe.

Collections

There are some differences between the way python + C# handle collections. Python lists look a lot like C# arrays, but behave like C# lists. C# arrays are fixed-size, so you can't initialize them as [] and then add items to them. For variable sized collections, you'll need C# Lists (System.Collections.Generic.List<T>) which introduces another new topic - "Generics". Both lists and arrays in C# must know their type, and will not accept objects of a different type. Some general remarks:

  • List<T> is slower than an array but much more flexible - usually it will be fine for your purposes.
  • list + array access look just like python: MyList[5] will retun the 6th element in the collection.
  • Setting up a list can look a little funky if you're used to Python:
    List<string> namesOfStuff = new List<string>();
    initializes an empty list. You can also populate a list at initialization, like so:
    var namesOfStuff = new List<string>{"Rock", "Plant", "Sofa", "Cheeky Neon", "Fruit Water"}; (note the {} instead of ())

Object-Orientation

Python lets you be as Object-oriented as you want - but it is possible to write python code that avoids object-orientation almost entirely. C# is much more opinionated about object-orientation, and has a lot of language features/architecture organized around OO practices. Python is much more flexible and free - it does less implicit hiding and protecting of class members, it's much easier to alter the behavior of code from modules borrowed from elsewhere. C# can be quite strict about what you're allowed to do, and good C# involves a lot of "encapsulation" which can feel foreign if you're used to python.