C# language
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.
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.
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.
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.
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.
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.
Partial Class Example• public partial class MyClass• {• partial void DoSomethingElse();• public void DoSomething()• { Console.WriteLine("DoSomething() execution
started.");• DoSomethingElse();• Console.WriteLine("DoSomething() execution
finished.");
• Console.ReadKey();• }• }• public partial class MyClass• {• partial void DoSomethingElse()• {• Console.WriteLine("DoSomethingElse()
called.");• }• }•
• class Program• {• • static void Main(string[] args)• {• MyClass mm = new MyClass();• mm.DoSomething();• }• }• }
OutPut
• DoSomething() execution started.• DoSomethingElse() called.• DoSomething() execution finished.
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.
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");
} }
C# Console Application in Visual Studio
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“.
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
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.
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.
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
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.
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.
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.
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.
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
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); }}
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:
• 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.
•
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.