Easy-to-understand C # notes for pythonist (only basic ones)

variable

# int
num = 10
# str
name = 'nobjas'
# float
rate = 10.5
# bool
is_people = True
int num = 10;
string name = "nobjas";
float rate = 10.4f;
double rate2 = 10.5d;
bool isPeople = true;

list

int_list = list(1,2,3,4,5)
int_list.append(6)
List<int> intList = new List<int>() {1,2,3,4,5};
intList.Add(6);

value in list

# return True
if 2 in int_list:
    return True
else:
    return False
// return true
if (intList.Contains(2)) {
    return true;
} else {
    return false;
}

map

filterd_list = [x + 1 for x in intList]
using System.Linq;
intList = intList.Select(x => x+1);

However, Linq may still have restrictions, so it may be safer to write as follows

List<int> filteredList = new List<int>();
foreach (var val in intList) {
    filteredList.Add(val + 1);
}

dict

str_int = dict(
    hoge = 10,
    huga = 20,
    )
str_int['moge'] = 30;
str_int['hoge'] += 10;
Dictionary<string, int> strInt = new Dictionary<string, int>() {
    {"hoge",10}, 
    {"huga", 20},
};
strInt.Add("moge", 30);
strInt["hoge"] += 10;

if

name = "nobjas"
if name == "noble jasper":
    print "full name"
elif name == "nobjas":
    print "short name"
else:
    print "unknown"
using System;
string name = "nobjas";
if (name == "noble jasper") {
    Console.WriteLine("full name");
} else if (name == "nobjas") {
    Console.WriteLine("short name");
} else {
    Console.WriteLine("unknown");
}

loop

sample_list = range(10);
for n in sample_list:
    print n
using System;
using System.Linq;
IEnumerable<int> sampleList = Enumerable.Range(0, 10);
foreach (int n in sampleList) {
    Console.WriteLine(n);
}

Recommended Posts

Easy-to-understand C # notes for pythonist (only basic ones)
Basic syntax notes for shell script
Theano's basic notes
python C ++ notes