




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
READ LEARN AND SOLVE VB.NET PROGRAMMING
Typology: Schemes and Mind Maps
1 / 118
This page cannot be seen from the preview
Don't miss anything!
Syllabus
The .Net Framework:Introduction Introduction Common Language Runtime (CLR) Common Type System (CTS) Common Language Specification (CLS) Microsoft Intermediate Language (MSIL) Just-In – Time Compilation Framework Base Classes.
C -Sharp Language (C#): Introduction Data Types, Identifiers, Variables, Constants and Literals
Array and Strings
Object and Classes
Inheritance and Polymorphism
Operator Overloading
Interfaces, Delegates and Events
Type conversion.
C# Using Libraries Namespace- System Input-Output, Multi-Threading, Networking and sockets, Managing Console I/O Operations, Windows Forms, Error Handling.
Advance Features using C# Web Services, Window Services, Asp.net Web Form Controls, ADO.Net. Distributed Application in C#, Unsafe Mode, Graphical Device interface with C#.
Assemblies and Attributes .Net Assemblies
Attributes
Generic.
1. 1 Introduction to Dot Net Framework The .NET is the technology from Microsoft, on which all other Microsoft technologies will be depending on in future. It is a major technology change. Just like the computer world moved from DOS to Windows, now they are moving to .NET. But don't be surprised if you find anyone saying that "I do not like .NET and I would stick with the good old COM and C++". There are still lot of people who like to use the bullock-cart instead of the latest Honda car. .NET technology was introduced by Microsoft, to catch the market from the SUN's Java. Few years back, Microsoft had only VC++ and VB to compete with Java, but Java was catching the market very fast. With the world depending more and more on the Internet/Web and java related tools becoming the best choice for the web applications, Microsoft seemed to be loosing the battle. Thousands of programmers moved to java from VC++ and VB. To recover the market, Microsoft announced .NET. .NET framework comes with a single class library. And thats all programmers need to learn!! Whether they write the code in C# or VB.NET or J#, it doesn't matter, you just use the .NET class library. There is no classes specific to any language. There is nothing more you can do in a language, which you can't do in any other .NET language. You can write code in C# or VB.NET with the same number of lines of code, same performance and same efficiency, because everyone uses same .NET class library.
Features of .NET
^ It is a platform neutral framework.^ It is a layer between the operating system and the programming language. It supports many programming languages, including VB.NET, C# etc. .NET provides a common set of class libraries, which can be accessed from any .NET based programming language. There will not be separate set of classes and libraries for each language. If you know any one .NET language, you can write code in any .NET
language.^ In future versions of Windows, .NET will be freely distributed as part of operating system and users will never have to install .NET separately. Major Components of .NET The diagram given below describes various components of .NET Framework.
The .NET framework can only be exploited by languages that are compliant with .NET. Most of Microsoft languages have been made to fully comply with .NET. .NET also introduces Web Forms, Web Services and Windows Forms. The reason why they have been shown separately and not as a part of a particular language is that these technologies can be used by any .NET compliant language. For example Windows Forms is used by VC, VB.NET, C# all as a mode of providing GUI. The next component of .NET is the .NET Framework Base Classes. These are the common class libraries (much like Java packages) that can be used by any .NET compliant language. These classes provide the programmers with a high degree of functionality that they can use in their programs. For example their are classes to handle reading, writing and manipulating XML documents, enhanced ADOs etc. The bottom most layer is the CLR - the common runtime language. Origin of .net Technology
OLE Technology(Object Linking and Embedding)
1.2 Common Language Runtime (CLR) The CLR is the heart of .NET framework. It is .NET equivalent of Java Virtual Machine (JVM). It is the runtime that converts a MSIL (Micro Soft Intermediate Language) code into the host machine language code, which is then executed appropriately. The CLR provides a number of services that include:
^ Loading and execution of codes^ Memory isolation for application Verification of type safety Compilation of IL into native executable code Providing metadata Automatic garbage collection Enforcement of Security
must follow these rules. In other words we can not have languages running around creating their own extensions and their own fancy new data types. CLS is the collection of the rules and constraints that every language (that seeks to achieve .NET compatibility) must follow. Microsoft has defined three level of CLS compatibility/compliance. The goals and objectives of each compliance level have been set aside. The three compliance levels with their brief description are given below: Compliant producer The component developed in this type of language can be used by any other language. Consumer The language in this category can use classes produced in any other language. In simple words this means that the language can instantiate classes developed in other language. This is similar to how COM components can be instantiated by your ASP code. Extender Languages in this category can not just use the classes as in CONSUMER category; but can also extend classes using inheritance. Languages that come with Microsoft Visual Studio namely Visual C++, Visual Basic and C#; all satisfy the above three categories. Vendors can select any of the above categories as the targeted compliance level(s) for their languages.
1.5 Microsoft Intermediate Language (MSIL) A .NET programming language (C#, VB.NET, J# etc.) does not compile into executable code; instead it compiles into an intermediate code called Microsoft Intermediate Language (MSIL). As a programmer one need not worry about the syntax of MSIL - since our source code in automatically converted to MSIL. The MSIL code is then send to the CLR (Common Language Runtime) that converts the code to machine language which is then run on the host machine. MSIL is similar to Java Byte code. A Java program is compiled into Java Byte code (the .class file) by a Java compiler, the class file is then sent to JVM which converts it into the host machine language. Managed Code The role of CLR doesn‘t end once we have compiled our code to MSIL and a JIT compiler has compiled this to native code. Code written using the .NET framework, is managed code when it is executed. This stage is usually referred to as being at runtime. This means that the CLR looks after our applications, by managing memory, handling security, allowing cross language debugging and so on. By contrast, applications that do not run under the control of the CLR are said to be unmanaged and certain languages such as C++ can be used to write such applications, that for example, to access low level functions of the operating systems. However in C# we can only write code that runs in a managed environment. Unified classes The term .NET framework refers to the group of technologies that form the development foundation for the Microsoft .NET platform. The key technologies in this group are the run time and the class libraries. The run time is responsible for managing your code and providing services to it while it executes, playing a role similar to that of the Visual Basic 6.0 run time. The .NET programming languages including Visual Basic .NET , Microsoft Visual C# and C++ managed extensions and many other programming languages from various vendors utilize .NET services and features through a common set of unified classes. The .NET unified classes provide foundation of which you build your applications, regardless of the language you use. Whether you simply concating a string, or building a windows Services or a multiple-tier web-based applications, you will be using these unified classes.
The unified classes provide a consistent method of accessing the platforms functionality. Once you learn to use the class library, you‗ll find that all tasks follow the same uniform architecture, you no longer need to learn and master different API architecture to write your applications. By building your applications on a unified, integrated framework, you maximize your return on the time you spend learning this framework, and you end up with more robust applications that are easy to deploy and maintain. 1.6 Just In Time Compiler
a language akin to assembly language called Microsoft intermediate language (MSIL). To compile applications for .NET, compilers take source code as input and produce MSIL as output.
assembly language, you would probably never do so except in unusual circumstances. Because MSIL is its own language, each compiler team makes its own decision about how much of the MSIL it will support. However, if you're a compiler writer and you want to create a language that does interoperate with other languages, you should restrict yourself to features specified by the CLS.
1.7 Framework Base Classes The .NET Framework has an extensive set of class libraries. This includes classes for:
Unit-II C -Sharp Language (C#)
2.1 Introduction Microsoft Corporation, developed a new computer programming language C# pronounced as ‗C- Sharp‘. C# is a simple, modern, object oriented, and type safe programming language derived from C and C++. C# is a purely object-oriented language like as Java. It has been designed to support the key features of .NET framework. Like Java, C# is a descendant language of C++ which is descendant of C language.
Object Orientation
Component Orientation
Component
Elegance
C# (^) Productivity
C# modernize C++ by enhancing some of its features and adding a few new features. C# borrows Java‘s features such as grouping of classes, interface and implementation together in one file so the programmers can easily edit the codes. C# also handles objects using reference, the same way as Java. C# uses VB‘s approach to form designing, namely, dragging controls from a tool box, dropping them onto forms, and writing events handlers for them. Comparing C# to C++ and Java C# versus Java C# and Java are both new-generation languages descended from a line including C and C++. Each includes advanced features, like garbage collection, which remove some of the low level maintenance tasks from the programmer. In a lot of areas they are syntactically similar. Both C# and Java compile initially to an intermediate language: C# to Microsoft Intermediate Language (MSIL), and Java to Java bytecode. In each case the intermediate language can be run -
by interpretation or just-in-time compilation - on an appropriate 'virtual machine'. In C#, however, more support is given for the further compilation of the intermediate language code into native code. C# contains more primitive data types than Java, and also allows more extension to the value types. For example, C# supports 'enumerations', type -safe value types which are limited to a defined set of constant variables, and 'structs', which are user-defined value types. Unlike Java, C# has the useful feature that we can overload various operators. Like Java, C# gives up on multiple class inheritance in favour of a single inheritance model extended by the multiple inheritance of interfaces. However, polymorphism is handled in a more complicated fashion, with derived class methods either 'overriding' or 'hiding' super class methods C# also uses 'delegates' - type-safe method pointers. These are used to implement event-handling. In Java, multi -dimensional arrays are implemented solely with single- dimensional arrays (where arrays can be members of other arrays. In addition to jagged arrays, however, C# also implements genuine rectangular arrays. C# versus C++ Although it has some elements derived from Visual Basic and Java, C++ is C#'s closest relative. In an important change from C++, C# code does not require header files. All code is written inline. As touched on above, the .NET runtime in which C# runs performs memory management, taking care of tasks like garbage collection. Because of this, the use of pointers in C# is much less important than in C++. Pointers can be used in C#, where the code is marked as 'unsafe', but they are only really useful in situations where performance gains are at an absolute premium. Speaking generally, the 'plumbing' of C# types is different from that of C++ types, with all C# types being ultimately derived from the 'object' type. There are also specific differences in the way that certain common types can be used. For instance, C# arrays are bounds checked unlike in C++, and it is therefore not possible to write past the end of a C# array. C# statements are quite similar to C++ statements. To note just one example of a difference: the 'switch' statements has been changed so that 'fall-through' behavior is disallowed. As mentioned above, C# gives up on the idea of multiple class inheritance. Other differences relating to the use of classes are: there is support for class 'properties' of the kind found in Visual Basic, and class methods are called using the. operator rather than the :: operator. Features of C#
1. Simplicity All the Syntax of java is like C++. There is no preprocessor, and much larger library. C# code does not require header files. All code is written inline. 2. Consistent behavior C# introduced an unified type system which eliminates the problem of varying ranges of integer types. All types are treated as objects and developers can extend the type system simply and easily. 3. Modern programming language C# supports number of modern features, such as:
^ Automatic Garbage Collection^ Error Handling features Modern debugging features Robust Security features
4. Pure Object- Oriented programming language In C#, every thing is an object. There are no more global functions, variable and constants. It supports all three object oriented features: Encapsulation
As C# is an object-oriented language, C# programs must be placed in classes (classes are discussed in chapter 5 but if you are new to object orientation we suggest that you first read some introductory material). This line declares the class to be named 'HelloWorld'. Line 4: public static void Main() When compiled and run, the program above will automatically run the 'Main' method declared and begun in this line. Note again C#'s case-sensitivity - the method is 'Main' rather than 'main'. Line 3,11 and 5,12 : These lines are uses the ‗{‗ for starting braces and ‗}‘ for closing braces of block. Lines 6-9 : Comments ( ‗//‘ uses for single line and ‗/* -- - - /‘ uses for multiple line comments) These lines of the program are ignored by the compiler, being comments entered by the programmer for his own benefit. Line 6 shows a single line comment, in which everything on the line after the two forward slashes is ignored by the compiler. Lines 7-9 demonstrate a multi-line comment, in which everything between the opening / and closing */ is ignored, even when it spans multiple lines. Line 10: The statement on this line calls the 'WriteLine' method of the Console class in the System namespace. It should be obvious how this works in the given example - it just prints out the given string to the 'Console' (on PC machines this will be a DOS prompt). Instruction for Saving the Program In order to run the program, it must first be saved in a file. Unlike in Java, the name of the class and the name of the file in which it is saved do not need to match up, although it does make things easier if you use this convention. In addition, you are free to choose any extension for the file, but it is usual to use the extension '.cs'. Writing program in Computer There are two ways of program writing in computer Using Text Editor Using Visual Studio.NET
Identifiers & Variables Identifiers refer to the names of variables, functions arrays, classes, etc. created by programmer. They are fundamental requirement of any language. Each language has its own rules for naming these identifiers. To name the variables of your program, you must follow strict rules. In fact, everything else in your program must have a name. There are some rules you must follow when naming your objects. On this site, here are the rules we will follow: The name must start with a letter or an underscore After the first letter or underscore, the name can have letters, digits, and/or underscores The name must not have any special characters other than the underscore The name cannot have a space C# is case-sensitive. This means that the names Case, case, and CASE are completely different. For example, the main function is always written Main. C# Keywords C# uses a series of words, called keywords, for its internal use. This means that you must avoid naming your objects using one of these keywords. They are:
abstract const extern int out short typeof as continue false interface override sizeof uint base decimal finally internal params stackalloc ulong bool default fixed is private static unchecked break delegate float lock protected string unsafe byte do for long public struct ushort case double foreach namespace readonly switch using catch else goto new ref this virtual char enum if null return throw void checked event implicit object sbyte true volatile class explicit in operator sealed try while
Data types C# is a type-safe language. Variables are declared as being of a particular type, and each variable is constrained to hold only values of its declared type. Variables can hold either value types or reference types, or they can be pointers. Here's a quick recap of the difference between value types and reference types.
C# .Net Framework Signed? Bytes Possible Values Type (System) type Occupied sbyte System.Sbyte Yes 1 - 128 to 127 short System.Int16 Yes 2 - 32768 to 32767 int System.Int32 Yes 4 - 2147483648 to 2147483647 long System.Int64 Yes 8 - 9223372036854775808 to 9223372036854775807 byte System.Byte No 1 0 to 255 ushort System.Uint16 No 2 0 to 65535 uint System.UInt32 No 4 0 to 4294967295
Then s2 does at this point reference the same string object as s1. However, when the value of s
is changed, for instance with
s1 = "goodbye";
what happens is that a new string object is created for s1 to point to. Hence, following this piece of code, s1 equals "goodbye", whereas s2 still equals "hello". The reason for this behaviour is that string objects are 'immutable'. That is, the properties of these objects can't themselves change. So in order to change what a string variable references, a new string object must be created. Boxing C# allows you convert any value type to a corresponding reference type, and to convert the resultant 'boxed' type back again. The following piece of code demonstrates boxing. When the second line executes, an object is initiated as the value of 'box', and the value held by i is copied across to this object. It is interesting to note that the runtime type of box is returned as the boxed value type; the 'is' operator thus returns the type of box below as 'int'. int i = 123; object box = i; if (box is int) {Console.Write("Box contains an int");} // this line is printed
When boxing occurs, the contents of value type are copied from stack into memory allocated into the managed heap. The new reference type created contains a copy of the value type, and can be used by other types that expect an object reference. The value contained in the value type and the created reference types are not associated in any way (except that they contain the same values). If we change the original value type, the refernce type is not affected.
The following code explicitly unboxes a reference type into a value type:
object o; int i = (int) o;
When unboxing occurs, memory is copied from the managed heap to the stack.
2.3 Array and Strings Arrays An array is a group or collection of similar values. An array contains a number of variables, which are accessed through computed indices. The various value contained in an array are also called the elements of array. All elements of an array have to be of same type, and this type is called the element type of the array. The element of an array can be of any type including an array type. An array has a rank that determines the number of indices associated wth each array elements. The rank of an array is also referred as the dimension of the array. An array may be :
^ Single Dimensional^ Multi Dimensional
An array with a rank of one is called single-dimensional array, and an array with a rank greater than one is called a multi dimensional array. Each dimension of array has an associated length, which is an integer number greater than or equal to zero. For a dimension of length n, indices can range from 0 to n-1. in C#, array types are categorized under the reference types alongside with classes and interfaces. Single Dimensional Array Single -dimensional arrays have a single dimension (ie, are of rank 1). The process of creation of arrays is basically divided into three steps:
type[] arrayname;
For Example:
int[] a; float[] marks; double[] x; int[] m,n;
You must note that we do not enter the size of the arrays in the declaration.
Memory Allocation for Array After declaring an array, we need to allocate space and defining the size. Declaring arrays merely says what kind of values the array will hold. It does not create them. Arrays in C# are objects, and you use the new keyword to create them. When you create an array, yu must tell the compiler how many components will be stored in it. Here is given the syntax:
arrayname = new type[size];
For Example:
a = new int[5]; marks = new float[6]; x = new double[10]; m = int[100]; n = int [50];
It is also possible to combine the two steps, declaration and memory allocation of array, into one as shown below:
Example using system; class Number { public static void Main() { int [] num = {10, 20, 30, 40, 50}; int n = num.Length; // Length is predefined attribute to access the size of array Console.Write(― Elements of array are :‖); for(int i=0; i<n; i++) { Console.WriteLine(num[i]); }
int sum =0; for(int i=0; i<n; i++) { sum = sum + num[i]); } Console.WriteLine(― The sum of elements :‖+sum); }
OUTPUT: Elements of array are: 10 20 30 40 50
The sum of elements :
Multi Dimensional Array C# supports two types of multidimensional arrays:
^ Rectangular Array^ Jagged Array Rectangular Arrays A rectangular array is a single array with more than one dimension, with the dimensions' sizes fixed in the array's declaration. The following code creates a 2 by 3 multi-dimensional array: int[,] squareArray = new int[2,3];
As with single-dimensional arrays, rectangular arrays can be filled at the time they are declared. For instance, the code int[,] squareArray = {{1, 2, 3}, {4, 5, 6}};
creates a 2 by 3 array with the given values. It is, of course, important that the given values do fill out exactly a rectangular array. The System.Array class includes a number of methods for determining the size and bounds of arrays. These include the methods GetUpperBound(int i) and GetLowerBound(int i) , which
return, respectively, the upper and lower subscripts of dimension i of the array (note that i is zero based, so the first array is actually array 0). For instance, since the length of the second dimension of squareArray is 3, the expression squareArray.GetLowerBound(1) returns 0, and the expression
squareArray.GetUpperBound(1)
returns 2. System.Array also includes the method GetLength(int i) , which returns the number of elements in the ith dimension (again, zero based). The following piece of code loops through squareArray and writes out the value of its elements. for(int i = 0; i < squareArray.GetLength(0); i++) for (int j = 0; j < squareArray.GetLength(1); j++) Console.WriteLine(squareArray[i,j]);
A foreach loop can also be used to access each of the elements of an array in turn, but using this construction one doesn't have the same control over the order in which the elements are accessed. Jagged Arrays Using jagged arrays, one can create multidimensional arrays with irregular dimensions. This flexibility derives from the fact that multidimensional arrays are implemented as arrays of arrays. The following piece of code demonstrates how one might declare an array made up of a group of 4 and a group of 6 elements: int[][] jag = new int[2][]; jag[0] = new int [4]; jag[1] = new int [6]; The code reveals that each of jag[0] and jag[1] holds a reference to a single-dimensional int array. To illustrate how one accesses the integer elements: the term jag[0][1] provides access to the second element of the first group. To initialise a jagged array whilst assigning values to its elements, one can use code like the following: int[ ][ ] jag = new int[ ][ ] {new int[ ] {1, 2, 3, 4}, new int[ ] {5, 6, 7, 8, 9, 10}};
Be careful using methods like GetLowerBound, GetUpperBound, GetLength, etc. with jagged arrays. Since jagged arrays are constructed out of single-dimensional arrays, they shouldn't be treated as having multiple dimensions in the same way that rectangular arrays do. To loop through all the elements of a jagged array one can use code like the following:
for (int i = 0; i < jag.GetLength(0); i++) for (int j = 0; j < jag[i].GetLength(0); j++) Console.WriteLine(jag[i][j]);
or
for (int i = 0; i < jag.Length; i++) for (int j = 0; j < jag[i].Length; j++) Console.WriteLine(jag[i][j]);