Basic c#
Upcoming SlideShare
Loading in...5
×
 

Basic c#

on

  • 413 views

C# description

C# description

Statistics

Views

Total Views
413
Views on SlideShare
413
Embed Views
0

Actions

Likes
0
Downloads
10
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • The term object sometimes refers to an instance and sometimes to a class. It’s meaning can usually be determined by context.
  • An interface is a well-defined set of methods (functions). Interfaces do not contain data members. Type is different from class. The type specifies the interfaces, the class specifies the implementation.
  • Polymorphism is achieved through: Inheritance: a base type can have multiple derived types Interfaces: multiple types can implement a given interface Late binding: you can use any object, as long as it implements the methods you want to call. In C# you can use reflection to dynamically determine if an object implements a method, and then call it. Interfaces can also be used in a late-bound manner. In order to handle multiple types without polymorphism you have to write conditional code (using if or switch statements) that tests the type of an instance and then runs the appropriate code. Such code is brittle and not easily extended. Many Object-Oriented design concepts are motivated by minimizing dependencies. You want to be able to develop independent modules, so that making a change to one doesn’t force you to have to go back and change others.
  • In scenarios where completely different objects need to support some kind of shared functionality like, let’s say, persist to XML, classes can implement interfaces that make them compatible with even if they don’t share the same base class. This provides most of the benefits of multiple class inheritance without the nasty side-effects that this usually brings. Interface members are implicitly public and abstract.
  • If there’s no ambiguity then you do not have to specify the name of the interface.
  • Classes and structs provide a way to create user-defined types.
  • protected internal = protected OR internal. No way to define protected AND internal.

Basic c# Basic c# Presentation Transcript

  • Agenda Object-Oriented Concepts Abstraction and Encapsulation Inheritance Polymorphism Interfaces and Abstract Classes Virtual Methods Classes Sealed Classes C# programming with examples
  •  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
  • 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(); } } }
  • 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; }
  • 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(); } } }
  • 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.
  • 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 bit 0 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
  • Reference Types Data Types Size Values string Variable length 0-2 billion Unicode characters object --- ---
  • Boxing and Unboxing class Test { static void Main() { int i = 1; object o = i; // boxing int j = (int) o; // unboxing } }
  • 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; } }
  • 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)
  • 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/Overloading Runtime polymorphism/Overriding
  • 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.”
  • 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
  • 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(); Interfaces Example
  • interface IControl { void Paint(); } interface IListBox: IControl { void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox { } Interfaces Multiple Inheritance  Classes and structs can inherit from  multiple interfaces  Interfaces can inherit from multiple interfaces
  • interface IControl { void Delete(); } interface IListBox: IControl { void Delete(); } interface IComboBox: ITextBox, IListBox { void IControl.Delete(); void IListBox.Delete(); } Interfaces Explicit Interface Members  If two interfaces have the same method name,  you can explicitly specify interface + method  name to disambiguate their implementations
  • Classes and Structs Similarities  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
  • 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 Structs Differences
  • Classes and Structs Access 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
  • 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 T or to types within A Classes and Structs Access Modifiers
  • Classes and Structs Abstract 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
  • Classes and Structs Sealed 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