Learn to Code via Tutorials on Repl.it!

← Back to all posts
C# Dash: Level One - Object Oriented Madness
DungeonMaster00 (194)

C# Dash - Object Oriented Madness

My competitor to the @HahaYes C++ Tutorials, even if i don't have lots of cycles. Anyways, sorry for another competitor. I like C#.

This tutorial will cover comments, printing stuff to console, Essential Value Types, Input, Variables, and Operators.

The reader is expected to know what Object Oriented Programming and integers are.

Some information was retrieved from the Microsoft .NET C# Documentation.

Words like this will appear in the glossary at the bottom.

More Tutorials

Scratch Tutorials


SO LET'S GET STARTED!!!!!!!!!!!!!!!!!!!


Basics

  • C# is object oriented.
  • It was made by Microsoft.
  • Every line except comments and lines with brackets end with a semicolon (;).

So this is a basic template for a C# program.

Comments

A coder's best friend is a comment. A comment is a part of the program that's not run. These enchance readability and can allow for not running stuff in a program without having to delete code. Here's the syntax:

Print stuff to console

To write print something to the console, you use Console.WriteLine();. Here is an example for Hello World!

To make the program print multiple strings to the console inside one Console.WriteLine, you can put a + in between strings. This is called string concatenation.

Additionally, you can use {}s in between around variables to print them to the console with Console.WriteLine($"Hello {variable}"); This is called string interpolation.

Escape Sequences

Escape Sequences start with a \ and can do special stuff inside a string. Here are some ones to remember:

  • \' Have a single quote without messing up the code
  • \" Have a double quote without messing up the code
  • \n Start a new line

For more about escape sequences, click here.


Essential Value Types

C# has a lot of value types. You can use these to make variables (explained after this.)

Note, string is not a value type, but is still essential.

Here are five important ones to remember:

  • string - A series of characters. The string itself is in ""s. Example: "Hello World!"
  • char - A single character. It is in ''s. Example: 'Z'
  • int - A 32-bit integer. Example: 5720132.
  • double - A floating-point value. Example: 1234.4321
  • bool - A boolean value (evaluating to true or false.) Example: false
    Click here fore more value types.

Variables

Variables are values stored under a name. When naming variables, be aware that:

  • You cannot have spaces in your variable name
  • Your variable cannot begin with a digit
  • The only other character besides alphabetical (accents are fine) and numerical characters is an underscore (_)
    To make a variable, begin with the type that you want, followed by an equal sign, then the value. Here are some examples of the five types I mentioned.

As you could see, Console.ReadLine(); is how you can have user input inside the console. Be aware that unlike Python, you cannot add text inside the (). Also, you can add it to a variable like in the example above. The string will not be saved if you don't put it in a varibale.

Operators

And finally, there's Operators. With operators, you can perform basic math (this is the part for math). Here they are:

If you want to do more complicated math than the arithmetic operators will allow, then check out the Math methods. On the left, there should be a list of the Math methods.


Level complete!


Level 2 will be about some essential Math methods and variable conversion.

EDIT: Saving conditionals for third tutorial sorry.

Coding challenge (optional):

Write a program that will have two number variables. Make one variable of the two number variables being added. Make another variable that's one of the number variables being subtracted from the other. Print the results to the console.


Glossary

  • string concatenation - The act of putting a plus in between strings in Console.WriteLine(); to combine them.
  • string interpolation - The act of putting braces around variables to print a variable to the console. In order for it to work, you have to put a $ before the strings (don't concatenate)
  • value type - A kind of type in C#. Data under value types include double and bool.
Comments
hotnewtop
DungeonMaster00 (194)

btw @nkibre and me are not related

nkibre (2)

I didn't know about string interpolation with variables. I thought only perl had that!

Highwayman (1537)

Like the initial set up!

TheForArkLD (772)

Cool tutorial.
I think, C# is best programming language.