Visual C# Programming
Visual C# Programming
C# . Net
Spring 2006
Visual Programming
Lecture 02-03-04:
using C#
Amjad Mehmood
Lec.IIT 1
.NET Framework and the
Common Language Runtime
• .NET Framework
– Heart of .NET strategy
» Manages and executes applications and Web services
» Provides security, memory management and other programming
capabilities
– Includes Framework class library (FCL)
» Pre-packaged classes ready for reuse
» Used by any .NET language
– Details contained in Common Language Specification
(CLS)
» Submitted to European Computer Manufacturers Association to
make the framework easily converted to other platforms
– Executes programs by Common Language Runtime (CLR)
Amjad Mehmood
Lec.IIT 2
.NET Framework and the
Common Language Runtime (II)
• Common Language Runtime (CLR)
– Central part of framework
» Executes Visual Basic .NET programs
– Compilation process
» Two compilations take place
» Programs compiled to Microsoft Intermediate Language
(MSIL)
Amjad Mehmood
Lec.IIT 3
.NET Framework and the
Common Language Runtime (III)
• Why two compilations?
– Platform independence
» .NET Framework can be installed on different platforms
» Execute .NET programs without any modifications to code
– Language independence
» .NET programs not tied to particular language
» Programs may consist of several .NET-compliant languages
» Old and new components can be integrated
Amjad Mehmood
Lec.IIT 4
The Microsoft .Net Framework Architecture
Micosoft.Net Famework
CLR,Assemblies,Security
Amjad Mehmood
Lec.IIT 5
Namespaces
• Logical grouping rather than physical groupings
• namespace Wrox
• {
• namespace ProCSharp
• {
• namespace Basics
• {
• class namespaceExample
• {
• // Code for the class here...
• }}}}
Amjad Mehmood
Lec.IIT 6
Namespaces Cont…
Amjad Mehmood
Lec.IIT 7
The using Statement
• say classes called NamespaceExample exist both in the
Wrox.ProCSharp.Basics and Wrox.ProCSharp.OOP namespaces. If we
then create a class called Test in the Wrox.ProCSharp namespace, and
instantiate one of the NamespaceExample classes in this class, we need to
specify which of these two classes we're talking about:
• using Wrox.ProCSharp;
• class Test
• {
• public static int Main()
• {
• Basics.NamespaceExample NSEx = new
Basics.NamespaceExample();
• return 0;
• }
• }
Amjad Mehmood
Lec.IIT 9
Console I/O
• Console.Write() - Writes the specified value to the console window
• Console.WriteLine() - Which does the same, but adds a new line character
at the end of the output.
• Example
• The following code lets the user input a line of text, and displays the first
character:
• int x = Console.Read();
• Console.WriteLine((char)x); This is similar, but returns the entire line of
text as a string:
• The following code lets the user input a line of text, and displays the first
character:
• string s = Console.ReadLine();
• Console.WriteLine(s);
Amjad Mehmood
Lec.IIT 10
Console I/O Cont…
• int i = 940;
• int j = 73;
• Console.WriteLine(" {0,4}\n+{1,4}\n ----\n {2,4}", i, j, i + j);
The result of this is:
• 940 + 73 ---- 1013
• StringDescription
• C Local currency format
• .D Decimal format. Converts an integer to base 10, and pads with leading zeros if a precision
specifier is given.
• E Scientific (exponential) format. The precision specifier sets the number of decimal places (6 by
default). The case of the format string (e or E) determines the case of the exponential symbol.
• F Fixed-point format; the precision specifier controls the number of decimal places. Zero is
acceptable.G General format. Uses E or F formatting, depending on which is the most compact.
• N Number format. Formats the number with commas as thousands separators, for example
32,767.44
• P Percent format.
• X Hexadecimal format. The precision specifier can be used to pad with leading zeros
Amjad Mehmood
Lec.IIT 11
• int i = 940;
• int j = 73;
• Console.WriteLine(" {0,4}\n+{1,4}\n ----\n {2,4}", i, j, i + j);
• The result of this is:
• 940
• + 73
----
1013
Example :02
• decimal i = 940.23m;
• decimal j = 73.7m;
• Console.WriteLine(" {0,9:C2}\n+{1,9:C2}\n ---------\n {2,9:C2}", i, j, i + j);
• The output of this in the United States is:
• $940.23
• + $73.70
• ---------
• $1,013.93
Amjad Mehmood
Lec.IIT 12
Our First C# Program
• using System;
• namespace Wrox.ProCSharp.Basics
• {
• class MyFirstCSharpClass
• {
• static void Main()
• {
• Console.WriteLine("This isn't at all like Java!");
• return;
• }
• }
• }
Amjad Mehmood
Lec.IIT 13
Execution Environment
• [C#/IL/CLR] [Java/Byte-Code/JVM]
Source MSIL
.Net Compiler
Code
Running
CLR
Program
Amjad Mehmood
Lec.IIT 14
MSIL
Amjad Mehmood
Lec.IIT 15
JIT Compilation
Amjad Mehmood
Lec.IIT 16
Quiz# 01
Time Allowed:10 Minutes
Marks:10
Amjad Mehmood
Lec.IIT 17
using System;
namespace Shape
{
class cone
{
float height, radius ;
public cone ( float h, float r )
{
height = h ;
radius = r ;
}
public void displaydata( )
{
Console.WriteLine ( "Height = " + height ) ;
Console.WriteLine ( "Radius = " + radius ) ;
}
public void volume( )
{
float v ;
v = ( 1 / 3.0f ) * 3.14f * radius * radius * height ;
Console.WriteLine ( "\nVolume = " + v ) ;
} Amjad Mehmood
} Lec.IIT 18
class Class1
{
static void Main ( string [ ] args )
{
cone c1 = new cone ( 10.0f, 3.5f ) ;
cone c2 = new cone ( 20.0f, 6.2f ) ;
c1.displaydata( ) ;
c1.volume( ) ;
c2.displaydata( ) ;
c2.volume( ) ;
}
}
}
Amjad Mehmood
Lec.IIT 19
using System;
namespace StaticDemo
{
class sample
{
private static int count ;
public sample( )
{
count++ ;
}
Amjad Mehmood
Lec.IIT 20
class Class1
{
static void Main ( string [ ] args )
{
sample s1 = new sample( ) ;
sample.showcount( ) ;
Amjad Mehmood
Lec.IIT 21
using System;
namespace StaticDemo
{
class sample
{
public static int y;
public static int m ;
public static int d ;
static sample( )
{
DateTime dt = DateTime.Now ;
y = dt.Year ;
m = dt.Month ;
d = dt.Day ;
}
Amjad Mehmood
Lec.IIT 23
Scope clashes for local variable
• using System;
• namespace Wrox.ProCSharp.Basics
• {
• public class ScopeTest
• {
• public static int Main()
• {
• for (int i = 0; i < 10; i++)
• {
• Console.WriteLine(i); } // i goes out of scope here We can declare a variable named
i again, because // there's no other variable with that name in scope
• for (int i = 9; i >= 0; i--)
• {
• Console.WriteLine(i);
• } // i goes out of scope here
• return 0;
• }
• }
• }
Amjad Mehmood
Lec.IIT 24
If we try to compile this, we'll get an error:
Amjad Mehmood
Lec.IIT 25
Scope clashes Cont…
• using System;
• namespace Wrox.ProCSharp.Basics
• {
• class ScopeTest2
• {
• static int j = 20;
• public static void Main()
• {
• int j = 30;
• Console.WriteLine(j);
• return;
• }
• }
• }
• C# makes a fundamental distinction between variables that are declared at
the type level (fields), and variables declared within methods (local
variables): Main() method hides the class-level variable with the same name, so when we
run this code, the number 30 will be displayed.
Amjad Mehmood
Lec.IIT 26
Scope clashes Cont…
• ...
• public static void Main()
• {
• int j = 30;
• Console.WriteLine(ScopeTest2.j);
• }
• ...
Amjad Mehmood
Lec.IIT 27
Constants
Amjad Mehmood
Lec.IIT 28
Predefined Data Types
• Value Types
• Stores the value directly
• stored in different places in memory; value types in an area known as the
stack
• Int, char, double
• Reference Types
• Stores a reference to the value.
• Reference types are stored in an area known as the managed heap
• Class
• Interface
• Delegates
• Object
• String
Amjad Mehmood
Lec.IIT 29
Predefined Data Types Cont…
• The basic predefined types recognized by C# are not intrinsic (native) to the language but part
of the .NET Framework
• Name CTS Type Description Range (min:max)
• sbyte System.SByte 8-bit signed integer -128:127 (-27:27-1)
• short System.Int16 16-bit signed integer -32,768:32,767 (-215:215-1)
• int System.Int32 32-bit signed integer -2,147,483,648:2,147,483,647 (-231:231-1)
• decimal System.Decimal 128-bit high precision decimal notation 28±1.0 × 10-28 to ±7.9 × 1028
• bool System.Boolean true or false
Amjad Mehmood
Lec.IIT 30
A simple car class in C#
Amjad Mehmood
Lec.IIT 31
Interface
• Interface interfaceCar
• {
• Void Move();
• Void Stop();
• }
• Class Car1:interfaceCar
• {
• Void Move()
• {
• //wirte code
• }
• Void Stop()
• {
• //write code here
• }
• }
Amjad Mehmood
Lec.IIT 32
Delegate
• namespace Example
• {
• using System;
• /// <summary>
• /// Delegatge
• /// </summary>
• delegate void FirstDelegate();//declare the delegate
• public class Delegate1
• {
• public static void hello()
• {
• Console.WriteLine("Hello Delegate");
• }
• public static int Main(string[] args)
• {
• FirstDelegate fd=new FirstDelegate(hello);// Instantiation
• fd();// invocation of the delgate
• return 0;
• }
• }
Amjad Mehmood
• } Lec.IIT 33
Object
Amjad Mehmood
Lec.IIT 34
String
Amjad Mehmood
Lec.IIT 35
Conversion of the Type
• Implicit conversion
• Explicit conversion via casting
• Use of a conversion method
Amjad Mehmood
Lec.IIT 36
Implicit conversion
• using System;
• public class testConversion1
• {
• public static void Main()
• {
• int i=7;
• long l=i;
• Console.WriteLine("The long is converted to int {0}",i);
• }
• }
Amjad Mehmood
Lec.IIT 37
Implicit conversion
• double F;
• int X = 2;
• F = X; // implicit conversion
Amjad Mehmood
Lec.IIT 38
Explicit conversion via casting
• using System;
• public class testConversion1
• {
• public static void Main()
• {
• long l=2.0;
• int i=l;
• Console.WriteLine("The long is converted to int {0}",i);
• }
• }
Amjad Mehmood
Lec.IIT 39
Explicit conversion via casting
• {
• byte b=90,c=45;
• byte d=b+c;
• Console.writeLine(“result is{0}”, d);
• }
• Solution
• byte d=(byte) b+c;
• Console.writeLine(“result is{0}”, d);
• Error messege
Amjad Mehmood
Lec.IIT 40
Use of a conversion method
• Sytem.Convert.ToString
• Sytem.Convert.ToInt
• Sytem.Convert.ToBoolean
• string s = i.ToString();
Amjad Mehmood
Lec.IIT 41
Boxing and Unboxing
• When a value type is converted into a reference type then it is
known as boxing
• Int b=145;
• Object o=b;//Boxing (implicit converted into object)
• Int j=(int) o;//UnBoxing(explicitly converted into int)
• Console.writeLine(“result is{0}”, j);
Amjad Mehmood
Lec.IIT 42
Modifiers in C#
Amjad Mehmood
Lec.IIT 43
•
internal
using System;
• internal class myFirst
• {
• public void hello()
• {
• Console.WriteLine("Hello C#");
• }
• }
• public class Mainclass
• {
• public static void Main()
• {
• myFirst m=new myFirst();
• m.hello();
• }
• }
Amjad Mehmood
Lec.IIT 44
Other modifiers are
• Abstract
• Sealed
• Virtual
• New
• Override
• Static
Amjad Mehmood
Lec.IIT 45
Using Abstract Classes and Methods
• using System;
• abstract class abshello
• {
• protected string s = "";
• public abstract void callhello();
• }
• class mainclass
• {
• public static void Main()
• {
• absDerived ad =new absDerived();
• ad.callhello();
• }
• }
Amjad Mehmood
Lec.IIT 46
• using System;
• abstract class abshello
• {
• protected string s = "";
• public abstract void callhello();
• public abstract void sayhello();// not implemented in the derived
• //class.
• }
• class mainclass
• {
• public static void Main()
• {
• absDerived ad =new absDerived();
• ad.callhello();
• }
• }
Amjad Mehmood
Lec.IIT 47
There are a few points to note about abstract
classes and methods:
Amjad Mehmood
Lec.IIT 48
Declaring Sealed Classes and Methods
• Sealed Classes
• Sealed Methods
• cannot override a sealed method in a derived class. may declare a sealed method
in a non-sealed class, but that sealed method cannot be overridden in a derived
class.
• e.g sealed public override void Accelerate( )
Amjad Mehmood
Lec.IIT 49
Sealed Classes
• using System;
•
class absDerived : abshello // Error
• {
• public override void callhello()
• {
• s="Hello C#";
• Console.WriteLine("{0}",s);
• }
• }
• class mainclass
• {
• public static void Main()
• {
• absDerived ad =new absDerived();
• ad.callhello();
•
Amjad Mehmood
}
Lec.IIT 50
Override
• using System;
• class abshello
• {
• protected string s = "";
• public void callhello()
• {
• s="No Hello to C#";
• }
• }
• class mainclass
• {
• public static void Main()
• {
• Amjad
absDerived ad =new Mehmood
absDerived();
• ad.callhello(); Lec.IIT 51
• using System; Static
• class absDerived {
• string s="Hello C#";
• public static void callhello()
• {
• Console.WriteLine("{0}",s);
• }
• public void normal()
• {
• Console.WriteLine("{0}",s);
• }
• }
• class mainclass
• {
• public static void Main()
• {
• absDerived.callhello();
• absDerived a=new absDerived();
• a.normal();
• }
Amjad Mehmood
• } Lec.IIT 52
Virtual
• using System;
• class shibi
• {
• public void hand() { Console.WriteLine("shibi's hand"); }
• public virtual void eye() { Console.WriteLine("shibi's eye"); }
• }
• class myson: shibi
• {
• public void hand() { Console.WriteLine("myson's hand"); }
• public override void eye() { Console.WriteLine("myson's eye"); }
• }
• class Test
• {
• static void Main() {
• shibi s=new shibi();
• myson m = s;
• s.hand();
• m.hand();
• s.eye();
• m.eye();
• }
• }
• whenever you call the virtual method it will search for the overriden method in the deri
Amjad Mehmood
Lec.IIT 53
Assignment
• Upcasting
• Downcasting
• extern The method is implemented externally, in a different language.
• Override the string functions
– Tostring
Amjad Mehmood
Lec.IIT 54