Lecture 02 - programming fundamentals
Lecture 02 - programming fundamentals
namespace consoleprogram1
{
internal class Program
{
static void Main(string[] args)
{
//Write to console
Console.WriteLine("Hello world!");
//Hold the screen
Console.ReadLine();
}
}
}
Variables and data types
A variable is nothing but a name given to a storage are that our programs can
manipulate.
Each variable in C# has a specific type, which determines the size and layout of the
variable’s memory; the range of value that can be stored within that memory; and
the set of operations that can be applied t0 the variable.
Getting Interactive Input/output
note: Remember C# is case sensitive.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace consoleprogram2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is your name :");
String name = Console.ReadLine();
//place holder{0} for the variable: can have more than one
Console.Write("Hello {0} ", name);
Console.WriteLine("Welcome to C#!!!");
//makes the program wait for a key press and it prevents the screen until a key is pressed
Console.ReadKey();
}
}
}
Program 3: Write a simple console
application to get an output as below
************
Enter name: M.D Silva
Enter Index No: HD142100
xxxxxxxxxxxxxxxxxx
Your name is: M.D Silva
Index no: HD142100
xxxxxxxxxxxxxxxxxxx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace consoleprogram3
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter name: ");
string name = Console.ReadLine();
Console.WriteLine("Enter ID no: ");
String idno = Console.ReadLine();
Console.WriteLine("*********");
Console.WriteLine("Your name: {0} ", name);
Console.WriteLine("Index no: {0} ", idno);
Console.WriteLine("*********");
Console.ReadLine();
}
}
}
Program 4: A console program to find
addition, subtraction, multiplication and
division of two variables
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace consoleprogram4
{
internal class Program
{
static void Main(string[] args)
{
int n1, n2, result; float fresult;
Console.WriteLine("Enter first number: ");
n1 = Convert.ToInt32(Console.ReadLine()); //because it thinks all inputs are string
Console.WriteLine("----Answer----");
result = n1 + n2;
Console.WriteLine("Addition of the two numbers are: {0} ", result);
result = n1 - n2;
Console.WriteLine("Substraction of the two numbers are: {0} ",
result);
result = n1 * n2;
Console.WriteLine("Multiplication of the two numbers are: {0} ",
result);
fresult = n1 / n2;
Console.WriteLine("Division of the two numbers are: {0} ",
fresult);
Console.ReadLine();
}
}
}
Write a console program to get the ODD and Even numbers from 1-50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace consoleprogram5
{
internal class Program
{
static void Main(string[] args)
{
for(int c=1; c<=50; c++)
{
int r = c % 2;
if(r == 0)
{
Console.WriteLine("{0} is an Even Number:", c);
}
else
{
Console.WriteLine("{0} is an Odd number: ", c);
}
Console.ReadKey();
}
}
}
}
Language concept
OOP’s Overview
OOP – The core ingredient of the .NET framework.
The fundamental idea behind OOP
Combine into a single unit(data & methods).
Such units are called an object.
All OOP languages provide mechanisms help to implement the object oriented
models
Encapsulation
Inheritance
Polymorphism
Reusability