{"id":26,"date":"2023-02-09T21:23:38","date_gmt":"2023-02-09T21:23:38","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/ingraang\/?p=26"},"modified":"2023-02-09T21:23:38","modified_gmt":"2023-02-09T21:23:38","slug":"learning-c-as-a-python-programmer","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/ingraang\/2023\/02\/09\/learning-c-as-a-python-programmer\/","title":{"rendered":"Learning C# as a python programmer"},"content":{"rendered":"\n<p>Learning a new programming language can be difficult at first. While the basic concept of object oriented programming is the same across languages, the syntax can be quite different. This post will go over the basic differences between python and C#, focusing on basic syntax and typing.  <\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"has-text-align-center wp-block-heading\">Comments<\/h5>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Python:<\/strong><br>single line comments start with #.<br>multi-line comments start with &#8221;&#8217; and end with &#8221;&#8217; on their own lines<br><br><code># this is a comment<\/code><br><code>'''<\/code><br><code>this is a<br>multi-line comment<br>'''<\/code><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>C#:<\/strong><br>single line comments start with \/\/.<br>multi-line comments start with \/* and end with *\/<br><br><code>\/\/ this is a comment<\/code><br><code>\/* <br>this is a multi-line comment<br>*\/<\/code><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"has-text-align-center wp-block-heading\">Ending statements<\/h5>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Python:<\/strong><br>statements end with a new line. statements can only wrap into the next line if there is an open parentheses or bracket.<br><br><code>print(\"this is long but it's okay <br>&nbsp;&nbsp;&nbsp;because of the parentheses\")<\/code><br>   <\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>C#:<\/strong><br>statements end with a semi-colon. any statement can be wrapped into multiple lines.<br><br><br><code>Console.WriteLine(\"this is long <br>&nbsp;&nbsp;&nbsp;but the statement won't end<br>&nbsp;&nbsp;&nbsp;until the semi-colon);<\/code><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"has-text-align-center wp-block-heading\">Separating blocks of code<\/h5>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Python:<br><\/strong>uses indentation to separate blocks of code.<br><br><br><br><code>if a &lt; b<br>&nbsp;&nbsp;&nbsp;foo(a)<br>else<br>&nbsp;&nbsp;&nbsp;foo(b)<br>&nbsp;&nbsp;&nbsp;bar(b)<\/code><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>C#:<\/strong><br>uses {} to separate blocks of code. braces can be omitted if the block is one line, but if\/else statements should always match in their use of braces<br><br><code>if (a &lt; b) {<br>&nbsp;&nbsp;&nbsp;foo(a);<br>else {<br>&nbsp;&nbsp;&nbsp;foo(b);<br>&nbsp;&nbsp;&nbsp;bar(b);<br>}<\/code><br><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"has-text-align-center wp-block-heading\">variable types<\/h5>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Python:<\/strong><br>types do not have to be explicitly declared. python will automatically convert types in some situations<br><br><br><br><code>x = 1<br>y = \"hello\"<\/code><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>C#:<\/strong><br>types must be explicitly declared. If you initialize the variable in the same line as declaration, you can use the &#8220;var&#8221; keyword to have the compiler determine the type.<br><br><code>int x;<br>var y = \"hello\"<\/code><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"has-text-align-center wp-block-heading\">List types<\/h5>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Python:<\/strong><br>can create a list or array without declaring the type. can have lists or arrays of multiple types<br><br><br><code>myList = [1, \"two\", 3.5]<\/code><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>C#:<\/strong><br>must declare the type of variable in a list or array. cannot have lists or arrays of multiple types (unless they are of classes derived from the same parent class)<br><br><code>var myList = new List&lt;int&gt;();<\/code><br><code>myList.Add(1);<\/code><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"has-text-align-center wp-block-heading\">Comparing<\/h5>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Python:<\/strong><br>use = to compare values in an if.<br><br><br><code>if a = b<\/code><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>C#:<\/strong><br>use == to compare values in an if. surround values being compared by ()<br><br><code>if (a == b)<\/code><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"has-text-align-center wp-block-heading\">Looping<\/h5>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Python:<\/strong><br>loop using <br><br><code>for i in range(0,10)<\/code><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>C#:<\/strong><br>loop using <br><br><code>for (int i = 0; i &lt; 10; i++)<\/code><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"has-text-align-center wp-block-heading\">iterating<\/h5>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Python:<\/strong><br>iterate using <br><br><code>for item in myList<\/code><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>C#:<\/strong><br>iterate using <br><br><code>foreach (var item in myList)<\/code><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learning a new programming language can be difficult at first. While the basic concept of object oriented programming is the same across languages, the syntax can be quite different. This post will go over the basic differences between python and C#, focusing on basic syntax and typing. Comments Python:single line comments start with #.multi-line comments [&hellip;]<\/p>\n","protected":false},"author":13119,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/users\/13119"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":4,"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/posts\/26\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ingraang\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}