Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Using keyword, Comments, + operator

Notes:

I. Understanding "using" keyword: [Starts At = 00min:00sec]
II. Understanding comments in C#: [Starts At = 05min:12sec]
III. The + as Addition and Concatenation operator in C#: [Starts At = 16min:52sec]

W.K.T
Unity Engine + .NET Framework = Project (Game, Simulation etc.)

Engine or Framework:
Is collection of dll files for creating applications
where each DLL is meant for specific purpose
Ex: Unity Engine , .NET framework

DLL:
Is collection of namespaces
Ex: mscorlib.dll, UnityEngine.dll

Namespace:
Is collection of other namespaces and datastructures
I.e. (Classes, structures, events etc.)

I. Understanding "using" keyword: [Starts At = 00min:00sec]
To use any specific datastructure we must use its namespace.
Hence:
To use MonoBehaviour and Debug classes, we must use UnityEngine namespace
To use Stack, Queue, HashTable classes, we must use System.Collections namespace

Syntax:
using NamespaceName;

Ex:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

II. Understanding comments in C#: [Starts At = 05min:12sec]
Comments are ignored by the compiler
Comments are writen by us programmers
Comments have no effect on the execution of a program
Comments are used to increase the readability and understand ability of the source code
Comments are used to document the source code
Comments are used to explain code logic, so that other developers can understand the code easily
Comments are also used to desable some code logic

Types of comments in C#: There are 3 types of comments in C#

1. Single line comment: (// comment goes here)
after // anything written in a single line is treated as a comment
Is used to comment a single line

2. Multiline comment: (/* comment goes here */)
Is used to comment multiple lines
anything written in between /* and */ is treated as a comment

3. Documentation comment: (/// uses XML syntax)
Is used to document methods
Meant to provide extra information to the user about the methods

Example Code: Program to demonstrate comments in C#

using UnityEngine;
public class FirstScript : MonoBehaviour {
void Start ()
{
// Single line comment //Debug.Log ("Hello Unity");

/*
Multiple
line
comment
*/
/*
Debug.Log ("Hello Unity");
Debug.Log ("Hello Unity");
Debug.Log ("Hello Unity");
*/

// documentation comment uses xml
int sum = add (2, 3, 4);
}

/// <summary>
/// Add the specified x, y and z coordinates.
/// </summary>
/// <param name="x">Give x coordinate.</param>
/// <param name="y">Give y coordinate.</param>
/// <param name="z">Give z coordinate.</param>
int add(int x,int y,int z)
{
return x + y + z;
}
}

III. The + as Addition and Concatenation operator in C#: [Starts At = 16min:52sec]
C# plus opeator is an overloaded operator
It behaves differently in different situations
Overloaded means one element doing more than one operations

i.e.
if LHS and RHS operands are both numbers then + operator works like addition operator
num + num = num // addition operator

if LHS or RHS any one or both string(s) then + operator works like concatenation operator
string + string = string // concatenation means appending
string + num = string // concatenation operator
num + string = string // concatenation operator

Associativity: Determines direction of evaluation
+ operator has left to right associativity
i.e. if you find more than one + operator in an expression evaluate them from left to right

Precedence: Determines which operator should be evaluated first before the other
() have higher precedence than + operator
+ has higher precedence than = opeator

string + num + num = string
string + (num + num) = string + (num) = string

Example Code: Program to demonstrate + operator in C#

using UnityEngine;
public class FirstScript : MonoBehaviour {
void Start ()
{
Debug.Log ("Hello" + " Unity!"); // Hello Unity!
Debug.Log("22" + "22"); // 2222

Debug.Log("Num=" + 22); // Num=22
Debug.Log("22" + 22); // 2222

Debug.Log(22 + " is Num"); // 22 is Num
Debug.Log(22+"22"); // 2222

Debug.Log (22 + 22); // 44

Debug.Log("Sum= " + (22 + 22) + " value"); // Sum= 44 value

int a = 10;
int b = 10;
int sum = a + b;
// Sum of 10 and 10 is= 20
Debug.Log ("Sum of " + a + " and " + b + " is= " + sum);
}
}