Date: 29/11/2018: Output
Date: 29/11/2018: 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
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
}
}
} 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
a[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
OUTPUT
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
Clear an array by
Clear removing all the items Array.clear(arr,0,35)
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
Structure:
Struct Books
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
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
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");
}
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
}
}
}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
}
}
}
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
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");
}
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
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceabstract_methods
{
classProgram
{
}
}
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;
}
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
namespace exception
{
class Program
{
}
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.
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
27/12/18
OUTPUT