Вы находитесь на странице: 1из 8

C#

is a OOP Language
OOP
compose of 2 types
Class
-Blueprint or prototype from which objects are instantiated
-a user define datatype that specific how objects of its type ar
e represented
Bjarne Stroustrup - developer of C++
Dennis m. Ritchie - developer of C
C is the nearest communication from human to camputer
Objects
an instance of a class
Compiler
a program that converts human readable language to a machine readable la
nguage.
Example;;;;;
using System; 'system is a namespace
using System.Text;
'
namespace SampleConsole{
//namespace declaraction, any na
me must be camellcasing
class Program {
//Declaration
static void Main (string[]args){
//start of class
definition
//(string[]args) is a parameter
//args is daclarable
//void -- no return value
//static - meaning there should
be only one
Console.WriteLine("I Am a C# Programmer. ");
//Consol
e.WriteLine is a class.method
//ended with sem
icolon, it is a statement
Console.readLine();
}
}
}
'csc.exe is the compiler of C#
EXAMPLE
C:\Users\Jorinz>D:
D:\>
D:\>cd cs
D:\CS>csc /target:exe Program.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.4927
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

D:\CS>Program
I Am a C# Programmer.
EXAMPLE WITH ERROR
D:\CS>csc /target:exe Program.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.4927
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.
Program.cs(7,47): error CS1002: ; expected
D:\CS>
6.30.2016
Set CSC at CMD:
SET PATH="C:\Windows\Microsoft.NET\Framework64\v2.0.50727
compile the program
CSC /target:exe Program.cs
Run The program
Program.exe
Sample READLINE
using System;
using System.Text;
namespace SampleConsole{
class Program {
static void Main (string[]args){
string name;
Console.WriteLine("What's Your Name");
name = Console.ReadLine();
Console.WriteLine("Hello {0}", name); //1st method (Plac
e Holder Method)
Console.WriteLine("Hello" +name); //2nd method
}
}
}
OUTPUT
What's Your Name
jorinz
Hello jorinz
Primitive Data Types in C#:
byte
short
int -> most commont integral data type
long
float
double
char -> 8-bit integer enclose in single code. ex. x='a';
string

