A history of C, C++, and C#. (Teach-In).The C# programming language was created in the spirit of the C and C++ programming languages. This accounts for its powerful features and easy learning curve. The same can't be said for C and C++, but because C# was created from the ground up, Microsoft took the liberty of removing some of the more burdensome features--such as pointers. This article takes a look at the C and C++ languages, tracing their evolution into C#. C The C programming language was originally designed for use on the UNIX operating system Noun 1. UNIX operating system - trademark for a powerful operating system UNIX, UNIX system operating system, OS - (computer science) software that controls the execution of computer programs and may provide various services . C was used to create many UNIX UNIX Operating system for digital computers, developed by Ken Thompson of Bell Laboratories in 1969. It was initially designed for a single user (the name was a pun on the earlier operating system Multics). applications, including a C compiler Noun 1. C compiler - a compiler for programs written in C compiling program, compiler - (computer science) a program that decodes instructions written in a higher order language and produces an assembly language program , and was eventually used to write UNIX itself. Its widespread acceptance in the academic arena expanded to include the commercial world, and software vendors such as Microsoft and Borland released C compilers for personal computers. The original Windows API The Windows API, informally WinAPI, is the name given by Microsoft to the core set of application programming interfaces (APIs) available in the Microsoft Windows operating systems. was designed to work with Windows code written in C, and the latest set of the core Windows operating system operating system (OS) Software that controls the operation of a computer, directs the input and output of data, keeps track of files, and controls the processing of computer programs. APIs remain compatible with C to this day. From a design standpoint, C lacked a detail that other languages such as Smalltalk had already embraced: the concept of an object, a collection of data and a set of operations that can be performed on that data. Object-style coding could be accomplished using C, but the notion of an object was not enforced by the language. If you wanted to structure your code to resemble an object, fine. If you didn't, fine. C really didn't care. Objects weren't an inherent part of the language, so many people didn't pay much attention to this programming paradigm A programming paradigm is a fundamental style of programming regarding how solutions to problems are to be formulated in a programming language. (Compare with a methodology, which is a style of solving specific software engineering problems). . After the notion of object-oriented development began to gain acceptance, it became clear that C needed to be refined to embrace this new way of thinking about code. C++ was created to embody em·bod·y tr.v. em·bod·ied, em·bod·y·ing, em·bod·ies 1. To give a bodily form to; incarnate. 2. To represent in bodily or material form: this refinement. It was designed to be backwardly compatible with C (such that all C programs would also be C++ programs and could be compiled with a C++ compiler compiler Computer software that translates (compiles) source code written in a high-level language (e.g., C++) into a set of machine-language instructions that can be understood by a digital computer's CPU. ). The major addition to the C++ language was support for the new object concept. The C++ language added support for classes (which are "templates' of objects), and enabled an entire generation of C programmers to think in terms of objects and their behavior. C++ The C++ language is an improvement over C, but it still has some disadvantages. C and C++ can be hard to get a handle on. Unlike easy-to-use languages like Visual Basic, C and C++ are very 'low level" and require the user to do a lot of coding to make an application run well. You have to write your own code to handle issues such as memory management and error checking. C and C++ can result in very powerful applications, but you must ensure that your code works well. One bug can make the entire application crash or behave unexpectedly. Because of the C++ design goal of retaining backward compatibility See backward compatible. (jargon) backward compatibility - Able to share data or commands with older versions of itself, or sometimes other older systems, particularly systems it intends to supplant. with C, C++ was unable to break away from the low level nature of C. Microsoft designed C# to retain much of the syntax of C and C++. Developers who are familiar with those languages can pick up C# code and begin coding relatively quickly. The big advantage to C#, however, is that its designers chose not to make it backwardly compatible with C and C++. While this may seem like a bad deal, it's actually good news. C# eliminates the things that makes C and C++ difficult to work with. Because all C code is also C++ code, C++ had to retain all of the original quirks and deficiencies found in C. C# is starting with a clean slate Noun 1. clean slate - an opportunity to start over without prejudice fresh start, tabula rasa chance, opportunity - a possibility due to a favorable combination of circumstances; "the holiday gave us the opportunity to visit Washington"; "now is your chance" and without any compatibility requirements, so it can retain the strengths of its predecessors and discard the weaknesses that made life hard for C and C++ programmers. Introducing C# C#, the new language introduced in the .NET Framework, is derived from C++. However, C# is a modern, objected-oriented (from the ground up) type-safe language. Language features The following sections take a quick look at some of the features of the C# language. Classes All code and data in C# must be enclosed en·close also in·close tr.v. en·closed, en·clos·ing, en·clos·es 1. To surround on all sides; close in. 2. To fence in so as to prevent common use: enclosed the pasture. in a class. You can't define a variable out- side of a class, and you can't write any code that's not in a class. Classes can have constructors, which execute when an object of the class is created, and a destructor (programming) destructor - A function provided by a class in C++ and some other object-oriented languages to delete an object, the inverse of a constructor. , which executes when an object of the class is destroyed. Classes support single inheritance In object-oriented programming, a class that has no more than one parent. Contrast with multiple inheritance. single inheritance - The property of an object-oriented language which restricts a sub-class to be derived from only one parent. Opposite of multiple inheritance. , and all classes ultimately derive from a base class called object. C# supports versioning techniques to help your classes evolve over time while maintaining compatibility with code that uses earlier versions of your classes. As an example, take a look at a class called Family. This class contains the two static fields that hold the first and last name of a family member as well as a method that returns the full name of the family member.
class Class
{
public string FirstName;
public string LastName;
public string FullName()
{
return FirstName + LastName.,
}
}
Note: Single inheritance means that a C# class can inherit from
only one base class.
C# enables you to group your classes into a collection of classes called a namespace A collection of names for a particular purpose. Typically, each name is unique. For example, tables in a relational database must all have unique names. A Windows workgroup that uses the original NetBIOS naming system requires a different "made-up" name for each computer and printer in . Namespaces have names, and can help organize collections of classes into logical groupings. As you begin to learn C#, it becomes apparent that all namespaces relevant to the .NET Framework begin with System. Microsoft has also chosen to include some classes that also in backwards compatibility backwards compatibility - backward compatibility and API (Application Programming Interface) A language and message format used by an application program to communicate with the operating system or some other control program such as a database management system (DBMS) or communications protocol. access. These classes are contained within the Microsoft namespace. Data types C# lets you work with two types of data: value types and reference types. Value types hold actual values. Reference types hold references to values stored elsewhere in memory. Primitive types In computer science, primitive types — as distinct from composite types — are data types provided by a programming language as basic building blocks. Primitive types are also known as built-in types or basic types. such as char, int and float, as well as enumerated This term is often used in law as equivalent to mentioned specifically, designated, or expressly named or granted; as in speaking of enumerated governmental powers, items of property, or articles in a tariff schedule. values and structures, are value types. Reference types hold variables that deal with objects and arrays. C# comes with predefined reference types (object and string), as well as predefined value types (sbyte, short, int, long, byte, ushort, uint, ulong float, double, bool, char, and decimal Meaning 10. The numbering system used by humans, which is based on 10 digits. In contrast, computers use binary numbers because it is easier to design electronic systems that can maintain two states rather than 10. ). You can also define your own value and reference types in your code. All value and reference types ultimately derive from a base type called object. C# allows you to convert a value of one type into a value of another type. You can work with both implicit conversions and explicit conversions. Implicit conversions always succeed and don't lose any information (for example, you can convert an int to a long without losing any data because a long is larger than an int). Explicit conversions may cause you to lose data (for example, converting a long into an int may result in a loss of data because a long can hold larger values than an int). You must write a cast operator into your code to make an explicit conversion happen. You can work with both one-dimensional and multidimensional mul·ti·di·men·sion·al adj. Of, relating to, or having several dimensions. mul ti·di·men arrays
in C#. Multidimensional arrays can be rectangular, in which each of the
arrays has the same dimensions, or jagged, in which each of the arrays
has different dimensions.
Classes and structures can have data members called properties and fields. Fields are variables that are associated with the enclosing en·close also in·close tr.v. en·closed, en·clos·ing, en·clos·es 1. To surround on all sides; close in. 2. To fence in so as to prevent common use: enclosed the pasture. class or structure. You may define a structure called Employee, for example, that has a field called Name. If you define a variable of type Employee called CurrentEmployee, you can retrieve the employee's name by writing Current Employee. Name. Properties are like fields, but enable you to write code to specify what should happen when code accesses the value. If the employee's name must be read from a database, for example, you can write code that says, "when someone asks for the value of the Name property, read the name from the database and return the name as a string." Functions A function is a callable Callable Applies mainly to convertible securities. Redeemable by the issuer before the scheduled maturity under specific conditions and at a stated price, which usually begins at a premium to par and declines annually. piece of code that may or may not return a value to the code that originally called it. An example of a function would be the Full Name function shown earlier in the Family class. A function is generally associated to pieces of code that return information whereas a method generally does not return information. For our purposes however, we generalize generalize /gen·er·al·ize/ (-iz) 1. to spread throughout the body, as when local disease becomes systemic. 2. to form a general principle; to reason inductively. and refer to them both as functions. Functions can have four kinds of parameters: + Input parameters have values that are sent into the function, but the function cannot change those values. + Output parameters have no value when they are sent into the function, but the function can give them a value and send the value back to the caller. + Reference parameters pass in a reference to another value. They have a value coming in to the function, and that value can be changed inside the function. + Params parameters define a variable number of arguments in a list. C# and the CLR (Common Language Runtime) The runtime engine in Microsoft's .NET platform. The CLR compiles and executes programs in Microsoft Intermediate Language (MSIL). The counterpart to the CLR for the Common Language Infrastructure (CLI), ECMA's standard version of . work together to provide automatic memory management. You don't need to write code that says 'allocate enough memory for an integer' or 'free the memory that this object was using." The CLR monitors your memory usage and automatically retrieves more when you need it. It also frees memory automatically when it detects that it is no longer being used (this is also known as Garbage Collection A software routine that searches memory for areas of inactive data and instructions in order to reclaim that space for the general memory pool (the heap). Operating systems may or may not provide this feature. ). C# provides a variety of operators that enable you to write mathematical and bit- wise expressions. Many (but not all) of these operators can be redefined, enabling you to change how the operators work. C# supports a long list of statements that enable you to define various execution paths within your code. Flow control statements that use keywords such as if, switch, while, for, break and continue enable your code to branch off into different paths, depending on the values of your variables. Classes can contain code and data. Each class member has something called an accessibility scope, which defines the member's visibility to other objects. C# supports public, protected, internal, protected internal, and private accessibility scopes. Variables Variables can be defined as constants, Constants have values that cannot change during the execution of your code. The value of pi, for instance, is a good example of a constant, because its value won't be changing as your code runs. Enum type declarations specify a type name for a related group of constants. For example, you could define an enum of Planets with values of Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune and Pluto, and use those names in your code. Using the enum names in code makes code more readable than if you used a number to represent each planet. C# provides a built-in mechanism for defining and handling events. If you write a class that performs a lengthy operation, you may want to invoke To activate a program, routine, function or process. an event when the operation is completed. Clients can subscribe to Verb 1. subscribe to - receive or obtain regularly; "We take the Times every day" subscribe, take buy, purchase - obtain by purchase; acquire by means of a financial transaction; "The family purchased a new car"; "The conglomerate acquired a new company"; that event and catch the event in their code, which enables them to be notified when you have completed your lengthy operation. The event handling mechanism in C# uses delegates, which are variables that reference a function. An event handler A software routine that provides the processing for various events such as mouse movement, a mouse click, a keystroke or a spoken word. See event, event driven and event management system. is a procedure in your code that determines the actions to be performed when an event occurs, such as the user clicking a button. If your class holds a set of values, clients may want to access the values as if your class were an array. You can write a piece of code called an indexer to enable your class to be accessed as if it were an array. Suppose you write a class called Rainbow, for example, that contains a set of the colors in the rainbow. Callers may want to write My Rainbow [0] to retrieve the first color in Verb 1. color in - add color to; "The child colored the drawings"; "Fall colored the trees"; "colorize black and white film" color, colorise, colorize, colour in, colourise, colourize, colour the rainbow. You can write an indexer into your Rainbow class to define what should be returned when the caller accesses your class, as if it were an array of values. Interfaces C# supports interfaces, which are groups of properties, methods, and events that specify a set of functionality. C# classes can implement interfaces, which tells users that the class supports the set of functionality documented by the interface. You can develop implementations of interfaces without interfering with any existing code, which minimizes compatibility problems. Once an interface has been published, it cannot be changed, but it can evolve through inheritance. C# classes can implement many interfaces, although the classes can only inherit To receive property according to the state laws of intestate succession from a decedent who has failed to execute a valid will, or, where the term is applied in a more general sense, to receive the property of a decedent by will. inherit v. from a single base class. Let's look at a real-world example that would benefit from interfaces to illustrate its extremely positive role in C#. Many applications available today support add-ins. Assume that you have created a code editor for writing applications. This code editor, when executed, has the capability to load add-ins. To do this, the add-in must follow a few rules. The DLL (1) See data link layer. (2) (Dynamic Link Library) An executable program module in Windows that performs one or more functions at runtime. DLLs are not launched by the user; they are called for by an executable program or by other DLLs. add-in must export a function called C EEn try, and the name of the DLL must begin with C Ed. When we run our code editor, it scans its working directory for all DLLs that begin with C Ed. When it finds one, it is loaded; and then it uses the GetProcAddress to locate the C E Entry function within the DLL, thus verifying that you followed all the rules necessary to create an add-in. This method of creating and loading add-ins is very burdensome because it bur- dens the code editor with. more verification duties than necessary. If an interface were used in this instance, your add-in DLL could have implemented an interface, thus guaranteeing that all nece ssary methods, properties, and events were present with the DLL itself and functioning as documentation specified. Attributes Attributes declare additional information about your class to the CLR. In the past, if you wanted to make your class self-describing, you had to take a disconnected approach in which the documentation was stored in external files such as IDL (1) (Interface Definition Language) A language used to describe the interface to a routine or function. For example, objects in the CORBA distributed object environment are defined by an IDL, which describes the services performed by the object and how the data or even HTML HTML in full HyperText Markup Language Markup language derived from SGML that is used to prepare hypertext documents. Relatively easy for nonprogrammers to master, HTML is the language used for documents on the World Wide Web. files. Attributes solve this problem by enabling you, the developer, to bind information to classes - any kind of information. For example, you can use an attribute to embed em·bed also im·bed v. em·bed·ded, em·bed·ding, em·beds v.tr. 1. To fix firmly in a surrounding mass: embed a post in concrete; fossils embedded in shale. documentation information into a class. Attributes can also be used to bind runtime information to a class, defining how it should act when used. The possibilities are endless, which is why Microsoft includes many predefined attributes within the NET Framework. Compiling C# Running your C# code through the C# compiler produces two important pieces of information: code and metadata. The following sections describe these two items and then finish up by examining the binary building block of NET code: the assembly. Microsoft intermediate language (MSIL (MicroSoft Intermediate Language) See CLI. ) The code that is output by the C# compiler is written in a language called Microsoft Intermediate Language, or MSIL. MSIL is made up of a specific set of instructions that specify how your code should be executed. It contains instructions for operations such as variable initialization in·i·tial·ize tr.v. in·i·tial·ized, in·i·tial·iz·ing, in·i·tial·iz·es Computer Science 1. To set (a starting value of a variable). 2. To prepare (a computer or a printer) for use; boot. 3. , calling object methods, and error handling, just to name a few. C# is not the only language in which source code changes into MSIL during the compilation process. All .NET-compatible languages, including Visual Basic .NET ' Visual Basic .NET (VB.NET) is an object-oriented computer language that can be viewed as an evolution of Microsoft's Visual Basic (VB) implemented on the Microsoft .NET framework. and Managed C++, produce MSIL when their source code is compiled. Because all of the .NET languages compile to the same MSIL instruction set, and because all of the .NET languages use the same runtime, code from different languages and different compilers can work together easily. MSIL is not a specific instruction set for a physical CPU CPU in full central processing unit Principal component of a digital computer, composed of a control unit, an instruction-decoding unit, and an arithmetic-logic unit. . It knows nothing about the CPU in your machine, and your machine knows nothing about MSIL. How, then, does your .NET code run at all, if your CPU can't read MSIL? The answer is that the MSIL code is turned into CPU-specific code when the code is run for the first time. This process is called "just-in-time" compilation, or JIT JIT - dynamic translation . The job of a JIT compiler (Just-In-Time compiler) A compiler that converts program source code into native machine code just before the program is run. In the case of Java, a JIT compiler converts Java's intermediate language (bytecode) into native machine code as needed. is to translate your generic MSIL code into machine code that can be executed by your CPU. You may be wondering about what seems like an extra step in the process. Why generate MSIL when a compiler could generate CPU-specific code directly'.? After all, compilers have always done this in the past. There are a couple of reasons for this. First, MSIL enables your compiled code to be easily moved to different hardware. Suppose you've written some C# code and you'd like it to run on both your desktop and a handheld device. It's very likely that those two devices have different types of CPUs. If you only had a C# compiler that targeted a specific CPU, then you'd need two C# compilers: one that targeted your desktop CPU and another that targeted your handheld CPU. You'd have to compile your code twice, ensuring that you put the right code on the right device. With MSIL, you compile once. Installing the .NET Framework on your desktop machine includes a JIT compiler that translates your MSIL into CPU-specific code for your desktop. Installing the .NET Framework on your handheld includes a JIT compiler that translates that same MSIL into CPU- specific code for your handheld. You now have a single MSIL code base that can run on any device that has a .NET JIT compiler. The JIT compiler on that device takes care of maki ng your code run on the device. Another reason for the compiler's use of MSIL is that the instruction set can be easily read by a verification process. Part of the job of the JIT compiler is to verify your code to ensure that it is as clean as possible. The verification process ensures that your code is accessing memory properly and that it is using the correct variable types when calling methods that expect a specific type. These checks ensure that your code doesn't execute any instructions that could make the code crash. The MSIL instruction set was designed to make this verification process relatively straightforward. CPU-specific instruction sets are optimized for quick execution of the code, but they produce code that can be hard to read and, therefore, hard to verify. Having a C# compiler that directly outputs CPU-specific code can make code verification difficult or even impossible. Allowing the .NET Framework JIT compiler to verify your code ensures that your code accesses memory in a bug-free way and that variable types are properl y used. Metadata The compilation process also outputs metadata, which is an important piece of the .NET code-sharing story. Whether you use C# to build an end-user application or you use C# to build a class library to be used by someone else's application, you're going to want to make use of some already-compiled NET code. That code may be supplied by Microsoft as a part of the NET Framework, or it may be supplied by a user over the Internet. The key to using this external code is letting the C# compiler know what classes and variables are in the other code base so that it can match up the source code you write with the code found in the precompiled code base that you're working with. Think of metadata as a 'table of contents' for your compiled code. The C# compiler places metadata in the compiled code along with the generated MSIL. This meta- data accurately describes all the classes you wrote and how they are structured. All of the classes' methods and variable information is fully described in the metadata, ready to be rea d by other applications. Visual Basic NET, for example, may read the metadata for a.NET library to provide the IntelliSense capability of listing all of the methods available for a particular class. If you've ever worked with COM (1) (Computer Output Microfilm) Creating microfilm or microfiche from the computer. A COM machine receives print-image output from the computer either online or via tape or disk and creates a film image of each page. (Component Object Model), you may be familiar with type libraries. Type libraries aimed to provide similar "table of contents" functionality for COM objects A software component that conforms to Microsoft's Component Object Model (COM). See COM. . However, type libraries suffered from some limitations, not the least of which was the fact that not all of the data relevant to the object was put into the type library. Metadata in .NET does not have this shortcoming short·com·ing n. A deficiency; a flaw. shortcoming Noun a fault or weakness Noun 1. . All of the information needed to describe a class in code is placed into the metadata. You can think of metadata as having all of the benefits of COM type libraries without the limitations. Assemblies Sometimes, you will use C# to build an end-user application. These applications are packaged as executable files See executable code. with an extension of.EXE Exe (ĕks), river, c.55 mi (90 km) long, rising in the Exmoor, Somerset, SW England, and flowing S across the Cornwall peninsula, past Exeter to the English Channel at Exmouth. . Windows has always worked with EXE files (EXEcutable file) Pronounced "ex-ee file." The name given to a program in machine language that is ready to run in DOS, Windows, OS/2 and VMS. The name comes from the .EXE extension at the end of the program name; for example: XYZ.EXE. as application programs, and C# fully supports building EXE flies. However, there may he times when you don't want to build an entire application. Instead, you may want to build a code library that can be used by others. You may also want to build some utility classes in C#, for example, and then hand the code off to a Visual Basic .NET developer, who will use your classes in a Visual Basic .NET application. In cases like this, you won't be building an application. Instead, you'll be building an assembly. An assembly is a package of code and metadata. When you deploy a set of classes in an assembly, you are deploying the classes as a unit; and those classes share the same level of version control, security information, and activation requirements. Think of an assembly as a 'logical DLL." If you're familiar with Microsoft Transaction Server A TP monitor for Windows NT servers from Microsoft that supports transaction-based applications on LANs, the Internet and intranets. It also serves as the infrastructure for a multi-tier system. It is used in the middle tier between the client and the database server. or COM+, you can think of an assembly as the .NET equivalent of a package. global assemblies. When you build your assembly, you don't need to specify whether you want to build a private or a global assembly. The difference is apparent when you deploy your assembly. With a private assembly, you make your code available to a single application. Your assembly is packaged as a DLL, and is installed into the same directory as the application using it. With a deployment of a private assembly, the only application that can use your code is the executable that lives in the same directory as your assembly. If you want to share your code among many applications, you might want to consider deploying your code as a global assembly. Global assemblies can be used by any.NET application on the system, regardless of the directory in which it is installed. Microsoft ships assemblies as a part of the .NET Framework, and each of the Microsoft assemblies is installed as a global assembly. The .NET Framework contains a list of global assemblies in a facility called the global assembly cache The Global Assembly Cache or GAC is a machine-wide .NET assemblies cache for Microsoft's CLR platform. The approach of having a specially controlled central repository, addresses the shared library concept while helping to avoid pitfalls of other solutions that lead to , and the NET Microsoft Framework SDK (Software Developer's Kit) See developer's toolkit and Windows SDK. SDK - Software Developers Kit (or "Software Development Kit"). includes utilities to both install and remove assemblies from the global assembly cache. C# Bible Jeff Ferguson Published September 2002 A Wiley Paperback Original, [pounds sterling]25.95 A modem object-oriented language object-oriented language - object-oriented programming used on the NET framework. C# combines the high level of flexibility enjoyed by programming in C and C++, with the power and productivity of languages such as Visual Basic. The C# Bible caters for the novice as well as the serious professional, combining tutorials on the fundamentals for beginners with in-depth coverage of all aspects of NET development to act as a reference resource for busy professionals. A major advantage for programmers is the ability to accommodate the new web technologies. C# includes built-in support to turn any component into an XML XML in full Extensible Markup Language. Markup language developed to be a simplified and more structural version of SGML. It incorporates features of HTML (e.g., hypertext linking), but is designed to overcome some of HTML's limitations. Web service that can be invoked over the internet from any application running from any platform. The final two portions of the C# Bible are dedicated to showcasing C# as an application development platform, illustrating the role of C# in desktop-, Web, database and component-based applications. Ready to run sample code from the book is available from the bonus companion web site. www.wiley.co.uk |
|
||||||||||||||

ti·di·men
Printer friendly
Cite/link
Email
Feedback
Reader Opinion