Suksvl Vki Lcdh Lgk;Rk Dsfy, Cuk;Sx;Sgsa a ;S Uksvl Ljy Hkk'kk Esa
Total Page:16
File Type:pdf, Size:1020Kb
.NET Programming 1 www.ashagroup.org fiz; fo|kfFkZ;ksa] ;s uksV~l vki lcdh lgk;rk ds fy, cuk;s x;s gSaA ;s uksV~l ljy Hkk’kk esa cuk;s x;s gSaA bu uksV~l dks i<+dj vki fofHkUu ijh{kkvksa dh rS;kjh dj ldrs gSaA bu uksV~l ds ckjs esa vki vius lkfFk;ksa tkudkjksa o fj”rsnkjksa dks Hkh crk ldrs gSaA budks cukus esa dkQh le; o esgur yxh gSA buesa dqN =qfVa;k o xyfr;ka gks ldrh gSA dqN VkWfid de gks ldrs gSa blds ckjs esa vki gesa lwfpr dj ldrs gSaA /kU;okn .NET Programming 2 www.ashagroup.org UNIT- I Introduction to .NET Framework: In July 2000 Microsoft announced a whole new software development framework for Windows called .NET in the Professional Developer Conference (PDC). In March 2002 Microsoft released final version of the .NET framework. We can develop various varieties of applications in it. It provides complete SDK (Software Development Kit). In .NET SDK classes, interfaces and language compilers are included. The Microsoft .NET framework 2.0 includes 18619 types 12909 classes 401759 public method, 93105 public property 30546 public events. Now we are using .NET framework 4.0 and 4.5. If we are developing powerful applications then we may require some IDE (Integrated Development Environment) that allows us rapid action development. The new visual studio.NET provides such IDE. There is various versions of VS such as VS 2008, 2010, 2012, 2013 are available. Terminology: To understand .NET framework properly we must have knowledge of some important terminologies. Common Language Runtime CLR: An important part of .NET framework is CLR (Common Language Runtime). CLR is responsible for executing our application code. It performs memory management, exception handling, debugging, security checking, thread execution, code execution, code safety, verification and compilation. Those codes which are directly managed by the CLR are called the managed code. MSIL (Microsoft Intermediate Language): When we write an application for the .NET framework with any language our source code is compiled directly into machine code. Instead our code is converted into a special language name MSIL (Microsoft Intermediate Language). MSIL is a low level and platform independent language. In reality the .NET framework understands only one language MSIL. We can write application using any language such as VB.NET, C++, Ada, COBOL, Fortran, Pascal, PHP, Perl, Java script and many more. The .NET framework includes compiler for these language that enables us to compile our code into MSIL. A Just in time compiler (JIT) compiles the IL code into native code, which is CPU specific. Framework Class Library: It contains a huge library of reusable types, classes, interfaces, structures and enumerated values, which are collectively called types. The .NET framework contains thousands of .NET Programming 3 www.ashagroup.org classes those we can use when building an application. The framework class library was designed to make it easier to perform most common programming tasks. Common Language Specification: It contains the specifications for the .Net supported languages and implementation of language integration. Common Type System: It provides guidelines for declaring, using and managing types at runtime, and cross-language communication. Namespaces: There are lots of classes in .NET framework. Microsoft divided the classes in framework into separate namespaces is simply a category to store the classes. Before we use a class in the program we must indicate the related namespace with the class name. Assemblies: An assembly is the actual .DLL (Dynamic Linking Library) file on our hard disk, where the classes in the .NET framework are stored. In other words an assembly is the primary unit of deployment, security and version control in the .NET framework, because an assembly can span multiple files. An assembly is often referred to as a logical DLL. Metadata: is the binary information describing the program, which is either stored in a portable executable file (PE) or in the memory. C# (Sharp) and .NET: C# is a simple, modern, object oriented and type safe programming language derived from C and C++. C# language was developed by small team of Microsoft engineer Anders Hejlsberg and Scott Wiltamuth. Hejlsberg is also known for creating Turbo Pascal. C# supports .NET framework and similar to java programming language. C# is a multi-paradigm programming language that encompasses functional, imperative, generic, object and component oriented programming disciplines. C# is one of the 44 programming languages supported by the .NET framework common language runtime. It was initially named cool which stood for C like Object Oriented Language. However in July 2000 when Microsoft made the project public the name of programming language was given as C#. Managed Execution Environment: The comparison between (C# / IL Code / CLR) and (Java / Bytecode / JVM) is an inevitable and valid comparison to make. With C and C++ we generally compile the source code to assembly language which will run on a .NET Programming 4 www.ashagroup.org particular processor and a particular operating system. The compiler need to know which processor and operating system it is targeting, because processor may vary in instruction sets and operating system may vary in its functions. C and C++ model has been very successful but has its limitations. Program interface do not interact with other programs. Microsoft COM (Component Object Model) was built to address this problem. Program can’t be used on different platforms. Java addressed these problems by interpreting program to bytecode which then runs on a virtual machine. This bytecode is machine independent which means the same class file can be used on number of platforms. But the interpretation is very slow process and it is never appealing to the performance conscious programmer. Today most JVM (Java Virtual Machine) use a Just In Time (JIT) compiler to make compilation fast. The basic model .NET uses the same as described above. The IL (MSIL) code has some improvements over byte code. 1. To provide greater type neutrality (helps implementation of templates). 2. To provide greater language neutrality. 3. To always be compiled to assembly language before executing and never interpreted. Similarities and Differences from Java: C++ and Java have many similarities. We can discuss these similarities and differences as follows:- Both have powerful reflection capabilities. C++ always uses .(dot) operator instead of arrow or scope resolution operator. Null, Boolean / Bool keywords area available. Garbage collection coupled with the elimination of pointers. Compiles into machine independent codes. Java generates bytecode and C# generate IL code. No header files, all code scoped to package or assemblies. No global function or constants everything belongs to a class. All values are initialized before use. Arrays and strings with length built-in and bounds checking. Both language support threads part of process such as open many tab in a browser. .NET Programming 5 www.ashagroup.org Structure of C# Program: Before we study basic building blocks of the C# programming language, let us look at a bare minimum C# program structure so that we can take it as a reference in upcoming chapters. A C# program basically consists of the following parts: Namespace declaration A class Class methods Class attributes A Main method Statements & Expressions Comments Let us look at a simple code that would print the words "Hello World": using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { /* my first program in C# */ Console.WriteLine("Hello World"); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Hello World Let us look at various parts of the above 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 .NET Programming 6 www.ashagroup.org would contain more than one method. 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 will do when executed. The next line /*...*/ will be ignored by the compiler and it has been put to add additional 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.9610040036 It's 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, file name could be different from the class name. Compile & Execute a C# Program: If you are using Visual Studio.NET for compiling and executing C# programs, take the following steps: Start Visual Studio. On the menu bar, choose File, New, Project. Choose Visual C# from templates, and then choose Windows. Choose Console Application. Specify a name for your project, and then choose the OK button. The new project appears in Solution Explorer. Write code in the Code Editor. Click the Run button or the F5 key to run the project.