C# Console

                 Get Building

 


Lecture 1 [  – Basics of C#     –  Purpose of Main method ]


Basic code of C#    :  There are two methods for running C#

  •  writing   “using System” on top .
  • writing “using System” code  in line .

 writing   “using System” on top :

using System;
class Program
{
static void Main()
{
Console.WriteLine(“welcome to Inside observer Tutorials”);
}
}

Capture

 


Writing  “using System in line”


class Program {   

static void Main()
{
System.Console.WriteLine(“now system.Console is running “);
}
}

Capture1


Compiler Error : [using System]


suppose if you write the code without adding  “using System” file , so your code can’t run. because of not adding that file , By  [using System]  file  the Console code will apply.

class Program
{
static void Main()
{
.Console.WriteLine(“now system.Console is running “);
}
}

erroe


using System;

class Program
{
static void Main1()
{
Console.WriteLine(“welcome to Inside observer Tutorials”);
}
static void Main()
{
Console.WriteLine(“i am Muhammad Ahsan”);
}

// Note :               Main method is the entry point into your application , so here main methord is the 2nd part of code .
}

Capture3


Calling functions & Reference  


Suppose if you want two outputs…. line(“1”) and line(“2″) , but you want to show 2nd line on top of the  1st line , and after  2nd line , the 1st one print in you output , so you need  to call 1st one    in the 2nd line code , then the procedure of that calling method will be like this . By using ”  Main1()   ” calling  function .

using System;

class Program
{
static void Main1()
{
Console.WriteLine(“welcome to Inside observer Tutorials”);
}
static void Main()
{
Console.WriteLine(“i am Muhammad Ahsan”);
Main1();
}

// Note : Main method is the entry point into your application , so here main method is the 2nd part of code .
}

Capture4


Lecture 2 [  – Basics of C#     -Reading and writing to  console ]


 

First application print the line like : “Enter your name” .  when you write your name or string (collection of alphabets ) , the output will be like “HELLO _____” .

using System;
class Program
{

static void Main()

{
Console.WriteLine(“Please enter your name :”);
string UserName=Console.ReadLine();

Console.Write(” \n   Hello _____   \n”);
}
}

Capture5

using System;
class Program
{

static void Main()

{
Console.WriteLine(“Please enter your name :”);
string UserName=Console.ReadLine();

Console.Write(” \n\n Hello ” + UserName + “\n\n\n\n\n”);
}
}

Capture6

 

 

2 ways to write console application : 

  • concatenation   —-  To concatenate string variables, you can use the + or                                                   += operators, or the String.
  • Placeholder Syntax  —-

using System;
class Program
{

static void Main()

{
Console.WriteLine(“Please enter your name :”);
string UserName=Console.ReadLine();

Console.WriteLine(“Hello {0}”, UserName );
}
}

Capture7

using System;
class Program
{

static void Main()

{
Console.WriteLine(“Please enter your name :”);
string UserName=Console.ReadLine();

Console.WriteLine(” Hello {0} {0} “, UserName);
}
}

Capture8

 

using System;
class Program
{

static void Main()

{
Console.WriteLine(“Please enter your name :”);
string UserName=Console.ReadLine();

Console.WriteLine(” Hello {0} “, UserName,UserName);
}
}

Capture9

Part 3 – C# Tutorial – Built – in types.

 


Lecture 3 [  – Basics of C#     -Data types and there values ]


using System;
class Program
{

static void Main()

{
int a= 0;

Console.WriteLine(“Minimum value of integer data type = {0}”, int.MinValue);

Console.WriteLine(“Maximum value of integer data type = {0}”, int.MaxValue);

}
}

Capture10

this is a chart of datatype , each datatype has a value

Capture11

using System;
class program
{
static void Main()
{
double a = 1233409.49373011203838378474;
Console.WriteLine(“Your value is = {0}”, a);
}
}

Capture12


Lecture 4 [  – Basics of C#     -Strings ]


using System;

class Program
{
static void Main()
{
string Name = “\”ahsan is admin\””;
Console.WriteLine(Name);
}
}

Capture13

ESCAPE SEQUENCES IN C#

using System;

class Program
{
static void Main()
{
string Name = “c:\ahasn\program\inside observer training\n\n\n\n”;
Console.WriteLine(Name);
}
}

Capture14

verbatim in C#:  in exactly the same words as were used originally.

A verbatim string is one that does not need to be escaped, like a filename:

string myFileName = "C:\\myfolder\\myfile.txt"; 

Capture16

string myFileName = @"C:\myfolder\myfile.txt";

The @ symbol means to read that string literally, and don’t interpret control characters otherwise.

Capture17

using System;

class Program
{
static void Main()
{
string Name = @”c:\ahasn\program\inside observer\C# training \n”;
Console.WriteLine(Name);
}
}Capture15

String literals

C-sharp supports two forms of string literals:

  • Regular string literals
  • Verbatim string literals.

    A  regular string  literal consists of zero or more characters  in double quotes,like welcome ahsan ", and  include  (like \n \t for next line , tabbing) etc.

    Capture20

    A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"ahsan\n Umer".

    example :  @”hello \n Umer”; // hello \n”;

    using System;

    class Program
    {
    static void Main()
    {
    string Name = @”C:\\myfolder\\myfile.txt”;
    string a = “hello \n Umer”; // hello \n umer
    string b = @”hello \n Daniel”; // hello \n daniel
    string c = “hello \t James”; // hello \t james
    string d = @”hello \t ahasn”; // hello \t ahasn
    Console.WriteLine(Name);
    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.WriteLine(c);
    Console.WriteLine(d);
    }
    }

Capture19

 


Lecture 5 [  – Common operators in C sharp ]


  • Assignment operator

* Arithmetic operator                                        + ,- ,* ,/ , %
* Comparison operator                         == ,!, =!, >, <, <=, >=
* Conditional operator                                            && , ||
* Ternary operator                                                          ?:
* Null coalescing operator                                        ??

* Arithmetic operator            

using System;

class Program
{
static void Main()
{
int Num1 = 10 ;
int Num2 = 2 ;

int Result = Num1 / Num2 ;

Console.WriteLine(” Num1=100 \n Num2=50 \n The result of Num1 / Num2 is = {0}”, Result) ;

}
}

Capture22

Comparison Operator:

using System;

class Program
{
static void Main()
{
int a = 10;
if(a==10)
{
Console.WriteLine(“The value of a is equal to 10 “);
}
}
}

Capture23

Conditional operator

using System;

class program
{
static void Main()
{
int Num1 = 10;
int Num2 = 20;

if (Num1 == 10 && Num2 == 20)
{

Console.WriteLine(“Hello Ahsan , your && operators is working”);
}
}

}Capture24

using System;
class Program
{
static void Main()
{
int Number = 10;
bool IsNumber10;

if (Number == 10)
{
IsNumber10 = true;

}
else
{
IsNumber10 = false;
}

Console.WriteLine(“Number == 10 is {0}” ,IsNumber10);
}
}

Capture25

using System;
class Program
{
static void Main()
{
int Number = 15;
bool IsNumber10;

if (Number == 10)
{
IsNumber10 = true;

}
else
{
IsNumber10 = false;
}

Console.WriteLine(“Number == 10 is {0}” ,IsNumber10);
}
}Capture26

using System;
class Program
{
static void Main()
{
int Number = 100;

bool IsNumber50 = Number == 10 ? true : false ;

Console.WriteLine(“Number == 10 is {0} ” ,IsNumber50);
}
}

Capture27

 


Lecture 6 [  – Null able type , Null coalescing in C sharp ]


using System;
class Program
{
private static bool AreYouAdmin;
static void Main()
{
bool? AreYouadmin = true;
if (AreYouadmin == true)
{
Console.WriteLine(“user is Admin”);
Console.WriteLine(” Welcome Ahsan”);
}
else if (AreYouadmin == false)
{
Console.WriteLine(“User is not Admin”);
Console.WriteLine(” Refuse Permission”);

}
else
{
Console.WriteLine(“user didn’t answer the question : “);
Console.WriteLine(” user is Robot”);

}
}
}Capture28

using System;
class Program
{
static void Main()
{
int? TicketsOnsale = 1;
int AvailableTickets;

if (TicketsOnsale == null)
{
AvailableTickets = 0;

}

else {
AvailableTickets = (int) TicketsOnsale;
}

Console.WriteLine(“AvailableTickets = {0}” , AvailableTickets);
}
}Capture29

using System;
class Program
{
static void Main()
{
int? TicketsOnsale = null;
int AvailableTickets;

if (TicketsOnsale == null)
{
AvailableTickets = 0;

}

else {
AvailableTickets = (int) TicketsOnsale;
}

Console.WriteLine(“AvailableTickets = {0}” , AvailableTickets);
}
}Capture30

 


Lecture 7 [  –  Basics of C#     – Data Types conversion in  C sharp ]


Implicit Conversions  :  when there is no loss of information if the conversion is done.

An implicit conversion does not require any special syntax in the source code. In the following example,  implicitly converts the integer value of [i] to a single-precision floating-point value before assigning it to [f].

 

using System;

class Program
{
static void Main()

{
int i = 100 ;
float f = i;

Console.WriteLine(f);

}
}

Capture31

 

using System;
class Program
{
static void Main()
{
int i = 1300;
float f = i ;
Console.WriteLine(f);
}
}

 

 

Explicit Conversions  :

Explicit conversion is required by some compilers to support narrowing conversions. It is a language-specific way to perform conversion. In some languages, like C# and C++, explicit conversion is performed using casting. Casting occurs when you prefix a conversion with a data type that defines the type of the conversion you want to perform.

 

using System;
class Program
{
static void Main()
{
float f = 123.45678F;
int i =(int)f ;
Console.WriteLine(i);
}
}

Capture32

 

using System;
class Program
{
static void Main()
{
float f = 123.45678F;
int i = Convert.ToInt32(f) ;
Console.WriteLine(i);
}
}

Capture33

 

/*Explicit Example :

using System;
class Program
{
static void Main()
{
float f = 100.24F;
int i = (int)f;
Console.WriteLine(i);
}
}
/* implecit Example:
using System;
class Program
{
static void Main()
{
int i = 1300;
float f = i ;
Console.WriteLine(f);
}
}
*/

 

 

Difference between Parse and TryParse :

if the number is a string format you have 2 options :
-Parse()
-Tryparse()

Parse() method  throws an exception if iterator cannot parse the value ,
whereas TryParse() returns a bool indicating whereas iterator succeeded or failed .

Use parse() if you are sure the value will be valid , otherwise use TryParse().

//PARSE Example

using System;
class Program
{
static void Main()
{
string strNumber = “1200”;
int i = int.Parse(strNumber);
Console.WriteLine(i);
}
}

Capture35

using System;
class Program
{
static void Main()
{
string strNumber = “987”;
int Result = 0 ;
bool IsConversionSuccessful =int.TryParse(strNumber,out Result);
if (IsConversionSuccessful)
{
Console.WriteLine(Result);
Console.WriteLine(” valid value”);
}
else
{
Console.WriteLine(” Invaid value : “);
}
}
}

Capture36]

using System;
class Program
{
static void Main()
{
string strNumber = “987abc”;
int Result = 0 ;
bool IsConversionSuccessful =int.TryParse(strNumber,out Result);
if (IsConversionSuccessful)
{
Console.WriteLine(Result);
Console.WriteLine(” valid value”);
}
else
{
Console.WriteLine(” Invaid value : “);
}
}
}

Capture37

 


Lecture 8 [  –  Basics of C#     – Array  in  C sharp ]


 

using System;

class program
{
static void Main()
{
int[] EvenNumbers= new int[3];

EvenNumbers[0] = 0;
EvenNumbers[1] = 2;
EvenNumbers[2] = 4;

Console.WriteLine(EvenNumbers[1]);

}

}

Capture41

 

LOGIC – syntax ERROR 

using System;
class program
{
static void Main()
{
int[] EvenNumbers= new int[3];

EvenNumbers[0] = 0;
EvenNumbers[1] = 2;
EvenNumbers[2] = 4;
EvenNumbers[3] = 6;
Console.WriteLine(EvenNumbers[1]);

}

}

Capture42

 


Lecture 9 [  –  Basics of C#     – Comments in  C sharp ]


  • Single line comment
  • Multi  line comment

using System;
class Program
{
static void Main()
{
Console.Writeline(“Hello I am Free 🙂 “);
//Console.WriteLine(“me in Comment 😦 “);
}
}
using System;
class Program
{
static void Main()
{
/*         Console.Writeline(“me in Comment 😦 “);
Console.WriteLine(“me in Comment 😦 “);          */
}
}

Capture43

 


Lecture 10 [  –  Basics of C#     – If Statements in  C sharp ]


 

using System;
class Program
{
static void Main()
{
Console.WriteLine(“Enter Branch Pin Coder”);

int pincode = int.Parse(Console.ReadLine());

if (pincode == 1)
{
Console.WriteLine(“welcome Ahasn”);
}

else if (pincode == 2)
{
Console.WriteLine(“welcome Umer”);
}
else if (pincode != 1 && pincode != 2)
{
Console.WriteLine(“Invalid”);
Console.WriteLine(“You have not permission to access the machine”);
}

Capture44

 

using System;
class Program
{
static void Main()
{
Console.WriteLine(“Enter favr8 number : “);

int fav = int.Parse(Console.ReadLine());
if (fav == 10 || fav == 20)
{
Console.WriteLine(“no is 10 or 20”);

}

else
{
Console.WriteLine(“Invalid”);

}

}
}Capture45

 

Lecture 12 [  –  Basics of C#     – Switch  Statements in  C sharp ]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

TIME CODE in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Create_a_Basic_Console_App_in_C_sharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“plase enter your name :”);
string userName = Console.ReadLine();

Console.WriteLine(“hello , {0} \nthe current time is {1} \n\n\n”, userName, System.DateTime.Now.TimeOfDay);
Console.WriteLine(“press any key to quit .\n\n\n”);
Console.ReadLine();
}
}
}

abcd

Advertisements