Srgoc dotnet_new

28
CS/CE/ME/IT-406 Computer Programming (.Net Technologies) Index S.No. Practical’s Date Remarks Signature 1. Explain the architecture of .NET Framework. 2. Explain the features of C#. 3. WAP in C# to implement hello world program 4. WAP in c# to implement inheritance program 5. WAP in c# to implement polymorphism 6. Wap in c# to implement delegates in c# 7. WAP in c# to implement Constructors 8. Explain Exception handling with example using c# 9. WAP in c# to implement file I/O 10. Write the code to add a flash item on your website. 11. Explain XML and DTD

Transcript of Srgoc dotnet_new

Page 1: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)Index

S.No. Practical’s Date Remarks Signature

1. Explain the architecture of .NET Framework.

2. Explain the features of C#.

3. WAP in C# to implement hello world program

4. WAP in c# to implement inheritanceprogram

5. WAP in c# to implement polymorphism

6. Wap in c# to implement delegates in c#

7. WAP in c# to implement Constructors

8. Explain Exception handling with example using c#

9. WAP in c# to implement file I/O

10. Write the code to add a flash item on your website.

11.Explain XML and DTD

Page 2: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

1. Explain the Architecture Of .NET Framework.Ans: .NET Framework Architecture: The .NET Framework Is A New And Revolutionary Platform Created By Microsoft For Developing Applications.  A Programming Infrastructure Created for Building, Deploying, And Running Applications and Services That Use .NET Technologies, Such As Desktop Applications and Web services.

Fig: .Net Framework Architecture

Components of .NET Architecture

Microsoft .NET consists of four Major Components:

1. Common Language Specification (CLS) – blue in the diagram below2. Framework Class Library (FCL) – red3. Common Language Runtime (CLR) – green4. .NET Tools – yellow

Common Language Specification (CLS)

The CLS is a common platform that integrates code and components from multiple .NET programming languages.

In other words, a .NET application can be written in multiple programming languages with no extra work by the developer 

Page 3: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

.Net Languages:

• .Net Includes New Object-Oriented Programming Languages Such As C#, Visual Basic .Net, J# (A Java Clone) And Managed C++.

• These Languages, Plus Other Experimental Languages Like F#, All Compile To The Common Language Specification And Can Work Together In The Same Application.

Framework Class Library (FCL): The FCL is a collection of over 7000 classes and data types that enable .NET applications to read and write files, access databases, process XML, display a graphical user interface, draw graphics, use Web services, etc.

The FCL wraps much of the massive, complex Win32 API into simpler .NET objects that can be used by C# and other .NET programming languages.

Fig: Framework class library

Page 4: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

Common Language Runtime (CLR)

The CLR is the execution engine for .NET applications and serves as the interface between .NET applications and the operating system.

The CLR provides many services such as: Loads and executes code Converts intermediate language to native machine code Separates processes and memory Manages memory and objects Enforces code and access security Handles exceptions Interfaces between managed code, COM objects, and DLLs Provides type-checking Provides code meta data (Reflection) Provides profiling, debugging, etc.

Fig: CLR (common language runtime)

Fig: CLR Execution Model

Page 5: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

.NET Tools:

Visual studio .net is Microsoft’s flagship tool for developing windows software. visual studio provides an integrated development environment (IDE) for developers to create standalone windows applications, interactive web sites, web applications, and web services running on any platform that supports .net.In addition, there are many .net framework tools designed to help developers create, configure, deploy, manage and secure .net applications and components.

Fig: .net tools

Page 6: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)2. Explain The Features Of C#.

Ans:

The following reasons make C# a widely used professional language:

It is a modern, general-purpose programming language It is object oriented. It is component oriented. It is easy to learn. It is a structured language. It produces efficient programs. It can be compiled on a variety of computer platforms. It is a part of .Net Framework.

Strong Programming Features of C# Although C# constructs closely follow traditional high-level languages, C and C++ and being an Object oriented programming language. It has strong resemblance with Java, it has numerous strong programming features that make it endearing to a number of programmers worldwide.

