Learn to Code via Tutorials on Repl.it!

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

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.

using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		//add code here
	}
}

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:

// This is a single line comment. If you start a single line comment, the code after the // on the same line will not run
/*
This is a multi-line comment
If you're not careful, this can consume the rest of the code in the file.
The only way to stop it is to type in:
*/
using System;

class insert_class_name_here {
	public static void Main(string[] args) {
		//add code here
	}
}

Print stuff to console

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

using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		Console.WriteLine("Hello World!");
                //Will output: 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.

using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		Console.WriteLine("Hello " + "World!");
                //Will output: Hello World!
                //Also, putting spaces in between the string and plus is optional
	}
}

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.

using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		string wld = "World!"; //This line was an example of a variable. They will be explained later in this tutorial
                Console.WriteLine($"Hello {wld}"); //Will output: Hello World!
	}
}

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.
using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		string yo = "yo MAMA!";
		string input = Console.ReadLine(); //YES THAT'S HOW YOU CAPTURE USER INPUT Y'ALL!!!
		char a_character = 'r';
		int integral_num = 1234;
		double floating_point_num;
		bool español_coding_languages_important = true; //for clarification, "español" transelates to "spanish".
                bool long_variable_names_useful = false;
	}
}

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:

//Arithmetic

+ //Addition
- //Subtraction
* //Multiplication
/ //Division
% //Remainder

//Equality and Comparison

< //Less than
<= //Less than or equal to
> //Greater than
>= //Greater than or equal to
== //Equal to
!= //Not equal to

//Boolean Logic

&& //Conditional AND (result is false if one thing being compared is false)
|| //Conditional OR (result is true if one thing being compred is true.
! //Negation (Place this before true, false, or a bool variable being compared. If it is placed before something with true, then it becomes false. If it is placed before something false, it becomes true.)

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 (190)

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 (1499)

Like the initial set up!

TheForArkLD (770)

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