C# Chapter 1-3

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C# Chapter 1-3 as PDF for free.

More details

  • Words: 2,950
  • Pages: 6
Chapter 1 What is C#? C# is a new computer-programming language developed by Microsoft Corporation, USA. C# is a fully object-oriented language like Java and is the first Component-oriented language. It has been designed to support the key features of .NET Framework. It is simple, efficient, productive and type-safe language derived from the popular C and C++ languages. C# is designed for building robust, reliable and durable components to handle real-world applications. Major highlights of C# are: - It is a brand new language derived from the C/C++ family - It simplifies and modernizes C++ - It is the only component-oriented language available today - It is the only language designed for the .NET framework - It is a concise, lean and modern language - It combines the best features of many commonly used languages: the productivity of Visual Basic, the power of C++ and the elegance of Java - It is intrinsically object- oriented and web-enabled - It has a lean and consistent syntax - It embodies today’s concern for simplicity, productivity and robustness - It will become the language of choice for .NET programming -Major parts of .NET Framework are actually coded in C# Applications of C# - Console applications - Windows applications - Developing Windows controls - Developing ASP.NET projects - Creating Web controls - Providing Web services - Developing .NET component library Characteristics of C# The main design goal of C# was simplicity rather than pure power. C# fulfills the need for a language that is easy to write, read and maintain and also provides the power and flexibility of C++. The language that is designed for both computing & communications is characterized by several key features. • Simple: C# simplifies C++ by eliminating operators such as ->,:: and pointers. C# treats integer and Boolean data types as two entirely different types. This means that the use of = in place of == in if statement will be caught by the compiler. • Consistent: C# supports 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. • Modern: C# is called a modern language due to a number of features it supports. It supports - Automatic garbage collection -Rich intrinsic model for error handling - Decimal data type for financial applications - Modern approach to debugging and - Robust security model • Object-Oriented: C# is truly object-oriented. It supports all the three tenets of object-oriented systems. – Encapsulation - Inheritance - Polymorphism The entire C# class model is built on top of the Virtual Object System of the .NET framework. In C#, everything is an object. There are no more global functions, variables and constants. • Type-Safe: Type-safe promotes robust programs. C# incorporates a number of type-safe measures. - All dynamically allocated objects and arrays are initialized to zero - Use of any uninitialized variables produces an error message by the compiler - Access to arrays are range-checked and warned if it goes out-of-bounds - C# does not permit unsafe casts - C# enforces overflow checking in arithmetic operations - Reference parameters that are passed are type-safe - C# supports automatic garbage collection • Versionable: Making new versions of software modules work with the existing applications is known as versioning. C# provides support for versioning with the help of new and override keywords. With this support, a programmer can guarantee that his new class library will maintain binary compatibility with the existing client applications. • Compatible: C# enforces the .NET common language specifications and therefore allows inter-operation with other .NET languages. - C# provides support for transparent access to standard COM and OLE Automation.

• •

- C# also permits interpolation with C-style APIs. Flexible: Although C# does not support pointers, we may declare certain classes and methods as ‘unsafe’ and then use pointers to manipulate them. However, these codes will not be type-safe. Inter-operability: C# provides support for using COM objects, no matter what language was used to author them. C# also supports a special feature that enables a program to call out any native API.

How does C# differ from C++? 1. C# compiles straight from source code to executable code, with no object files. 2. C# does not separate class definition from implementation. 3. In C#, class definition does not use a semicolon at the end. 4. The first character of the Main() function is capitalized. The Main must return either int or void type value. 5. C# does not support #include statement. 6. All data types in C# are inherited from the object super class and therefore they are objects. 7. All the basic value types will have the same size on any system. 8. In C#, data types belong to either value types or reference types. 9. C# checks for uninitialized variables and gives error messages at compile time. 10. In c#, structs are value types. 11. C# declares null as a keyword and considers it as an intrinsic value. 12. C# permits declaration of variables between goto and label. 13. C# does not support default arguments • C++ features dropped 1. Macros 2. Multiple Inheritance 3. Templates 4. Pointers 5. Global variables 6. Typedef statement 7. Default arguments 8. Constant member functions or parameters 9. forward declaration of classes • Enhancements to C++ 1. Automatic garbage collection 2. Versioning support 3. Strict type-safety. 4. Properties to access data members 5. Delegates and events 6. Boxing and unboxing 7. Web services How does C# Differ from Java? 1. C# compiler produces an executable code. 2. C# has more primitive data types 3. Unlike java, all C# data types are objects. 4. Arrays are declared differently in C#. 5. Java uses static final to declare a class constant while C# uses const. 6. C# supports the structure type and Java does not. 7. Java does not provide for operator overloading. 8. The new modifier used for class members has no complement in Java. 9. C# provides better versioning support than Java. 10. C# provides static constructors for initialization. 11. C# includes native support for properties, Java does not. 12. Java does not directly support enumerations. 13. C# checks overflows using checked statements. 14. C# uses is operator instead of instance of operator in Java. 15. C# allows a variable number of parameters using the params keyword.

