basic c#

23
Agenda Object-Oriented Concepts Abstraction and Encapsulation Inheritance Polymorphism Interfaces and Abstract Classes Virtual Methods Classes Sealed Classes C# programming with examples

Upload: kishore4268

Post on 12-Nov-2014

452 views

Category:

Education


5 download

DESCRIPTION

C# description

TRANSCRIPT

Page 1: Basic c#

Agenda

Object-Oriented ConceptsAbstraction and EncapsulationInheritancePolymorphismInterfaces and Abstract ClassesVirtual MethodsClassesSealed ClassesC# programming with examples

Page 2: Basic c#

Encapsulation and abstraction is the advanced mechanism in C# that lets your program to hide unwanted code within a capsule and shows only essential features of an object.

Encapsulation is used to hide its members from outside class or interface, whereas abstraction is used to show only essential features.

Access Modifier – Public, Private, Protected, Internal & Protected Internal

Key Object-Oriented Concepts

Page 3: Basic c#

Foreach

using System; namespace foreach_loop{  class Program   {     static void Main(string[] args)      {        string[] arr = new string[5]; // declaring array         //Storing value in array element        arr[0] = "Steven";        arr[1] = "Clark";        arr[2] = "Mark";        arr[3] = "Thompson";        arr[4] = "John";         //retrieving value using foreach loop        foreach (string name in arr)         {           Console.WriteLine("Hello " + name);         }        Console.ReadLine();      }   }}

Page 4: Basic c#

switch case

switch (opt)

{

case 1:

result = num1 + num2;

Console.WriteLine("\n{0} + {1} = {2}", num1, num2, result);

break;

case 2:

result = num1 - num2;

Console.WriteLine("\n{0} - {1} = {2}", num1, num2, result);

break;

default:

Console.WriteLine("\nInvalid option.Please try again.");

Break;

}

Page 5: Basic c#

Array

using System; namespace Declare_Array{  class Program   {     static void Main(string[] args)      {        int[] num = new int[6]; //Declaring Array                  //Initializing array        num[0] = 6;        num[1] = 23;        num[2] = 12;        num[3] = 9;        num[4] = 14;        num[5] = 52;         //Showing value of Array        Console.WriteLine("1st value:\t{0}", num[0]);        Console.WriteLine("2nd value:\t{0}", num[1]);        Console.WriteLine("3rd value:\t{0}", num[2]);        Console.WriteLine("4th value:\t{0}", num[3]);        Console.WriteLine("5th value:\t{0}", num[4]);        Console.WriteLine("6th value:\t{0}", num[5]);         Console.ReadLine();                 }   }}

Page 6: Basic c#

Data Types

C# is a strongly typed language. It means, that you cannot use variable without data types.

Data types tell the compiler that which type of data is used for processing.

Such as if you want to work with string value then you will have to assign string type variable to work with.

C# provides two types of data types: Value types and Reference types. A Value type data type stores copy of the value whereas the 

Reference typedata types stores the address of the value.

Page 7: Basic c#

Value types

Data Types

Size Values

sbyte 8 bit -128 to 127

byte 8 bit 0 to 255

short 16 bit -32,768 to 32,767

ushort 16 bit 0 to 65,535

int 32 bit-2,147,483,648 to 2,147,483,647

uint 32 bit 0 to 4,294,967,295

long 64 bit-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

ulong 64 bit0 to 18,446,744,073,709,551,615

char 16 bit 0 to 65535

float 32 bit -1.5 x 1045 to 3.4 x 1038

double 64 bit -5 x 10324 to 1.7 x 10308

decimal 128 bit -1028 to 7.9 x 1028

bool --- True or false

Page 8: Basic c#

Reference Types

Data Types Size Values

stringVariable length

0-2 billion Unicode characters

object --- ---

Page 9: Basic c#

Boxing and Unboxing

class Test

{

static void Main()

{

int i = 1;

object o = i; // boxing

int j = (int) o; // unboxing

}

}

Page 10: Basic c#

Overloading and Overriding

class Person

{

private String firstName;

private String lastName;

Person()

{

this.firstName = "";

this.lastName = "";

}

Person(String FirstName)

{

this.firstName = FirstName;

this.lastName = "";

}

Person(String FirstName, String LastName)

{

this.firstName = FirstName;

this.lastName = LastName;

}

}

Page 11: Basic c#

Calling overloading method

Person(); // as a constructor and call method without parameter

Person(userFirstName); // as a constructor and call method with one parameter(like User's first Name)

Person(userFirstName,userLastName); // as a constructor and call method with one parameter(like User's first Name)

Page 12: Basic c#

Polymorphism

Polymorphism provides following features: It allows you to invoke methods of derived class through base class reference during runtime.It has the ability for classes to provide different implementations of methods that are called through the same name.

Polymorphism is of two types: Compile time polymorphism/OverloadingRuntime polymorphism/Overriding

Page 13: Basic c#

Polymorphism

Compile Time Polymorphism

 Compile time polymorphism is method and operators overloading. It is also called early binding. In method overloading method performs the different task at the different input parameters.

 

Runtime Time Polymorphism Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.

 

“When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.”

Page 14: Basic c#

Interfaces

An interface defines a contract An interface is a type Includes methods, properties, indexers, events Any class or struct implementing an interface must support all parts of the contract

Interfaces provide no implementation When a class or struct implements an interface it must provide the implementation

Interfaces provide polymorphism Many classes and structs may implement

a particular interface

Page 15: Basic c#

public interface IDelete { void Delete();}public class TextBox : IDelete { public void Delete() { ... }}public class Car : IDelete { public void Delete() { ... }}

TextBox tb = new TextBox();IDelete iDel = tb;iDel.Delete();

Car c = new Car();iDel = c;iDel.Delete();

InterfacesExample

Page 16: Basic c#

interface IControl { void Paint();}interface IListBox: IControl { void SetItems(string[] items);}interface IComboBox: ITextBox, IListBox {}

InterfacesMultiple Inheritance

Classes and structs can inherit from multiple interfaces

Interfaces can inherit from multiple interfaces

Page 17: Basic c#

interface IControl { void Delete();}interface IListBox: IControl { void Delete();}interface IComboBox: ITextBox, IListBox { void IControl.Delete(); void IListBox.Delete();}

InterfacesExplicit Interface Members

If two interfaces have the same method name, you can explicitly specify interface + method name to disambiguate their implementations

Page 18: Basic c#

Classes and StructsSimilarities

Both are user-defined types Both can implement multiple interfaces Both can contain

Data Fields, constants, events, arrays

Functions Methods, properties, indexers, operators, constructors

Type definitions Classes, structs, enums, interfaces, delegates

Page 19: Basic c#

Class Struct

Reference type Value type

Can inherit from any non-sealed reference type

No inheritance(inherits only from System.ValueType)

Can have a destructor No destructor

Can have user-defined parameterless constructor

No user-defined parameterless constructor

Classes and StructsDifferences

Page 20: Basic c#

Classes and StructsAccess Modifiers

Access modifiers specify who can use a type or a member

Access modifiers control encapsulation Top-level types (those directly in a namespace)

can be public or internal Class members can be public, private, protected, internal, or protected internal

Struct members can be public, private or internal

Page 21: Basic c#

If the access modifier is

Then a member defined in type T and assembly A is accessible

public to everyone

private within T only (the default)

protected to T or types derived from T

internal to types within A

protected internal

to T or types derived from Tor to types within A

Classes and StructsAccess Modifiers

Page 22: Basic c#

Classes and StructsAbstract Classes

An abstract class is one that cannot be instantiated

Intended to be used as a base class May contain abstract and non-abstract

function members Similar to an interface Cannot be sealed

Page 23: Basic c#

Classes and StructsSealed Classes

A sealed class is one that cannot be used as a base class

Sealed classes can’t be abstract All structs are implicitly sealed Why seal a class?

To prevent unintended derivation Code optimization

Virtual function calls can be resolved at compile-time