Following is the list of few important features of C#: Portability Namespace Libraries Boolean Conditions Automatic Garbage Collection Standard Library Assembly Versioning Properties and Events Delegates and Events Management Easy-to-use Generics Indexers Conditional Compilation Simple Multithreading LINQ and Lambda Expressions Integration with Windows

3. WAP in C# to implement hello world program Ans: A C# program consists of the following parts:

Page 7: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies) Namespace declaration A class Class methods Class attributes A Main method Statements and Expressions Comments

Let us look at a simple code that prints the words "Hello World":

using System ;nam espace HelloWorldApplication{class HelloWorld{static void Main(string[] args){/* m y first program in C# * /Console.WriteLine("HelloWorld");Console.ReadKey();}}}

When this code is compiled and executed, it produces the following result:

Let us look at the various parts of the given program: The first line of the program using System; - the using keyword is used to include the

System namespace in the program. A program generally has multiple using statements.

The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.

The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.

The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.

Page 8: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies) The next line /*...*/ is ignored by the compiler and it is put to add comments in the program. The Main method specifies its behavior with the statement Console.WriteLine("Hello World"); WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" to be displayed on the screen.

The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

It is worth to note the following points: C# is case sensitive. All statements and expression must end with a semicolon (;). The program execution starts at the Main method. Unlike Java, program file name could be different from the class name.

3. Wap In C# To Implement Inheritance

using System;namespace InheritanceApplication

Page 9: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies){ class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; }

// Derived class class Rectangle: Shape { public int getArea() { return (width * height); } }class RectangleTester{static void Main(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. Console.WriteLine("Total area: {0}", Rect.getArea()); Console.ReadKey(); } }}

OUTPUT

Page 10: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

4.WAP in c# to implement polymorphism

Page 11: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)using System;namespace PolymorphismApplication{ class Printdata { void print(int i) { Console.WriteLine("Printing int: {0}", i ); }

void print(double f) { Console.WriteLine("Printing float: {0}" , f); }

void print(string s) { Console.WriteLine("Printing string: {0}", s); } static void Main(string[] args) { Printdata p = new Printdata(); // Call print to print integer p.print(5); // Call print to print float p.print(500.263); // Call print to print string p.print("Hello C++"); Console.ReadKey(); } }}

OUTPUT

Page 12: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

5.Wap in c# to implement delegates in c#

Page 13: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.

Declaring Delegates

Delegate declaration determines the methods that can be referenced by the delegate. A delegate can refer to a method, which have the same signature as that of the delegate.

For example, consider a delegate:

public delegate int MyDelegate (string s);

The preceding delegate can be used to reference any method that has a single string parameter and returns an int type variable.

Syntax for delegate declaration is:

delegate <return type> <delegate-name> <parameter list>

using System;

delegate int NumberChanger(int n);namespace DelegateAppl{ class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; }

public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; }

static void Main(string[] args) { //create delegate instances NumberChanger nc1 = new NumberChanger(AddNum);

Page 14: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies) NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objects nc1(25); Console.WriteLine("Value of Num: {0}", getNum()); nc2(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } }}

OUTPUT

6.WAP in c# to implement Constructors

Page 15: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor.  

using System;namespace DefaultConstractor {    class addition    {        int a, b;         public addition()   //default contructor        {            a = 100;            b = 175;        }         public static void Main()        {            addition obj = new addition(); //an object is created , constructor is called            Console.WriteLine(obj.a);            Console.WriteLine(obj.b);            Console.Read();        }      }    }

OUTPUT

Page 16: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)7. Explain Exception handling with example using c#

An exception is a problem that arises during the execution of a program. A C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally and throw.

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.

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.

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.

throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

Syntaxtry{ // statements causing exception}catch( ExceptionName e1 ){ // error handling code}catch( ExceptionName e2 ){ // error handling code}catch( ExceptionName eN ){ // error handling code}finally{ // statements to be executed}

using System;namespace ErrorHandlingApplication{ class DivNumbers { int result; DivNumbers() {

Page 17: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies) result = 0; } public void division(int num1, int num2) { try { result = num1 / num2; } catch (DivideByZeroException e) { Console.WriteLine("Exception caught: {0}", e); } finally { Console.WriteLine("Result: {0}", result); }

} static void Main(string[] args) { DivNumbers d = new DivNumbers(); d.division(25, 0); Console.ReadKey(); } }}

OUTPUT

Page 18: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

8.WAP in c# to implement file I/O

Page 19: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

C# I/O Classes

The System.IO namespace has various class that are used for performing various operation with files, like creating and deleting files, reading from or writing to a file, closing a file etc.

The following table shows some commonly used non-abstract classes in the System.IO namespace:

I/O Class Description

BinaryReader Reads primitive data from a binary stream.

BinaryWriter Writes primitive data in binary format.

BufferedStream A temporary storage for a stream of bytes.

Directory Helps in manipulating a directory structure.

DirectoryInfo Used for performing operations on directories.

DriveInfo Provides information for the drives.

File Helps in manipulating files.

FileInfo Used for performing operations on files.

FileStream Used to read from and write to any location in a file.

MemoryStream Used for random access to streamed data stored in memory.

Path Performs operations on path information.

StreamReader Used for reading characters from a byte stream.

StreamWriter Is used for writing characters to a stream.

StringReader Is used for reading from a string buffer.

Page 20: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

StringWriter Is used for writing into a string buffer.

using System;using System.IO;

namespace FileIOApplication{ class Program { static void Main(string[] args) { FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);

for (int i = 1; i <= 20; i++) { F.WriteByte((byte)i); }

F.Position = 0;

for (int i = 0; i <= 20; i++) { Console.Write(F.ReadByte() + " "); } F.Close(); Console.ReadKey(); } }}

OUTPUT

Page 21: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)9.Write the code to add a flash item on your website.<html> <body> <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="468" height="60" id="mymoviename"> <param name="movie" value="example.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <embed src="example.swf" quality="high" bgcolor="#ffffff"width="468" height="60" name="mymoviename" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed> </object> </body></html>

