• Effort by :
Name Enrollment no.
• Dumasia Yazad H. 140420107015
• Gajjar Karan 140420107016
• Jinay Shah 140420107021
• Jay Pachchigar 140420107027
1
Computer (Shift-1) 6th year
C# .NET
LANGUAGE FEATURES AND CREATING .NET PROJECTS,
NAMESPACES CLASSES AND INHERITANCE , EXPLORING
THE BASE CLASS LIBRARY -, DEBUGGING AND ERROR
HANDLING , DATA TYPES
2
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++ etc
6.Source code Intermediate Language (IL) (JIT Compiler) Native code
7.Class and Data types are common to .NET Language
8.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 3
Language Features And creating .NET project
Windows Store App
Windows
Client
Office
Enterprise
WebsitesComponents
Mobile Apps
Backend
Service
4
Namespaces
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.
5
using 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_cl
second_space.namespace_cl sc = new second_space.namespace_cl
fc.func();
sc.func();
Console.ReadKey(); } }
Inside first space
Inside second space
Output:
Demonstrates use of namespaces:
6
Class 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)
7
Example of Class
public class Person{// Field
public string name;
// Constructor that takes no arguments.
public Person(){
name = "unknown"; }
// Constructor that takes one argument.
public Person(string nm)
{ name = nm; }
// Method
public 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 Pachchigar
Yazad Dumasia
8
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
9
C# 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(); } } }
10
Use Base keyword in inheritance
class 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=6
Derived class i=6
Base class i=5
11
Exploring 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.
12
Exploring 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.
13
Exception 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.
14
Syntax 15
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
16
class 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();
} }
17
User Defined Exception
using System;
namespace ExceptionHandling {
class NegativeNumberException:ApplicationException
{
public NegativeNumberException(string message)
// show message
}
}
if(value<0)
throw new NegativeNumberException(" Use Only Positive
numbers");
18
THE COMMON TYPE SYSTEM (CTS)
String Array ValueType Exception Delegate Class1
Multicast
Delegate
Class2
Class3
Object
Enum1
Structure1Enum
Primitive types
Boolean
Byte
Int16
Int32
Int64
Char
Single
Double
Decimal
DateTime
System-defined types
User-defined types
Delegate1
TimeSpan
Guid
19
Not all languages support all CTS types and features
C# supports unsigned integer types, VB.NET does not
C# is case sensitive, VB.NET is not
C# supports pointer types (in unsafe mode), VB.NET does not
C# supports operator overloading, VB.NET does not
CLS was drafted to promote language interoperability
vast majority of classes within FCL are CLS-compliant
20
MAPPING C# TO
CTSLanguage keywords map to common CTS classes:
Keyword Description Special format for literals
bool Boolean true false
char 16 bit Unicode character 'A' 'x0041' 'u0041'
sbyte 8 bit signed integer none
byte 8 bit unsigned integer none
short 16 bit signed integer none
ushort 16 bit unsigned integer none
int 32 bit signed integer none
uint 32 bit unsigned integer U suffix
long 64 bit signed integer L or l suffix
ulong 64 bit unsigned integer U/u and L/l suffix
float 32 bit floating point F or f suffix
double 64 bit floating point no suffix
decimal 128 bit high precision M or m suffix
string character sequence "hello", @"C:dirfile.txt"
21
EXAMPLE
• 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
22
BOXING 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!
23
USER-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; }
}
24
25

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 1 Computer (Shift-1) 6th year
  • 2.
    C# .NET LANGUAGE FEATURESAND CREATING .NET PROJECTS, NAMESPACES CLASSES AND INHERITANCE , EXPLORING THE BASE CLASS LIBRARY -, DEBUGGING AND ERROR HANDLING , DATA TYPES 2
  • 3.
    Some of thefeature 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++ etc 6.Source code Intermediate Language (IL) (JIT Compiler) Native code 7.Class and Data types are common to .NET Language 8.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 3
  • 4.
    Language Features Andcreating .NET project Windows Store App Windows Client Office Enterprise WebsitesComponents Mobile Apps Backend Service 4
  • 5.
    Namespaces A namespace isdesigned 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. 5
  • 6.
    using System; namespace first_space { classnamespace_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_cl second_space.namespace_cl sc = new second_space.namespace_cl fc.func(); sc.func(); Console.ReadKey(); } } Inside first space Inside second space Output: Demonstrates use of namespaces: 6
  • 7.
    Class And Inheritance Classesin 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) 7
  • 8.
    Example of Class publicclass Person{// Field public string name; // Constructor that takes no arguments. public Person(){ name = "unknown"; } // Constructor that takes one argument. public Person(string nm) { name = nm; } // Method public 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 Pachchigar Yazad Dumasia 8
  • 9.
    Inheritance is theability 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 9
  • 10.
    C# does notsupport 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(); } } } 10
  • 11.
    Use Base keywordin inheritance class 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=6 Derived class i=6 Base class i=5 11
  • 12.
    Exploring the BaseClass 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. 12
  • 13.
    Exploring the BaseClass 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. 13
  • 14.
    Exception Handling An exceptionis 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. 14
  • 15.
  • 16.
    C# exceptions arerepresented 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 16
  • 17.
    class Program { publicstatic 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(); } } 17
  • 18.
    User Defined Exception usingSystem; namespace ExceptionHandling { class NegativeNumberException:ApplicationException { public NegativeNumberException(string message) // show message } } if(value<0) throw new NegativeNumberException(" Use Only Positive numbers"); 18
  • 19.
    THE COMMON TYPESYSTEM (CTS) String Array ValueType Exception Delegate Class1 Multicast Delegate Class2 Class3 Object Enum1 Structure1Enum Primitive types Boolean Byte Int16 Int32 Int64 Char Single Double Decimal DateTime System-defined types User-defined types Delegate1 TimeSpan Guid 19
  • 20.
    Not all languagessupport all CTS types and features C# supports unsigned integer types, VB.NET does not C# is case sensitive, VB.NET is not C# supports pointer types (in unsafe mode), VB.NET does not C# supports operator overloading, VB.NET does not CLS was drafted to promote language interoperability vast majority of classes within FCL are CLS-compliant 20
  • 21.
    MAPPING C# TO CTSLanguagekeywords map to common CTS classes: Keyword Description Special format for literals bool Boolean true false char 16 bit Unicode character 'A' 'x0041' 'u0041' sbyte 8 bit signed integer none byte 8 bit unsigned integer none short 16 bit signed integer none ushort 16 bit unsigned integer none int 32 bit signed integer none uint 32 bit unsigned integer U suffix long 64 bit signed integer L or l suffix ulong 64 bit unsigned integer U/u and L/l suffix float 32 bit floating point F or f suffix double 64 bit floating point no suffix decimal 128 bit high precision M or m suffix string character sequence "hello", @"C:dirfile.txt" 21
  • 22.
    EXAMPLE • An exampleof 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 22
  • 23.
    BOXING 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! 23
  • 24.
    USER-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; } } 24
  • 25.