36
C# language Session -3

ASP.NET Session 3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: ASP.NET Session 3

C# language

Session -3

Page 2: ASP.NET Session 3

Objectives1.Understanding of the language basics to facilitate

development.

2.Basic Object Oriented Programming concepts and C# Programming Structure.

3. Language fundamentals

4.Doing our first C# Program.

5. Hands on knowledge in Visual Studio.

Page 3: ASP.NET Session 3

Introduction• C# (pronounced “See Sharp”) is a simple, modern,

object-oriented programming language.

• It was made by Microsoft Corporation, more precisely by Anders Hejlsberg.

• It was created to use all capacities of .NET platform. The first version appeared in 2001, last update appeared in 2010 with the C# 4.0.

• C# has its roots in the C family of languages and will be immediately familiar to C, C++, and Java programmers.

Page 4: ASP.NET Session 3

Fundamentals of Object Oriented Programming

• OOPs design methodology is different from traditional language like BASIC, Pascal, C etc. Those are called the Procedural Language.

• In OOP, the emphasis is on Data and not on procedures.

Class

• In OOP, A class describes all the attributes of objects,as well as the methods that implement the behavior of member object.

• A class is only a specification of a data type.

• A class is like a blue print of the Object.

Page 5: ASP.NET Session 3

Fundamentals of Object Oriented Programming(Contd..)Objects

• They are instance of classes.

• Objects are the central idea behind OOP technology.

• An object is a bundle of variables and related methods.

• When an object is created memory allocation takes place.

Page 6: ASP.NET Session 3

Fundamentals of Object Oriented Programming(Contd..)

Three principles of object oriented programming

1.Encapsulation

• The ability to hide the internals details of an object from the outside world.

2.Inheritance

• Hierarchy is used to define more specialized classes based on a preexisting generalized class.

3.Polymorphism

• The ability to extend functionality.

Page 7: ASP.NET Session 3

Abstract Class: We can not create a object of abstract class. It only allows other classes to inherit from it but can't

be instantiated. In C# we use Abstract Keyword.

Interface An interface is not a class, is entity. An interface has no implementation; it only has the

signature. Just the definition of the methods without the body.

Page 8: ASP.NET Session 3