four integral
byte
short
int
long
Precision data type
float -> single precision
double ->double precision
-------------------------------------------------------------------Example of In
t
namespace SampleConsole{
class Program {
static void Main (string[]args){
string name;
int age;
Console.WriteLine("What's Your Name?");
name = Console.ReadLine();
Console.WriteLine("What's Your Age?");
age = int.Parse(Console.ReadLine()); //1st conversion me
thod
age = Convert.ToInt32(Console.ReadLine()); //2nd convers
ion method
Console.WriteLine("Hello " + name +", Age: " +age); //1s
t method
Console.WriteLine("Hello {0}, Age {1}", name, age); //2n
d method
}
}
}
-------------------------------------------------------------------Example of In
t with computation
using System;
using System.Text;
namespace SampleConsole{
class Program {
static void Main (string[]args){
string name;
int age;
Console.WriteLine("What's Your Name?");
name = Console.ReadLine();
Console.WriteLine("What's Your Age?");
age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Hello " + name +", Age: " +age );
Console.WriteLine("You will be {0} years old after 5 yea
rs", (age + 5));
}
}
-------------------------------------------------------------------Example of Do
uble with computation
using System;
using System.Text;
namespace SampleConsole{
class Program {
static void Main (string[]args){

double length, width, area;


Console.WriteLine("Enter the Length of the Rectangle");
length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Width of the Rectangle");
width = double.Parse(Console.ReadLine());
area = length * width;
Console.WriteLine("The Are of Rectangle is : {0} square
units", area);
}
}
}
7.7.2016
Operators in C#
1. Arithmetic Operation
(* + - / %)
2. Relational Operators In C#
bool
T = 1
F = 0
< --- less than
> ----- greater than
<= ----- Less Than or Equal
>= ------ Greater Than or Equal
== ------ Equal to
!= ------ is NOT Equal
3. Single Operator
= -------- Equal is Assignment Operation
4. Logical Operators
Logical OR -- ||
Logical AND -- &&
Ex.
int x =
int y =
(((x+y)
8

2
6
< (x*1)/y) || (x!=100)) && ((x*x) > (x*3))
<
2/6 = 0
T
4
>
6
T
|| T
&&
f
F

Hierarchy
&& first Before ||
Ex.
(((x+x*2) > ((y/2)*3) && (x+y!=y+x))|| ((x<y) && (x<(y*3)))
6
>
9
&&
8!=8 || T &&
2<18
F
&&
F || T &&
T
F
|| T
T
Mexp1 - A
Write a sample
C# Program that will initialize two int variables and create four expressions wi
th the following rules
1. All Expression will use at least 3 arithmetic operation
2. All expression will use both OR and AND Logical Operation

using System;
using System.Text;
namespace SampleConsole{
class Program {
static void Main (string[]args){
int x = 8;
int y = 3;
Console.WriteLine(((5>(1*x)) || (11==(y+x)) && ((x-y)==5
)));
Console.WriteLine(((y+y) == x) && (0<(x+y*x)) || (2 == (
x/y)));
Console.WriteLine(((x+10)==(x*y)) || ((x/2)>9) && ((y+2)
>(x-3)));
Console.WriteLine((8>(9-y)) && ((y+y*x)>6) || (8!=(y/1))
);
}
}
}
7/14/2016
Instantiation
Initialization
- a variable being assigned a value depending on a datatype
ex. int num = 8;
Instantiation
-creating a copy from a class/prototype
ex. new reserve word
ex. Employee emp = new Employee; //Comstractor method
Write a C# class named
Person, inside the Human namespace that will have a method that accept a paramet
er of a particular name, the mothod will simply display Hello, <name of the pers
on>, Hope you're doing great today
Then create a main program (.exe)
That will use the object as Intance of a person class
the program will ask for a name of the person and the data will be passed on the
method inside the person class
======Program.cs
using Human;
using System;
using System.Text;
namespace Program{
public class MainProgram{
static void Main(string [] args){
string name;
Console.WriteLine("Please enter Name: ");
name = Console.ReadLine();
Person student = new Person();
student.displayMessage(name);
}
}
}

=======Human.cs
using System;
using System.Text;
namespace Human{
public class Person {
public void displayMessage (string name){
Console.WriteLine("Hello, " + name + " Hope you're doing
great today");
}
}
}
'================================================How to Compile with Module
Compile module -------- CSC /t:module ModuleName.cs
Compile Progtram including Module
------ CSC /t:exe /addmodule:Human.netmodule MainProgramName.cs
Ex.
========Program.cs
using Human;
using System;
using System.Text;
namespace Program{
public class MainProgram{
static void Main(string [] args){
string name, age, bday;
Console.WriteLine("Please enter Name: ");
name = Console.ReadLine();
Console.WriteLine("Please enter Age");
age = Console.ReadLine();
Console.WriteLine("Please enter Birthday");
bday = Console.ReadLine();
Person student = new Person();
student.displayMessage(name);
student.displayAge(age);
student.displayBDay(bday);
}
}
}
========Human.cs
using System;
using System.Text;
namespace Human{
public class Person {
public void displayMessage (string name){
Console.WriteLine("Hello, " + name);
}
public void displayAge (string age){
Console.WriteLine("You are " + age);
}
public void displayBDay (string bday){
Console.WriteLine("And Your Birthday is " + bday);
}
}
}

Homework:
Write a c# class named
temperature that will hace a method named
convertToFahrenheit()
that will accept parameters of type double using the following formula:
F = ((C*9)/5)+32
then on the main program, use the object named temp as an INSTANCE of Temperatur
e class
100 C = 212 F
0C = 32 F
========> Temperature.cs
using convertToFahrenheit;
using System;
using System.Text;
namespace Temperature{
public class MainProgram{
static void Main(string [] args){
double celsius;
Console.WriteLine("Please enter the Celsius: ");
celsius = double.Parse(Console.ReadLine());
temperature temp = new temperature();
temp.displayFResult(celsius);
}
}
}
=======> convertToFahrenheit.cs
using System;
using System.Text;
namespace convertToFahrenheit{
public class temperature {
public void displayFResult (double celsius){
double fahrenheit;
fahrenheit = ((celsius*9)/5)+32;
Console.WriteLine(celsius + " degree celsius is equivale
nt to " + fahrenheit + " degree fahrenheit");
}
}
}

2016/7/28
Polymorphism is a capability of a method on a OOP Language to have the same name
, but with different signitures
Polys - many

morphos - change
ism / ismos - Characteristic
ex Public void Add( int x, int y)
Signitures of a method
1. method name ---- Add
2. return type ---- void
3. access type ---- public
4. numbers of parameter ---- 2(x and y)
5. Data Type of Parameters ---- Int
Example:
=============Program.cs (main)
using System;
namespace Program{
public class MainP {
public static void Main(){
int x = 0, y = 0;
Console.WriteLine("Enter 1st Number: ");
x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter 2nd Number: ");
y = int.Parse(Console.ReadLine());
Addition add = new Addition();
add.Add(x,y);
}
}
}
=============Poly.cs (module)
using System;
public class Addition{
public void Add(int x, int y){
int sum = x + y;
Console.WriteLine("The Sum: "+sum);
}
}

Вам также может понравиться