0% found this document useful (0 votes)
100 views52 pages

Date: 29/11/2018: Output

The document discusses .NET framework and C# programming language. It provides an overview of .NET framework including common language runtime, framework class library and languages interoperability. It also discusses C# data types, OOPS concepts, type casting, control statements like if-else, switch, do-while etc. and arrays in C#. It provides code examples to demonstrate various concepts like implicit type casting, explicit type casting, parse method, boxing-unboxing, single dimension and multi dimension arrays and passing arrays as parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views52 pages

Date: 29/11/2018: Output

The document discusses .NET framework and C# programming language. It provides an overview of .NET framework including common language runtime, framework class library and languages interoperability. It also discusses C# data types, OOPS concepts, type casting, control statements like if-else, switch, do-while etc. and arrays in C#. It provides code examples to demonstrate various concepts like implicit type casting, explicit type casting, parse method, boxing-unboxing, single dimension and multi dimension arrays and passing arrays as parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

OUTPUT

.Net

Date: 29/11/2018
INTRODUCTION
.Net framework is a software frame work developed by Microsoft windows. It
includes a large class library knows as framework class library (FCL) and provides
languages interoperability (each languages can use code written for .Net framework
execute in a software environment (as contrasted to hardware environment) known
as common language runtime (CLR). An application virtual machine that provides
services such as security, memory management, and exception handling (As such,
computer code written using .Net framework. FCL provides user interface, data
access, database connectivity, cryptography, web applications development, numeric
algorithms, and network communications programmers produce software by
combining their own source code with .Net framework and other libraries .Net
framework is intended to be used by most new applications created by the windows
platform. Microsoft also produces an integrated development environment largely
for .Net framework is called visual studio.
C# was developed by andersflylsberg and his team during the development of
.Net framework at year (2002).consists of executable code and runtime environment
that allows us of various high level languages on different computer plateforms and
architectures.

DATA TYPES IN C#
PREDEFINED -> SIMPLE OBJECT STRING
SIMPLE -> NON NUMERIC NUMERIC
NON NUMERIC -> BOOLEAN CHAR
NUMERIC -> INTEGER FLOATING
INTEGER -> SBYTE BYTE STRONG USTRONG INT UINT LONG
ULONG
FLOATING -> DECIMAL FLOAT DOUBLE
All the programing languages supports object oriented programing will be
supporting these main concepts.
OUTPUT

OOPS CONCEPTS
1. Abstraction.
2. Encapsulation.
3. Inheritance.
4. Polymorphism.

30/11/18
TYPE CASTING
we often work with multiple datatypes at once converting one data type to
another is common in programming.Type conversion or type casting refers to
changing an entity of one datatype into another
there are 2 types of conversions
Implicit
It is automatic done by the compiler
PROGRAM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace implict
{
class Program
{
static void Main(string[] args)
{
int a = 0;
byte b = 10;
b = a;
Console.WriteLine(a.GetType());
Console.WriteLine(b.GetType());
Console.WriteLine(a);
}
}
}
OUTPUT

OUTPUT

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

namespace explict
{
class Program
{
static void Main(string[] args)
{
float a;
double b =12;
int c;
a = (float)b;
c = (int)a;
Console.WriteLine(a + b + c);
}
}
}
OUTPUT
OUTPUT

PARSE METHOD
It is used for converting user input into primitive datatypes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace parsemethod
{
class Program
{
static void Main(string[] args)
{
int n;
float w;
Console.WriteLine("enter any number");
n = int.Parse(Console.ReadLine());
Console.WriteLine("enter your weight");
w = float.Parse(Console.ReadLine());
Console.WriteLine("you entered"+n);
Console.WriteLine("your weight is" + w);
Console.ReadLine();

}
}
}OUTPUT
OUTPUT

BOXING AND UNBOXING


Boxing and unboxing are 2 important concepts in c# type
system ,with boxing and unboxing one can link between
value type and reference types by allowing any value type
to be converted to and form of type object

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

namespace boxing
{
class Program
{
static void Main(string[] args)
{
int i = 10;
object o = i;
int j = (int)o;
Console.WriteLine("value of object:" + o);
Console.WriteLine("value of j:" + j);
Console.ReadLine();
}
}
}
OUTPUT
OUTPUT

3/12/18

CONTROL STATEMENTS
These statements give you additional means to control the processing with in
the applications with in the applications you develop
This section explores the syntax and function of the switch ,do while,
for,foreach,goto,break,continue and return statements

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

namespace @if
{
class Program
{
static void Main(string[] args)
{
int a, b;
Console.WriteLine("enter two numbers");
OUTPUT

a = Int32.Parse(Console.ReadLine());
b = Int32.Parse(Console.ReadLine());
if (a > b)
{
Console.WriteLine("a is greater");

}
else
{
Console.WriteLine("b is greater");
}
Console.ReadLine();
}
}
}
OUTPUT

SWITCH

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

namespace @switch
{
class Program
{
static void Main(string[] args)
{
int gender;
OUTPUT

Console.WriteLine("enter 1 for male 2for female");


gender = Int32.Parse(Console.ReadLine());
switch (gender)
{
case 1: Console.WriteLine("you are male");
break;
case 2: Console.WriteLine("you are female");
break;
default: Console.WriteLine("other");
break;
}
Console.ReadLine();

}
}
} OUTPUT

DO WHILE
The while loop allows the user to repeat a section of code until a guard
condition is met .The do while construct checks the condition at the end of the
loop ,the do while executes atleast once even through the condition to be
checked is false

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

namespace dowhile
{
OUTPUT

class Program
{
static void Main(string[] args)
{
int i=0,n,count=0;
Console.WriteLine("enter the number");
n = Int32.Parse(Console.ReadLine());
i = n;
do
{
count++;
i = i / 10;
} while (i > 0);
Console.WriteLine("it has {0}digits", count);
Console.ReadLine();
}
}
}
OUTPUT

4/12/18
Arrays
An array stores a fixed size sequential collection of elements of the same type.
An array is used to store a collection of data, but it is often more useful to
think of an array as a collection of variables of the same type stored at
contiguous memory locations.
OUTPUT

To declare an array in c#, we use the following syntax.


Syntax: - Datatype [] arrayname;
Example: - Int [] a;

Example program on single dimension array


namespace singledarray
{
class Program
{
static void Main(string[] args)
{
int[] n = new int[10];
int i, j;
for (i = 0; i < 10; i++)
{
n[i] = i + 100;
}
for (j = 0; j < 10; j++)
{
Console.WriteLine("elements [{0}]={1}", j, n[j]);
}
Console.ReadLine();
}
}
}

Example program on multi dimension array


namespace _2d_array
{
class Program
{
static void Main(string[] args)
{
int [,] a = new int[2, 2];
int [,] b = new int[2, 2];
int i,j;
Console.WriteLine("enter array values");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{

a[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
OUTPUT

for (i = 0; i < 2; i++)


{
for (j = 0; j < 2; j++)
{
b[i, j] = a[i, j];
}
}
Console.WriteLine("elements of 2nd array");
for (i = 0; i < 2; i++)
{
Console.WriteLine();
for (j = 0; j < 2; j++)
{
Console.WriteLine("{0} ", b[i, j]);
}
}
Console.ReadLine();
}
}
}

Param Arrays
Passing arrays as a parameters in functions
namespace paramarrays
{
class Program
{
double getaver(int[] arr, int size)
{
int i;
double avg;
int sum = 0;
for (i = 0; i < size; i++)
{
sum += arr[i];
}
avg = (double)sum / size;
return avg;
}
static void Main(string[] args)
{
Program o = new Program();
int[] balance = new int[] { 1, 2, 3, 17, 50 };
double avg;
OUTPUT

avg = o.getaver(balance, 5);


Console.WriteLine("average value is{0}", avg);
Console.ReadLine();
}
}
}

Properties of an array class


Properties Explanation Example
Return the length of array
Length return integer value Int i=arr1.length

Return total number of


Rank items in all the Int i=arr1.Ranks
dimensions return integer
value
Check whether array is
IsFixedSize fixed size(or)not return bool i=arr1.IsFixedSize
Boolean value

Check whether array is


IsReadOnly read only(or)not return bool i=arr1.IsReadOnly
Boolean value

Most common functions of array class


Functions Explanation Example

Sort Sort an array Array.sort(arr)


OUTPUT

Clear an array by
Clear removing all the items Array.clear(arr,0,35)

Return the number of


Get length Elements Array.getlength(0)

Return the number of Array.getvalue(2)


Get value specified items

Return the indexed Array.Indexof(arr,4)


Index Of position of value

Copy array elements to Array.copy(arr1,arr2,3)


Copy another elements

Jagged Arrays
An array inside an array is called jagged array
namespace jagged_arrays
{
class Program
{
static void Main(string[] args)
{
Int [][] a = new int[] { new int[] { 0, 0 }, new int[]
{ 1, 2 }, new int[] { 2, 4 }, new int[] { 3, 6 }, new int[] { 4, 8 }
};
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 2; j++)
{
Console.WriteLine("a[{0}] [{1}] = {2}", i, j,
a[i][j]);
}
}
Console.ReadLine();

}
}
}
OUTPUT

5/12/18
String:
In C# programming string is another kind of data type that represent Unicode characters it is the
ailas of System. String. It is the sequence of characters is a Unicode characters.

C# sting function:

Sting Definition
Clone() Make clone of string
compareTo() Compare two stings and return integer value as
output. It return for true and flase.
Contain() The C# contain method checks whether specified
characters or sting is exits or not in the string value.
Endwith() The Endwith method checks whether specified
character is last characters of sting or not
Equals() The equals method in C# contains two string and
return Boolean value as output
Gethashcode() This method return hashvalue of specified string
Gettype() This return the system. type of current instance

Gettypecode() It return the system. Type code fpr class system.


String
Indexof() Return the index position of fire occurrence of
specified characters
Tolower() Convert string into lower case based on rules of the
current culture.
Toupper() Convert strong into upper case based on rules of
the current culture
Insert() Insert the string or characters in the string at the
specified position
Isnormalization() This method checks whether this string is in
Unicode normalization form c
Lastindex() Return the index position of last occurrence of
specified character
Remove () This method deletes all the characters from
beginning to specified index position
Replace() This method replaces the characters
Split() This method split the string based on specified
value
Start with() It checks whether the first characters of string is
same as specified characters
Substring() This method returns substring
TocharArray() Convert string into char array
OUTPUT

Trim() It removes extra whitespaces from beginning and


ending of string
Program
OUTPUT

Structure:
Struct Books

Private string title;

Private string author;

Private string subject;

Private int bood-id;

Program:
OUTPUT

ABSTRACT CLASS
Abstract classes are one of the essential nbehaviour provided by .NET
commonly ,you would like to make classes that only represents base classes
and don’t want anyone to create objects of these class types you can make use
OUTPUT

of abstract classes to implement such functionality in c# using the modifier


‘abstract’
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace @abstract
{
class Program
{
abstract class Fourwheeler
{
public virtual string describe()
{
return "not much is known about four wheeler";
}

}
public class Car :Fourwheeler
{

}
static void Main(string[] args)
{
Car c = new Car();
Console.WriteLine(c.describe());
Console.ReadKey();
}

}
}

Date: 6/12/2018
ENCAPSULATION
Encapsulation is defined “as the process of enclosing one or more items with
in a physical or logical package”. Encapsulation in object oriented programing
methodology prevents access to implementation details.
Abstraction and encapsulation are related features in object oriented
programing. Abstraction allows making relevant information visible and
encapsulation enables programmers to implement the desired level of abstractions.
1. Public.
2. Private.
3. Protected.
4. Internal.
OUTPUT

5. Protected internal.

Example
Namespace accessmodifires
{
Class program
{
Class Accessmod
{
Public int num1;
}
Static void main(string[] args)
{
Accessmod obj=new accessmod();
Obj.num1=100;
Console.writeline(“number one{0}”,obj.num);
Console.readline();
}
}
}

EXAMPLE
Class Accessmod
{
Class Base
{
Protected int num1;
}
Class Derived : Base
OUTPUT

{
Public int num2;
Static void main(String[] args)
{
Base ob1=new Base();
Derived ob2=new Derived();
Ob2.num1=20;
Ob2.num2=90;
Console.writeline(“number 2 value {0}”,ob2.num1);
Console.writeline(“number 1 value {0}”,ob2.num1);
Console.readline();
}
}
}
}

INTERNAL
The internal keyword is an access modifiers for Types members. We can
declare a class as internal. Internal members are accessible only within files in the
same Assembly (.DLL).
In other words access is limited exclusively to class defined with in the current
project assembly.

PROTECTED INTERNAL
The protected internal accessibility means protected or internal, not
protected and internal.
OUTPUT

In other words a protected internal member is accessibility from any class in


the same assembly including derived classes.
Classes:
When you define a class, you define a blue print for a datatype . this doesn’t actually define any
data ,but it does define what the class name means. That is, what an object of the class consists of
and what operations can be performed on that object. object are instances of a classes. The method
and variable that constitute a class are called member of the class.

10/12/18
Constructor:
A class constructor is a special member function of a class that is executed whenever we create a
new objects of that class

A constructs has exactly the same name as that of class and it doesn’t have any return type

Types:

1. default constructor
2. parameterized constructor
3. copy constructor
4. static constructor
5. private constructor

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

namespace ConsoleApplication15
{
class sample
{
public string p1, p2;
public sample(string x, string y)
{
p1 = x;
p2 = y;
}
public sample(sample obj)
{
p1 = obj.p1;
p2 = obj.p2;
}
}
class Program
{
static void Main(string[] args)
OUTPUT

{
sample obj = new sample("welcome","asdonet");
sample ob1 = new sample(obj);
Console.WriteLine(ob1.p1+"0"+ob1.p2);
Console.ReadLine();

}
}
}

static constructor:

when we declared constructor as static it will be invoked only one fir any number of instances of the
class and it’s during the creation of first instance of the class or the first references to a static
member in the class static constructor is used to initialize static fields of the classes and to write the
code that needs to be executed only once

 static constructor will not accept any parameter because it is automatically called by CLR
 static constructor will not have any access modifiers
 static constructor will execute automatically whenever we create first instances of class
only one static constructor will allowed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication17
{
class sample
{
public string p1, p2;
static sample()
{
Console.WriteLine("static");
OUTPUT

}
public sample()
{
p1 = "sample";
p2 = "instance constructor";
}
}
class Program
{
static void Main(string[] args)
{
sample obj = new sample();
Console.WriteLine(obj.p1+" "+obj.p2);
sample ob1 = new sample();
Console.WriteLine(ob1.p1+""+ob1.p2);
Console.ReadLine();
}
}

Destructor:

A destructor is a special member. Destructor can be very useful for releasing memory resource
before exiting the program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication23
{
class Program
{
OUTPUT

class line
{
private double len;
public line()
{
Console.WriteLine("object is bring created");
}
~line()
{
Console.WriteLine("object is beging deleted");
}

static void Main(string[] args)


{
line l = new line();
Console.ReadKey();
}
}
}

11/12/18
method in c#

A method is a group of statements that together perform a task. Every c# program has atleast
one class with a method named Main. Method are extremely useful because they allow you to
separate your logic into different units. You can pass information to method have it perform one
or more statements and retrieve a return value. the capability to pass parameter and return
values is optional and depends on what you want the method to do.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication19
{
OUTPUT

class Programu
{
class membermanipulator
{
public int findmax(int num1, int num2)
{
int r;
if (num1 < num2)
r = num1;
else
r = num2;
return r;
}
}
class test
{
static void Main(string[] args)
{
int a = 100, b = 200, ret;
membermanipulator n = new membermanipulator();
ret = n.findmax(a, b);
Console.WriteLine("max vlaue is:{0}", ret);
Console.ReadLine();

}
}

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

namespace ConsoleApplication21
{
class r
{
public int fact(int num)
{
int result;
if (num == 1)
{
OUTPUT

return 1;
}
else
{
result = fact(num - 1) * num;
return result;
}
}
}
class Program
{
static void Main(string[] args)
{
r n = new r();
Console.WriteLine("factorial of 6 is {0}", n.fact(6));
Console.ReadLine();
}
}
}

12/18/2018
Practice on console application

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

namespace parsemethod
{
class Program
{
OUTPUT

static void Main(string[] args)


{
int n;
float w;
Console.WriteLine("enter any number");
n = int.Parse(Console.ReadLine());
Console.WriteLine("enter your weight");
w = float.Parse(Console.ReadLine());
Console.WriteLine("you entered"+n);
Console.WriteLine("your weight is" + w);
Console.ReadLine();

}
}
}OUTPUT
OUTPUT

Multidimensional program:
OUTPUT

Paramarray
OUTPUT
OUTPUT

13/12/18
Operator overloading

You can redefine or overload most of the built-in operator available in c#. thus a programmer
can use operator with user-defned types as well. Overloaded operator are function with special
names the keyword operator followed by the symbol for the operator being defined similar to
any other function, an overloaded operator has a return type and a parameter list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication22
{
class Program
{
public struct complex
{
public int real;
public int imaginary;
public complex(int real,int imaginary)
{
this.real=real;
this.imaginary=imaginary;
}
public static complex operator +(complex c1,complex c2)
{
return new complex(c1.real+c2.real,c1.imaginary+c2.imaginary);
}
public override string ToString()
{
return (string.Format("{0}+{0}i", real, imaginary));
}
static void Main(string[] args)
{
complex num1=new complex(2,3);
complex num2=new complex(3,4);
complex sum=num1+num2;
Console.WriteLine("the first complex{0}",num1);
Console.WriteLine("second complex {0}",sum);
Console.ReadKey();
}
}
}

}
OUTPUT

Overriding method:

Creating a method in derived class with same signature as a method in base class is called as
method over loading

Same signature means must have same name, same number of arguments and same type of
arguments

Method over ridding is possible only in derived classes bit not within the same class.

When derived class needs a method with same signature as in base class but wants to execute
different code than provided by base class then method overriding will be used

To allow the derived class to over ride a method of base class, c# provide two options, virtual
method and abstract method in classes, known as over ridding their logic is one of the most
powerful aspects of oops.
OUTPUT
OUTPUT

14/12/18
Practice

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

namespace ConsoleApplication15
{
class sample
{
public string p1, p2;
public sample(string x, string y)
{
p1 = x;
p2 = y;
}
public sample(sample obj)
{
p1 = obj.p1;
p2 = obj.p2;
}
}
class Program
{
OUTPUT

static void Main(string[] args)


{
sample obj = new sample("welcome","asdonet");
sample ob1 = new sample(obj);
Console.WriteLine(ob1.p1+"0"+ob1.p2);
Console.ReadLine();

}
}
}

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

namespace ConsoleApplication17
{
class sample
{
public string p1, p2;
static sample()
{
Console.WriteLine("static");
}
public sample()
{
p1 = "sample";
p2 = "instance constructor";
}
}
class Program
{
static void Main(string[] args)
{
sample obj = new sample();
Console.WriteLine(obj.p1+" "+obj.p2);
OUTPUT

sample ob1 = new sample();


Console.WriteLine(ob1.p1+""+ob1.p2);
Console.ReadLine();
}
}

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

namespace ConsoleApplication23
{
class Program
{
class line
{
private double len;
public line()
{
Console.WriteLine("object is bring created");
}
~line()
{
Console.WriteLine("object is beging deleted");
}

static void Main(string[] args)


{
line l = new line();
Console.ReadKey();
}
}
}
OUTPUT

18/12/18
pratice
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication19
{
class Programu
{
class membermanipulator
{
public int findmax(int num1, int num2)
{
int r;
if (num1 < num2)
r = num1;
else
r = num2;
return r;
}
}
class test
{
static void Main(string[] args)
{
int a = 100, b = 200, ret;
membermanipulator n = new membermanipulator();
ret = n.findmax(a, b);
Console.WriteLine("max vlaue is:{0}", ret);
Console.ReadLine();

}
}
OUTPUT

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

namespace ConsoleApplication21
{
class r
{
public int fact(int num)
{
int result;
if (num == 1)
{
return 1;
}
else
{
result = fact(num - 1) * num;
return result;
}
}
}
class Program
{
static void Main(string[] args)
{
r n = new r();
Console.WriteLine("factorial of 6 is {0}", n.fact(6));
Console.ReadLine();
}
}
}
OUTPUT

19/12/18

ABSTRACT METHODS

Abstract classes and methods


Abstract classes are one of the essential behavior provided by .net commonly, you would like to
make classes that only represent base classes and don’t want anyone to create objects of these
class types. You can make use of abstract classes to implements such functionality in c# using
the modifier abstract
OUTPUT
OUTPUT

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

namespaceabstract_methods
{
classProgram
{

staticvoid Main(string[] args)


{
System.Collections.ArrayList booklist = newSystem.Collections.ArrayList();
booklist.Addnew(csharp());
booklist.Addnew(oraclebook());
foreach(softwarebook book in booklist)
{
Console.writeLine(book Describe());
Console.Readkey();
}
}
abstractclasssoftwarebook
{
publicabstractstringDescribe();
}
classcsharp :softwarebook
{
publicoverridestringDescribe()
{
return"i' am a csharp book";

}
}
classoraclebook :softwarebook
{
publicoverridestringDescribe()
{
return"i' am a oracle book";
}

DELEGATES
A deligate is a reference type variable that reference to a method the
reference can be changed at run time
Delegates are especially used for implementing event and call back
methods all delegates are implicitly derived from the system.Delegate
class

using System;
using System.Collections.Generic;
using System.Linq;
OUTPUT

using System.Text;
using System.Threading.Tasks;

namespace delegates
{
class Program
{
delegate int NC(int n);
class TD
{
static int num = 10;
public static int Addnum(int p)
{
num += p;
return num;
}

public static int multnum(int q)


{
num *= q;
return num;
}
public static int getnum()
{
return num;
}
static void Main(string[] args)
{
NC a1 = new NC(Addnum);
NC a2 = new NC(multnum);
a1(24);
Console.WriteLine("value of num:{0}", getnum());
a2(5);
Console.WriteLine("value of num:{0}", getnum());
Console.ReadKey();
}
}
}
}
OUTPUT

EXCEPTION HANDLING
an exception is a problem that araises during the
execution of a program. A c# exception is a response to
exceptional circumstance that arises while a program
running such as an attempt to divide by zero

Exceptions provide a way to transfer control from one part


of a program to another .c# exception handling is built
upon four keywords :try,catch,finally and throw
TRY: A try block identifies a block of code for which
particular exceptions is activated .It is followed by one
or more catch blocks

CATCH: A program catches an exception with an exception


handler at the place in a program where you want to handle
the problem ,The catch keyword indicates exception

FINALLY:the finally block is used to executes a given set


of statements whether an exception is thrown pr not thrown
for example if you open a file it must be closed whether
an exception is raised or not

THROW:A program throws an exception whether problem shows


nup this is done using a throw keyword
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exception
{
class Program
{

static void Main(string[] args)


{
int a = 10, b = 0;
int c=0;
try
{
c = a / b;
}
catch (DivideByZeroException e)
{
Console.WriteLine("exception caught :{0}", e);
OUTPUT

}
finally
{
Console.WriteLine("Result :{0}",c);
}
Console.ReadLine();

}
}
}
OUTPUT

20/12/18

C# file i/o

A file is a collection of data stored in a disk with a specific name and a directory path. When a
files is opened for reading or writing it become a stream.

The stream is basically the sequence of bytes passing through the communication path. There
are two main streams. the input stream and the output streams. The input stream is used for
reading data from file (read operation) and the output stream is used for writing into the file.

The system IO namespace has various classes that are used for performing numerous
operation with files, closing a file such as creating and deleting files, reading from or writing to a
file, closing a file etc.
OUTPUT

You need to create a file stream object to create a new file or open an existing file. The syntax
for creating a file stream object is as follow.

Filestream<objectname>=new Filestream<filestream>, <fileMode Enumerator>,<FileAccess


Enumerator>, <fileshare Enumerator>);

file mode:

The File Mode Enumerator defines various methods for opening files, The members of the File
Mode enumerator are:

 Append: It opens an existing file and puts curse at the end of file,or creating the file if
the file does not exist.
 Create: It creates a new file.
 Create new: It specifics to the operating system, that it should create a new file.
 Open: It opens an existing file.
 Open or create: It specifies to the os that it should open a file if it exists, otherwise it
creates a new file
 Truncate: It creates an existing file and truncate its sixe to zero to zero by tens

file Access enumeration:

members: Read, Read Write, Write.


File share enumeration have the following members:
 Inheritance: It allows a file handle to pass inheritance to the child process.
 None: It decline sharing of the current file.
 Read: It allows opening the file for reading
 Read write: It allows opening the file for reading and writing
 Write: It allows opening the file for writing
OUTPUT
OUTPUT

21/12/18 & 26/12/18


OUTPUT
OUTPUT
OUTPUT
OUTPUT

27/12/18
OUTPUT

You might also like