Partial Class Example• public partial class MyClass• {• partial void DoSomethingElse();• public void DoSomething()• { Console.WriteLine("DoSomething() execution

started.");• DoSomethingElse();• Console.WriteLine("DoSomething() execution

finished.");

Page 9: ASP.NET Session 3

• Console.ReadKey();• }• }• public partial class MyClass• {• partial void DoSomethingElse()• {• Console.WriteLine("DoSomethingElse()

called.");• }• }•

Page 10: ASP.NET Session 3

• class Program• {• • static void Main(string[] args)• {• MyClass mm = new MyClass();• mm.DoSomething();• }• }• }

Page 11: ASP.NET Session 3

OutPut

• DoSomething() execution started.• DoSomethingElse() called.• DoSomething() execution finished.

Page 12: ASP.NET Session 3

C# Language Fundamentals• Garbage collection automatically reclaims

memory occupied by unused objects.

• Exception handling provides a structured and extensible approach to error detection and recovery.

• Ex: try, catch & finally

• C# is case-sensitive.

Page 13: ASP.NET Session 3

C# Console Application • Console applications in C# have exactly the same purpose

as regular console applications: to provide a command line interface with the user.

• Console applications have three main input streams: standard in, standard out and standard error.

• “Hello World” Program using C#: using System; class Hello

{static void Main(String[] args)

{Console.WriteLine("Hello, World");

} }

Page 14: ASP.NET Session 3

C# Console Application in Visual Studio

Page 15: ASP.NET Session 3

C# Console Application(Contd..)

• The "System.Console" class:- The main element in a console application is the "System.Console" class. It contains all methods needed to control the three streams of data.

• "ReadLine" method:- The main ways of acquiring data from the standard input stream. "ReadLine" reads a whole line of characters from the buffer up to the point where the first end line character ("\n") is found. It outputs its data as "string“.

Page 16: ASP.NET Session 3

Working with Visual Studio

• Microsoft Visual Studio is an Integrated Development Environment (IDE) from Microsoft.

• It can be used to develop Console applications along with Windows Forms applications, web sites, web applications, and web services.

• Features :1. Code editor2. Debugger3. Designer4. Other Tools

Page 17: ASP.NET Session 3

Code editor

• Visual Studio includes a code editor that supports syntax highlighting and code completion using IntelliSense for not only variables, functions and methods but also language constructs like loops and queries.

• Autocomplete suggestions are popped up in a modeless list box, overlaid on top of the code editor.

Page 18: ASP.NET Session 3
Page 19: ASP.NET Session 3

Debugger

• Visual Studio includes a debugger that works both as a source-level debugger and as a machine-level debugger.

• It works with both managed code as well as native code and can be used for debugging applications written in any language supported by Visual Studio.

• The debugger allows setting breakpoints (which allow execution to be stopped temporarily at a certain position).

• The debugger supports Edit and Continue, i.e., it allows code to be edited as it is being debugged (32 bit only; not supported in 64 bit).

• When debugging, if the mouse pointer hovers over any variable, its current value is displayed in a tooltip ("data tooltips"), where it can also be modified if desired.

Page 20: ASP.NET Session 3
Page 21: ASP.NET Session 3

Designer

• Visual Studio includes a host of visual designers to aid in the development of applications. These tools include:

1. Windows Forms Designer

2. Web designer/development

3. Class designer

4. Data designer

Page 22: ASP.NET Session 3

Windows Forms Designer

• The Windows Forms designer is used to build GUI applications using Windows Forms.

• It includes a palette of UI widgets and controls (including buttons, progress bars, labels, layout containers and other controls) that can be dragged and dropped on a form surface.

• Layout can be controlled by housing the controls inside other containers or locking them to the side of the form.

• Controls that display data (like textbox, list box, grid view, etc.) can be data-bound to data sources like databases or queries.

• The designer generates either C# or VB.NET code for the application.

Page 23: ASP.NET Session 3
Page 24: ASP.NET Session 3

Web designer/development

• Visual Studio also includes a web-site editor and designer that allows web pages to be authored by dragging and dropping Web controls.

• It is used for developing ASP.NET applications and supports HTML, CSS and JavaScript.

• It uses a code-behind model to link with ASP.NET code.

Page 25: ASP.NET Session 3
Page 26: ASP.NET Session 3

Class designer

• The Class Designer is used to author and edit the classes.

• The Class Designer can generate C# and VB.NET code outlines for the classes and methods.

• It can also generate class diagrams from hand-written classes.

Page 27: ASP.NET Session 3
Page 28: ASP.NET Session 3

Data designer

• The data designer can be used to graphically edit typed database tables, primary and foreign keys and constraints.

• It can also be used to design queries from the graphical view.

Page 29: ASP.NET Session 3
Page 30: ASP.NET Session 3
Page 31: ASP.NET Session 3
Page 32: ASP.NET Session 3

Polymorphism in .Net

• What is Polymorphism?Polymorphism means same operation may behave differently on different classes.Example of Compile Time Polymorphism: Method OverloadingExample of Run Time Polymorphism: Method Overriding

Page 33: ASP.NET Session 3

Example of Compile Time Polymorphism

Method Overloading- Method with same name but with different arguments is called method overloading.- Method Overloading forms compile-time polymorphism.- Example of Method Overloading:class A1{void hello(){ Console.WriteLine(“Hello”); }

void hello(string s){ Console.WriteLine(“Hello {0}”,s); }}

Page 34: ASP.NET Session 3

Run Time Polymorphism

• Method Overriding- Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.- Method overriding forms Run-time polymorphism.- Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.- Example of Method Overriding:

Page 35: ASP.NET Session 3

• Class parent{virtual void hello(){ Console.WriteLine(“Hello from Parent”); }}

Class child : parent{override void hello(){ Console.WriteLine(“Hello from Child”); }}

static void main(){parent objParent = new child();objParent.hello();}//OutputHello from Child.

Page 36: ASP.NET Session 3

SummeryLanguage basics to facilitate development.

Basic Object Oriented Programming concepts

C# Programming Structure.

Language fundamentals

Doing our first C# Program.

Hands on knowledge in Visual Studio.