Skip to content

Instantly share code, notes, and snippets.

@DanielKoehler
Forked from carols10cents/c#-to-rust.md
Last active August 27, 2024 05:03
Show Gist options
  • Save DanielKoehler/606b022ec522a67a0cf3 to your computer and use it in GitHub Desktop.
Save DanielKoehler/606b022ec522a67a0cf3 to your computer and use it in GitHub Desktop.
Python to C# Cheat Sheet

Python to C# Cheat Sheet

Variables

Python:

foo = 1
bar = "hi"
something_that_varies = 2 # Though strict immutability in Python isn't common.
something_that_varies += 1

C#:

var foo = 1; // Equivalent to: int foo
var bar = "hi"; // Equivalent to: String bar
var somethingThatVaries = 2; // Equivalent to: int somethingThatVaries
somethingThatVaries++;

Functions

Python:

# Function definition: takes an argument
def do_domething(some_argument):
  return some_argument + 1


# Function use
results = do_something(3)

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);

Conditionals

Python:

if x == 3:
    # ...
elif x == 0:
    # ...
else:
    # ...

C#:

if (x == 3)
{
  // ...
}
else if (x == 0)
{
  // ...
}
else
{
  // ...
}
// Or using a switch:
switch(x) {
  case 3:
    // ...
    break;
  case 0:
    // ...
    break;
  default:
    // ...
    break;
}

Output to the screen

Python:

x = 5
print("x has the value %s" % x);

C#:

var x = 5;
Debug.Log("x has the value {0}", x);

Lists of variable size

Python:

i = ["a", "b", "c"];
i.append("d"); 
print(i[1]); # outputs b

C#:

var i = new List<String>() { "a", "b", "c" };
i.Add("d");
Debug.Log(i[1]); // outputs b

Iterating over the elements in a list

Rust:

i = ["a", "b", "c"];
for j in i:
    print(j):

C#:

var i = new List<String>() { "a", "b", "c" };

foreach(var j in i) {
  Debug.Log(j);
}

i.ForEach((j) => Debug.Log(j));
Copy link

ghost commented Oct 21, 2015

Thanks a lot. Also,

@Pnudd
Copy link

Pnudd commented Jan 5, 2021

Yes! Thank you

@ntokozo9
Copy link

ntokozo9 commented Mar 3, 2022

//C# for solving inverse interpolation
using System;
class GFG

{

// Consider a structure to keep

// each pair of x and y together

class Data

{

public double x, y;

public Data(double x, double y)

{

this.x = x;

this.y = y;

}

};

// Function to calculate the

// inverse interpolation

static double inv_interpolate(Data []d,

int n, double y)

{

// Initialize readonly x

double x = 0;

int i, j;

for (i = 0; i < n; i++)

{

// Calculate each term

// of the given formula

double xi = d[i].x;

for (j = 0; j < n; j++)

{

if (j != i)

{

xi = xi * (y - d[j].y) /

(d[i].y - d[j].y);

}

}

// Add term to readonly result

x += xi;

}

return x;

}

// Driver Code

public static void Main(string[] args)

{

// Sample dataset of 4 points

// Here we find the value

// of x when y = 4.5

Data []d = {new Data(1.27, 2.3),

new Data(2.25, 2.95),

new Data(2.5, 3.5),

new Data(3.6, 5.1)};

// Size of dataset

int n = 4;

// Sample y value

double y = 4.5;

// Using the Inverse Interpolation

// function to find the

// value of x when y = 4.5

Console.Write("Value of x at y = 4.5 : {0:f5}");
Console.Write(inv_interpolate(d, n, y));

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment