25
Effort by : Name Enrollment no. Dumasia Yazad H. 140420107015 Gajjar Karan 140420107016 Jinay Shah 140420107021 Jay Pachchigar 140420107027 Sarvajanik College of Engineering & Technology 1 Computer (Shift-1) 6 th year

C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

Embed Size (px)

Citation preview

Page 1: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

1

•Effort by : Name Enrollment no.

• Dumasia Yazad H. 140420107015• Gajjar Karan 140420107016• Jinay Shah 140420107021• Jay Pachchigar 140420107027

Sarvajanik College of Engineering & Technology

Computer (Shift-1) 6th year

Page 2: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

2

C# .NETLANGUAGE FEATURES AND CREATING .NET PROJECTS, NAMESPACES CLASSES AND INHERITANCE , EXPLORING THE BASE CLASS LIBRARY -, DEBUGGING AND ERROR HANDLING , DATA TYPES

Page 3: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

3

Some of the feature of Visual C# in .NET are:1.Simple modern, Object Oriented Language 2.Aims to combine high productivity of Visual Basic and the raw power of C++.3.Common Execution engine and rich class libraries 4.MS JVM equivalent is Common Language Run time(CLR)5.CLR accommodates more then one language such C#,ASP,C++ etc6.Source code Intermediate Language (IL) (JIT Compiler) Native code7.Class and Data types are common to .NET Language8.Develop Console applications, Windows applications & web app using C#9.In C#, MS has taken care of C++ problem like memory management, pointers, etc.10.It supports Garbage collection, Automatic memory management and a lot.

Language Features And creating .NET project

Page 4: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

4Language Features And creating .NET project

Windows Store App

Windows Client

Office

Enterprise

WebsitesComponents

Mobile Apps

BackendService

Page 5: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

5Namespaces

A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.

Page 6: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

6using System;namespace first_space{class namespace_cl {public void func(){Console.WriteLine("Inside first_space"); } } }

namespace second_space {class namespace_cl {public void func() {Console.WriteLine("Inside second_space"); } } }

class TestClass {staticc void Main(string[] args){first_space.namespace_cl fc = new first_space.namespace_clsecond_space.namespace_cl sc = new second_space.namespace_clfc.func();sc.func();Console.ReadKey(); } }

Inside first spaceInside second space

Output:

Demonstrates use of namespaces:

Page 7: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

7Class And Inheritance

Classes in C# allow single inheritance and multiple interface inheritance. Each class can contain methods, properties, events, indexers, constants, constructors, destructors, operators and members can be static (can be accessed without an object instance) or instance member (require you to have a reference to an object first)

Also, the access can be controlled in four different levels: public (everyone can access), protected (only inherited members can access), private (only members of the class can access) and internal (anyone on the same EXE or DLL can access)

Page 8: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

8Example of Classpublic class Person{// Fieldpublic string name;// Constructor that takes no arguments.public Person(){name = "unknown"; }// Constructor that takes one argument.public Person(string nm){ name = nm; }// Methodpublic void SetName(string newName){ name = newName; }}class TestPerson{static void Main(){// Call the constructor that has no parameters.Person person1 = new Person();Console.WriteLine(person1.name);person1.SetName(“Jinay Shah");

Console.WriteLine(person1.name);// Call the constructor that has one parameter.Person person2 = new Person(“Karan Gajjar");Console.WriteLine(person2.name);Person person3 = new Person(“Jay Pachchigar");Console.WriteLine(person2.name);Person person4 = new Person(“Yazad Dumasia");Console.WriteLine(person2.name);// Keep the console window open in debug mode.Console.WriteLine("Press any key to exit.");Console.ReadKey();}}Output: unknown Jinay Shah Karan Gajjar Jay PachchigarYazad Dumasia

Page 9: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

9

Inheritance is the ability to create a class from another class, the "parent" class, extending the functionality and state of the parent in the derived, or "child" class. It allows derived classes to overload methods from their parent class. Inheritance is one of the pillars of object-orientation.

Important characteristics of inheritance include:

1. A derived class extends its base class. That is, it contains the methods and data of its parent class, and it can also contain its own data members and methods.2. The derived class cannot change the definition of an inherited member.3. Constructors and destructors are not inherited. All other members of the base class are inherited.4. The accessibility of a member in the derived class depends upon its declared accessibility in the base class.5. A derived class can override an inherited member.

Class Inheritance

Page 10: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

10C# does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance. The following program demonstrates this:

Multiple Inheritance in C#

using System;namespace InheritanceApplication { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; }

// Base class PaintCost public interface PaintCost { int getCost(int area); } // Derived class class Rectangle : Shape, PaintCost { public int getArea() { return (width * height); } public int getCost(int area) { return area * 70; } }

class RectangleTester { static void Main(string[] args) { Rectangle Rect = new Rectangle(); int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); Console.WriteLine("Total area: {0}", Rect.getArea()); Console.WriteLine("Total paint cost: Rs {0}" , Rect.getCost(area)); Console.ReadKey(); } } }

Page 11: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