Chapter 2 The .NET Strategy .NET is a software framework that includes everything required for developing software fro web services. It integrates presentation technologies, component technologies and data technologies on a single platform so as to enable users to develop Internet applications as easily as they do on desktop systems. Microsoft .NET platform includes the following components that would help develop a smart Internet services. - .NET infrastructure and tools - .NET user experience - .NET building block - .NET device software. Microsoft .NET products and services consist of the following: - Windows .NET - MSN .NET - Office .NET - Visual Studio .NET - Personal subscription services -bCentral for .NET

The Origin of .NET Technology The current technology of .NET has gone through three significant phases of development: 1. OLE technology 2. COM technology 3. .NET technology 1. OLE Technology - OLE (object Linking and Embedding) technology was developed by Microsoft in the early 1990s to enable easy interprocess communications. OLE provided support to achieve the following - To embed documents from one application into another application. - To enable one application to manipulate objects located in another application. 2. COM Technology - In the component-based approach, a program is broken into a number of independent components where each one offers a particular service. Each component can be developed and tested independently and then integrated it into the main system. This technology is known as the Component Object Model (COM) and the software built using COM is referred to as componentware. COM technology offers a number of benefits to developers and users. - Reduces the overall complexity of software - Enables distributed development across multiple organizations or departments and - Enhances software maintainability. 3. .NET Technology - .NET technology is a third-generation component model. The inter-module communication is replaced by an intermediate language called Microsoft Intermedia Language in the .NET technology. Various .NETlanguage compilers enforce inter-operability by compiling code into IL, which is automatically compatible with other IL modules. IL allows for true cross-language integration. The .NET Framework The .NET framework is one of the tools provided by the .NET infrastructure and tools component of the .NET platform. The .NET platform provides a new environment for creating and running robust, scalable and distributed applications over the Web. C# derives much of its power from the .NET framework on which it runs. Diagram The .NET framework provides an environment for building, deploying and running web services and other applications. It consists of three distinct technologies 1. Common Language Runtime (CLR) 2. Framework Base Classes 3. User and program interfaces (ASP .NET and Win forms) Diagram The CLR is the core of the .NET framework and is responsible for loading and running C# programs. Base classes provide basic data types, collection classes and other general classes for use by C# and other .NET languages. The top layer contains a set of classes for developing web services and to deal with the user interface. 1. The Common Language Runtime The Common Language Runtime is a runtime environment in which programs written in C# and other .NET languages are executed. It also supports cross-language interoperability. The CLR provides a number of services that include: - Loading and execution of programs - Memory isolation for applications - Verification of type safety - Compilation of IL into native executable code - Providing metadata - Memory management (automatic garbage collection) - Enforcement of security - Interoperability with other systems - Managing exceptions and errors - Support for tasks such as debugging and profiling. Diagram Common type System (CTS) - The .NET framework provides multiple languages support using the feature known as Common type System that is built in to the CLR. Common Language Specification (CLS) - The Common Language Specification defines a set of rules that enables interoperability on the .NET platform. The CLS is a subset of CTS and therefore the languages supporting the CLS can use each others class libraries as if they are their own. Microsoft Intermediate Language (MSIL) - MSIL or simply IL is an instruction set into which all the .NET programs are compiled. Diagram

Managed Code - The CLR is responsible for managing the execution of code compiled for the .NET platform. The code that satisfies the CLR at runtime in order to execute is referred to as managed code. Compilers that are compatible to the .NET platform generate managed code. 2. Framework Base Classes .NET supplies a library of base classes that can be used to implement applications quickly. Much of the functionality in the base framework classes resides in the vast namespace called System. The base classes in the system namespace for many different tasks: - Input/Output Operations - String handling - Managing arrays, lists, maps etc - Accessing files and file systems - Accessing the registry - Security - Windowing - Windows messages - Database management - Evaluation of mathematical functions - Drawing - Managing errors and exceptions - Connecting to the Internet Benefits of the .NET Approach - Simple and faster systems development - Rich object model - Enhanced built-in functionality - Many different ways to communicate - Integration of different languages into one platform - Easy deployment and execution - Wide range of scalability - Interoperability with existing applications - Simple and easy-to build sophisticated development tools - Fewer bugs - Potentially better performance C# and the .NET The CLR extends a number of benefits to C# when it is implemented on the .NET platform. These include: Interoperability with other languages - Enhanced security - Versioning support - Debugging support - Automatic garbage collection - XML support for web-based applications Chapter 3 Executing a program After creating the program i.e. writing the source code, save the file with .cs extension in a folder on your system i.e. program_name.cs Go to the folder where your file is saved and compile the program using csc program_name.cs by this, a executable file will be generated, if there are no errors in the program. If there are some errors, appropriate error messages will be displayed and you have to correct the error and then compile the program again. The executable file will be generated in the same folder that of the program program_name.exe For executing the program, run the file in command prompt, note that there is no extension program_name Diagram Program structure Documentation section Using directive section Interfaces section Classes section Main Method Section