Page 22: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)

10. Explain XML and DTD

A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes.A DTD can be declared inline inside an XML document, or as an external reference.

Internal DTD Declaration

If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element [element-declarations]>

Example XML document with an internal DTD:

<?xml version="1.0"?><!DOCTYPE note [<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>]><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend</body></note>

Open the XML file above in your browser (select "view source" or "view page source" to view the DTD)

The DTD above is interpreted like this:

!DOCTYPE note defines that the root element of this document is note

!ELEMENT note defines that the note element contains four elements: "to,from,heading,body"

!ELEMENT to defines the to element to be of type "#PCDATA"

!ELEMENT from defines the from element to be of type "#PCDATA"

!ELEMENT heading defines the heading element to be of type "#PCDATA"

!ELEMENT body defines the body element to be of type "#PCDATA"

Page 23: Srgoc dotnet_new

CS/CE/ME/IT-406 Computer Programming (.Net Technologies)External DTD Declaration

If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element SYSTEM "filename">

This is the same XML document as above, but with an external DTD (Open it, and select view source):

<?xml version="1.0"?><!DOCTYPE note SYSTEM "note.dtd"><note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>

And this is the file "note.dtd" which contains the DTD:

<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>

XML

Extensible Markup Language, abbreviated XML, describes a class of data objects called XML documents and partially describes the behavior of computer programs which process them. XML is an application profile or restricted form of SGML, the Standard Generalized Markup Language [ISO 8879]. By construction, XML documents are conforming SGML documents.XML documents are made up of storage units called entities, which contain either parsed or unparsed data. Parsed data is made up of characters, some of which form character data, and some of which form markup. Markup encodes a description of the document's storage layout and logical structure. XML provides a mechanism to impose constraints on the storage layout and logical structure.