11Use Base keyword in inheritanceclass A { int i; A(int n, int m) { x = n; y = m Console.WriteLine("n="+x+"m="+y); }}class B:A { int i; B(int a, int b):base(a,b)//calling base //class constructor and passing value { base.i = a;//passing value to base class field i = b; }

public void Show() { Console.WriteLine("Derived class i="+i); Console.WriteLine("Base class i="+base.i); }}class MainClass { static void Main(string args [] ) { B b=new B(5,6);//passing value to derive class constructor b.Show(); }} OUTPUT

n=5m=6Derived class i=6Base class i=5

Page 12: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

12Exploring the Base Class Library

Within the Microsoft .NET framework, there is a component known…as the base class library,And this is essentially library of classes, interfaces, and value types that you can. Use to provide common functionality in all of your .NET applications.The base class library is divided into namespaces to make locating that functionality easier.

We've seen examples of using some of that functionality from the using directives that exist at the top of our Program.cs file.We bring in namespaces such as…System, and system.Collections.Generic, System.Text, and System.Threading.Tasks.

Page 13: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

13Exploring the Base Class Library

And within these namespaces exist classes that perform certain functionality that are related to the namespaces.

As an example, let's take a look at what…exists in the System.Text namespace for functionality or for classes. Now in order to do that, one of the…simplest ways is to bring up our Object Browser window.…If we click on the View menu, we can see that there's…a window called Object Browser, and the shortcut key combination is Ctrl+ALT+J.

Page 14: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

14Exception Handling

An exception is a problem that arises during the execution of a program. C# exception handling is built upon four keywords:o Try: A try block identifies a block of code for which particular

exceptions will be activated. It's followed by one or more catch blocks.

o Catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

o Finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown.

For example, if you open a file, it must be closed whether an exception is raised or not. o Throw: A program throws an exception when a problem shows up.

This is done using a throw keyword.

Page 15: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

15Syntax

Page 16: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

16

C# exceptions are represented by classes. The exception classes in C# are mainly directly or indirectly derived from the System.Exception class.Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemException classes

Page 17: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

17class Program { public static void division(int num1, int num2) { float result=0.0f; try { result = num1 / num2; } catch (DivideByZeroException e) { Console.WriteLine("Exception Error !! \n divid by zero !!"); // Console.WriteLine("Exception caught: {0}", e); } finally { Console.WriteLine("Result: {0} ", result); } } static void Main(string[] args) { division(10,0); Console.ReadLine(); } }

Page 18: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

18User Defined Exceptionusing System;namespace ExceptionHandling { class NegativeNumberException:ApplicationException { public NegativeNumberException(string message) // show message }}

if(value<0) throw new NegativeNumberException(" Use Only Positive numbers");

Page 19: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

19THE COMMON TYPE SYSTEM (CTS)

St r i ng Ar r ay Val ueType Except i on Del egat e Cl ass1

Mul t i castDel egat e Cl ass2

Cl ass3

Obj ect

Enum1

St r uct ur e1EnumPr i mi t i ve t ypes

Bool ean

Byt e

I nt 16

I nt 32

I nt 64

Char

Si ngl e

Doubl e

Deci mal

Dat eTi me

System-defined types

User-defined types

Del egat e1

Ti meSpan

Gui d

Page 20: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

20

Not all languages support all CTS types and featuresC# supports unsigned integer types, VB.NET does notC# is case sensitive, VB.NET is notC# supports pointer types (in unsafe mode), VB.NET does notC# supports operator overloading, VB.NET does not

CLS was drafted to promote language interoperabilityvast majority of classes within FCL are CLS-compliant

Page 21: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

21MAPPING C# TO CTSLanguage keywords map to common CTS classes:

Keyword Description Special format for literalsbool Boolean true falsechar 16 bit Unicode character 'A' '\x0041' '\u0041'sbyte 8 bit signed integer nonebyte 8 bit unsigned integer noneshort 16 bit signed integer noneushort 16 bit unsigned integer noneint 32 bit signed integer noneuint 32 bit unsigned integer U suffixlong 64 bit signed integer L or l suffixulong 64 bit unsigned integer U/u and L/l suffixfloat 32 bit floating point F or f suffixdouble 64 bit floating point no suffixdecimal 128 bit high precision M or m suffixstring character sequence "hello", @"C:\dir\file.txt"

Page 22: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

22EXAMPLE• An example of using types in C#

• declare before you use (compiler enforced)• initialize before you use (compiler enforced)

public class App{ public static void Main() { int width, height; width = 2; height = 4;

int area = width * height;

int x; int y = x * 2; ... }}

declarations

decl + initializer

error, x not set

Page 23: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

23BOXING AND UNBOXING• When necessary, C# will auto-convert value <==> object

• value ==> object is called "boxing"• object ==> value is called "unboxing"

int i, j;object obj;string s;

i = 32;obj = i; // boxed copy!i = 19;j = (int) obj; // unboxed!

s = j.ToString(); // boxed!s = 99.ToString(); // boxed!

Page 24: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

24USER-DEFINED REFERENCE TYPES

• Classes!• for example, Customer class we worked with earlier…

public class Customer{ public string Name; // fields public int ID;

public Customer(string name, int id) // constructor { this.Name = name; this.ID = id; }

public override string ToString() // method { return "Customer: " + this.Name; }}

Page 25: C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and Inheritance , Exploring the Base Class Library -, Debugging and Error Handling , Data Types

25