Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
288 views

Visual C# Programming

Here are the answers to the quiz questions in less than 3 lines each: 1. Attributes of variable: type, name, value 2. .Net architecture diagram: Applications, Class Libraries, CLR, OS/Services 3. Static libraries linked at compile time, dynamic libraries linked at runtime 4. Interface defines common methods, allows polymorphism 5. Delegate is a type-safe reference to a method 6. Static variables shared across instances, instance variables unique per instance 7. Message is data sent between components in messaging system
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
288 views

Visual C# Programming

Here are the answers to the quiz questions in less than 3 lines each: 1. Attributes of variable: type, name, value 2. .Net architecture diagram: Applications, Class Libraries, CLR, OS/Services 3. Static libraries linked at compile time, dynamic libraries linked at runtime 4. Interface defines common methods, allows polymorphism 5. Delegate is a type-safe reference to a method 6. Static variables shared across instances, instance variables unique per instance 7. Message is data sent between components in messaging system
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 54

Kohat University of Science and Technology

C# . Net
Spring 2006
Visual Programming
Lecture 02-03-04:
using C#

Oct 10-2006-Oct 17-2006

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)

Defines instructions for CLR


» MSIL code translated into machine code

Machine code for a


particular platform

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

• Other advantages of CLR


– Execution-management features
» Manages memory, security and other features
» Relieves programmer of many responsibilities
» More concentration on program logic

Amjad Mehmood
Lec.IIT 4
The Microsoft .Net Framework Architecture

Micosoft.Net Famework

Application Programming Model

ASP.NET Win Form

Class Lib,Data,debug tool etc

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…

• We can use this syntax to organize the namespaces in our


namespace definitions too, so the code above could also be
written:
• namespace Wrox.ProCSharp.Basics
• {
• class NamespaceExample
• {
• // Code for the class here...
• }
• }

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;
• }
• }

• CTS types contained within this namespace, as is much of .NET's core


functionality, such as console I/O.
Amjad Mehmood
Lec.IIT 8
Namespace Aliases
• Two or more name of the same namespace
• using alias = NamespaceName;
• using System;
• using Introduction = Wrox.ProCSharp.Basics;
• class Test
• {
• public static int Main()
• {
• Introduction.NamespaceExample NSEx = new
Introduction.NamespaceExample();
Console.WriteLine(NSEx.GetNamespace());
• 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

• Microsoft intermediate language


• CPU-independent language
• Set of instruction that can be effectively converted in
to native code
• Includes the instructions for
• loading, storing, initializing, and calling methods on
objects, arithmetic and logical operations ,control
flow, directly memory access and exception
handling.
• Can be converted to native code by JIT Compilation

Amjad Mehmood
Lec.IIT 15
JIT Compilation

• Converts MSIL to native code


• CPU-specific code that runs on the same computer
architecture that the JIT compiler is running on.

Amjad Mehmood
Lec.IIT 16
Quiz# 01
Time Allowed:10 Minutes
Marks:10

Note Please don’t write more than three lines

• What are the attributes of variable?


• Draw the diagram of .Net architecture
• What is difference between static and Dynamic libraries?
• Need and Use of interface?
• What is delegate?
• Difference between static and instance variable?
• What is Message?

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++ ;
}

public static void showcount( )


{
Console.WriteLine ( "Value of Count is: " + count ) ;
}
}

Amjad Mehmood
Lec.IIT 20
class Class1
{
static void Main ( string [ ] args )
{
sample s1 = new sample( ) ;
sample.showcount( ) ;

sample s2 = 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 ;
}

public static void showdate( )


{
Console.WriteLine ( "Year: " + y + " Month: " + m +
" Day: " + d ) ;
}
}
Amjad Mehmood
Lec.IIT 22
• class Class1
• {
• static void Main ( string [ ] args )
• {
• sample.showdate( ) ;
• }
• }
• }

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:

• public static int Main()


• {
• int j = 20;
• for (int i = 0; i < 10; i++)
• {
• int j = 30; // Can't do this - j is still in scope
• Console.WriteLine(j + i);
• }
• return 0;
• }

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

• const int a = 100; // This value cannot be changed


• once a value has been assigned, it can never be
overwritten.
• value of a constant must be computable at
compiletime
• Constants make it easier to avoid mistakes in your
programs.

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)

• long System.Int64 64-bit signed integer -9,223,372,036,854,775,808:


• float System.Single 32-bit single-precision floating- point 7±1.5 × 10-45 to ±3.4 × 1038
double System.Double 64-bit double-precision floating- point 15/16±5.0 × 10-324 to ±1.7 × 10308

• 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#

• Public class car


• {
– Int length;
– Int weight;
– Int wheels;
» Public Car()
» {}
» Public void Move()
» {}

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

• All the types In C# are directly or indirectly derived


from Object Type.

Amjad Mehmood
Lec.IIT 34
String

• The string type is directly inherited from the object


type.
• Sring is an alias for System.String class
• This is sealed type.

Amjad Mehmood
Lec.IIT 35
Conversion of the Type

Three ways to convert from one type to another:

• 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

• When a reference type is converted into value type then it is


knowing as unboxing

• 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#

• Modifiers are keywords used to specify the


declared accessibility of a member or a type.
• Access modifiers are
1. Public
2. Protected
3. Internal
4. Private

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 absDerived : abshello


• {
• 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 46
• using System;
• abstract class abshello
• {
• protected string s = "";
• public abstract void callhello();
• public abstract void sayhello();// not implemented in the derived
• //class.
• }

• class absDerived : abshello


• {
• 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 47
There are a few points to note about abstract
classes and methods:

• You cannot create objects of an abstract class.


• You cannot mark a constructor as abstract.
• You may use an abstract class to derive other classes,
including other abstract classes.
• You must override any abstract methods in a base class with
methods that contain code in a derived class.

Amjad Mehmood
Lec.IIT 48
Declaring Sealed Classes and Methods

• Sealed Classes

• cannot use a sealed class to derive a class.


• You mark a class or method as sealed using the sealed keyword. The following example
declares a sealed class:
• sealed public class MotorVehicle { ... }

• 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;

• sealed class abshello


• {
• protected string s = "";
• public virtual void callhello()
• {
• s="No Hello to C#";
• }
• }


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 absDerived : abshello


• {
• public override void callhello()
• {
• s="Hello C#";
• Console.WriteLine("{0}",s);
• }
• }

• 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

You might also like