Documentation section must contain the name, date, program name, other details, name of classes etc. Using directive section may contain the names of Namespaces that are to be added in the program. An interface is similar to a class but contains only abstract members. Interfaces are used during implementing the concept of multiple inheritances in a program. Classes are the primary and essential elements of a C# program. The number of classes depends on the complexity of the problem. C# application program requires a Main method as its starting point, the class containing the main is the essential part of the program.

Chapter 4 Literals, variables and Data Types The smallest, non-reducible, textual elements in a program are referred to as Tokens. A C# program is a collection of tokens, comments and white spaces. C# include the following five types of tokens: Keywords, Identifiers, Literals, Operators, Punctuators Keywords are an essential part of a language definition. They implement specific features of the language. They are reserved, and cannot be used as identifiers except when they are prefaced by the @ character. Identifiers are programmer –designed tokens. They are used for naming classes, methods, variables, labels, namespace, interfaces, etc. C# identifiers enforce the following rules: They can have alphabets, digits and underscore character. They must not begin with a digit. Upper case and lower case letters are distinct. Literals are the way in which the values that are stored in variables are represented. Operators are symbols used in expressions to describe operations involving one or more operands. Punctuators are symbols used for grouping and separating code. Punctuators in C# include: Parentheses, Braces, Brackets, Semicolon, Colon, Comma, Period. C# implements several types of statements: Empty Statements, Labeled Statement, Declaration statements, Expression statement, Selection statement, Interaction statements, Jump statements, try statements, checked statements, unchecked statements, lock statements, using statements. Scope of Variables The scope of variables is the region of code within which the variable can be accessed. C# variables are categorized into - Static variables - Instance Variables - Array elements - Value Parameters - Reference Parameters - Output Parameters - Local variables the scope of variables are class Sample { - m is a static variable, n is an instance variable static int m; int n; - a is a value parameter, b is an reference parameter, c is an output void Test (int a, ref int b, out int c, int parameter - arr is an array element [] arr) -d is a local variable. { int d = 100; ………… } } Static and instance variables are declared at the class level and are known as fields and ends when the main method terminates. The reference and output parameters do not create new storage locations; they represent the same storage locations as the variables that are passed as arguments. The scope of a local variables starts immediately after its identifier in the declaration and extends up to the end of the block containing the declaration. C# makes a fundamental distinction between the field variables declared at the class level and the local variables declared within methods. Example Boxing and Unboxing Boxing means the conversion of a value type on the stack to a object type on the heap, conversely, the conversion from an object type back to a value type is known as unboxing. • Boxing – Any type, value or reference can be assigned to an object without an explicit conversion. When the compiler finds a value type where it needs a reference type, it creates an object ‘Box’ into which it places the value of the value type. • Unboxing – Unboxing is the process of converting the object type back to the value type. A variable that has previously been boxed can only be unboxed. While unboxing the object could be cast to any type. Method Overloading - C# allows us to create more than one method with the same name, but with the different parameter lists and different definitions. This is called method overloading. - Method overloading is used when methods are required to perform similar tasks but using different input parameters. - Overloaded methods must differ in number and/or type of parameters they take. This enables the compiler to decide which one of the definitions to execute depending on the type and number of arguments in the method call. - using the concept of method overloading, a family of methods with one name but different arguments lists can be designed.

The method selection involves the following steps: 1) The compiler tries to find an exact match in which the types of actual parameters are same and uses that method. 2) If the exact match is not found, then the compiler tries to use the implicit conversions to the actual arguments and then uses the method whose match is unique. If the conversion creates multiple matches, then the compiler will generate an error message. Program

Related Documents

Chapter 13
June 2020 16
Chapter 13
November 2019 58
Chapter 13
June 2020 13
Chapter 13
November 2019 7
Chapter 13
November 2019 5
Chapter 13
December 2019 4