C# for Unity 

Introduction

C# (C-Sharp) is a computer programming language evolved by Microsoft that runs on the .NET Framework. C# is used to create web apps, desktop apps, mobile apps, games, and far more.

Variables

In C#, there are different types of variables (defined with different keywords), for example:

  • int - stores integers (whole numbers), without decimals, such as 123 or -123 
  • double - stores floating-point numbers, with decimals, such as 19.99 or -19.99 
  • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes 
  • string - stores text, such as "Hello World". String values are surrounded by double quotes bool - stores values with two states: true or false

Operators

Arithmetic operations

Addition, subtraction, multiplication, division, modulus, increment, decrement

Assignment operators

It is used to assign a value to a variable. In this example shown below, an integer value 10 is assigned into the int type variable “ x ”

Comparison operators

Comparison operators are used to compare the variables. Equal to, Not equal to, Greater than, Less than, Greater than or equal to, Less than or equal to. These are the comparison operators used in C#

Logical operators

Logical AND, Logical OR, Logical NOT

C# Booleans

Many Times, in programming, you will need a data type that can have only one of the two values, like:

  • YES / NO 
  • ON / OFF 
  • TRUE / FALSE

For this case, C# has a bool data type, which can take the two values true or false.

C# If ... Else

Use if to specify a block of code that needs to be executed, if a specified condition is or becomes true 

Use else to specify a block of code to needs to be executed if the same condition is or becomes false

C# Switch

The switch expression is evaluated once in the code and The value of the expression is compared with the values of each case. If there is a match, the block associated with the case of code is executed. 

 When C# reaches a break keyword, it breaks out of the switch block and the code will stop executing.

C# For Loop

for (statement 1; statement 2; statement 3

 { 

  // code block to be executed 

 } 

Statement 1 is executed (one time) before the execution of the code block. 

 Statement 2 defines the condition for executing the code block. 

 Statement 3 is executed (every time) after the code block has been executed.

In the above example, the value of i will be printed repeatedly until the value of i become 5 and it prints from 0 to 4, but not 5.

C# While Loop

The while loop moves through a block of code as long as a specified condition is True:

In the above example, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:

C# Do/While Loop

This loop will execute the code block once, before checking if the condition is true, then it'll repeat the loop as long because the condition is true.

In the above example, the loop will always be executed a minimum of once, even though the condition is false because the code block is executed before the condition is tested.

C# Arrays

Arrays are meant to store multiple values during a single variable, rather than declaring separate variables for every value. To declare an array, declare the variable type with square brackets.

In the above example, the statement accesses the worth of the primary element in iceCream i.e Chocolate. Similarly, the 4th element in myNum i.e 40. The Length property will determine what percentage elements are there in an array

Foreach Loop and Sort in array

The above example are often read like this: for every string element (called i - as in index) in iceCream, print out the worth of i, that's from Chocolate to Butterscotch 

The Sort method sorts an array alphabetically or in an ascending order

C# Methods

A method could be a block of code that only runs when it's called. you'll be able to pass data, called parameters, into a method. Methods are used to perform certain actions, and that they also are referred to as functions.

Create a Method

A method is defined with the name of the method, followed by parentheses (). C# provides some pre-defined methods, which you already are aware of, such as Main(), but you'll also create your own methods to perform certain actions

Call a Method

To call or execute a method, write the method's name come after by two parentheses () and a semicolon. within the following example, MyMethod() is employed to print a text (the action), when it's called.

Parameters and Arguments

Information will be passed to methods as parameters. Parameters act as variables inside the method. they're specified after the method name, inside the parentheses. you'll add as many parameters as you would like, just separate them with a comma.

The following example features a method that takes a string called name as a parameter. When the method is named, we pass along a name, which is employed inside the method to print the name during a sentence. the subsequent code gives the output as:

My name is Haris 

 My name is Bennet 

 My name is Lucy

Multiple Parameters

When you are working with multiple parameters, the method call must have a similar number of arguments as there are parameters, and also the arguments must be passed within the same order.

The above example gives the output : 

 Haris is 5 

 Bennet is 8 

 Lucy is 31

Return Values

The void keyword, employed in the examples above, indicates that the method shouldn't return a value. If you wish the method to return a value, you'll be able to use a primitive data type (such as int or double) rather than void, and use the return keyword inside the method.

Classes and Objects

Everything in C# is related to classes and objects, together with its attributes and methods. For example: in real world, a car is an object. The car has attributes, like weight and color, and methods, like drive and brake

Create a Class

To create a class, use the class keyword

In the above example, class name is "Car" with a variable color

Create an Object

An object is formed from a class. we've already created the class named Car, so now we are able to use this to form objects. 

 To create an object of Car, specify the class name, followed by the object name, and use the keyword new

C# Enums

An enum is a special "class" that represents a group of constants (unchangeable/read-only variables). To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma

By default, the first item of an enum has the value 0. The second has the value 1, and so on

Tutorials: https://www.w3schools.com/cs/cs_operators.php                                         https://docs.microsoft.com/en-us/dotnet/csharp/

Ambareesh V R