Structure and Algorithm
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the author. This book was prepared with LaTeX and reproduced from camera-ready copy supplied by the author. The book is typeset using the Computer Modern fonts designed by Donald E. Knuth with various additional glyphs designed by the author and implemented using METAFONT.
Dissecting a C# Application: Inside SharpDevelop
The developers who created SharpDevelop give you an inside track on application development with a guided tour of the source code for SharpDevelop. They will show you the most important code features and explain how you can use these techniques in your own projects. You will gain valuable experience of building an application on this scale, learning from the decisions, mistakes, problems and solutions that lead to the current version on SharpDevelop.
Sams Teach Yourself C# in 24 Hours
With Microsoft's introduction of the .NET platform, a new, exciting programming language was born. C# is the language of choice for developing on the .NET platform, and Microsoft has even written a majority of the .NET Framework using C#. C# is a modern object-oriented language designed and developed from the ground up with a best-of-breed mentality, implementing and expanding on the best features and functions found in other languages. C# combines the power and flexibility of C++ with the simplicity of Visual Basic.
Welcome to the first installment on a series of 12 volumes dedicated to the latest technology to hit the third rock from the sun. But before diving in and getting both feet wet, a wee bit of background. Microsoft has always had a habit of doing things in style. Whether it was the basic yet robust Disk Operating System (DOS) or the (user-friendly?) Windows Operating System, Microsoft has always managed to rewrite the rules in the computer world. In fact it won't be exaggeration to say that today it is thanks to the efforts of Microsoft and the vision of its founder, Bill Gates, that it is software and not machines, that occupy centrestage. But then that is another story altogether.
C# is pronounced as "C sharp". It is a new programming language that enables programmers in quickly building solutions for the Microsoft .NET platform. Today, one cannot, just cannot, afford to ignore C#. It is our considered opinion that it holds immense promise and we are going to try our best, through this book, to help you realize its potential. Be assured, we are not going to teach you just another programming language. It is our intention to help you apply C# in practical situations, to actually implement your newly acquired knowledge on the Net.With this brief introduction, let us embark on a path that will take you to new adventures in the world of Internet. In this chapter, we will get you started with C# by introducing a few very simple programs. For remember, even a journey of a thousand miles must begin with a single step.
Normally, students have an implicit trust in their teachers. For they know that if the blind lead the blind, both shall fall into the ditch. However, our philosophy is that a good teacher has to be an even better liar.In accordance with this belief, initially we told you that WriteLine is the name of a function. Then we told you that the name is not WriteLine, it is System.Console.WriteLine. But even that's not true. Now comes the moment of truth. Console is actually the name of a class. Yes, trust us! There is no crying wolf here. The class Console has a function called WriteLine. Hence the name now becomes Console.WriteLine.
Components and Database Handling
Failing to prepare is preparing to fail. Ergo, one is constantly planning and preparing for the future. We often set a path for ourselves and try to follow it steadily. But then life intervenes. Uncertainties in life result in unforeseen situations. These are like exceptions in the normal course that we set for ourselves. Similarly, when you write programs, unforeseen problems may arise during its normal path of execution.
These unforeseen problems are nothing but an euphemism for errors. Just as in life, in the programming world, these errors can be further classified into Fatal errors and Non-Fatal errors. A Fatal error is an error that brings the program to a grinding halt. A Non-Fatal error is an error that allows your program to run but with limited capacity.
Web Enabling Data
We now have to our credit, programs that enable execution of a general purpose select statement as well as those that update, insert and delete commands. These were all C# programs which were compiled into executable files. Let's assume you want to display the contents of the customer table. Our program will very well serve this purpose. Alternatively, the program can be made to accept user input and depending upon the table name that the user typed in, the program can display contents of that table. You can easily do so with the help of a function called GetLine in Console. GetLine will cause the program to wait at the console for the user input followed by the 'Enter' key.
Whenever a class is created by us we want to have the ability to decide who can access certain members of the class. In other words, we would like to restrict access to the members of the class. The basic rule is that members of a class can freely access each other. There is no way one can prevent a function of a particular class from executing another function in the same class. By default though, the same class is allowed complete access but no one else is granted access to the members of the class.
Properties and Indexers
Properties are a natural extension to fields. Very few programming languages support the notion of a property. Unlike a variable, a property is not stored in a memory location. It is made up of functions. Thus even though a property and a field share the same syntax a property has the advantage that code gets called. When we initialize a variable, no code in our class gets called. We are not able to execute any code for a variable access or initialization at all. In the case of a property, we can execute tons of code. This is one singular reason for the popularity of a product like Visual Basic - the use of properties. One simple example is setting the value of a variable.
Let?s explain what operator overloading is all about with an example of a class that represents a date. Would it not be great if we could subtract two date objects and be returned an int representing the number of days elapsing between the two dates. We would like to use the good old subtraction operator ? like we do when subtracting numbers. Also we would like the > operator to compare two date objects and tell us which one is larger. The + operator could add a number to a date resulting in a new date object. Thus, operator overloading lets us redefine the existing operators so that they work with classes/objects we create like yyy. We have not yet instructed C# on how to use the trusty old + operator with two yyy objects.
Nuts and Bolts
This is our second book on C# so we have assumed that you have read our first book, 'C# - The Basics'. It contains a lot more on the C# language that we could not cover in the first one. We have read a lot on what Microsoft and others have written, and wherever we liked the idea, we simply copied it. And because we have picked from so many places, there was just no way we could mention each and every of them. If you like an idea in this volume, please do the same. The objective is to spread the C# message.
The first function to be called in any C# program is Main. Obviously someone at Microsoft tossed a coin and as it fell 'heads', they chose Main. Had it been 'tails', one wonders what the first function would have been called. As everything starts from Main, it is also called the entry point of the program. It is the fountainhead of all knowledge. Incidentally, the C programming language also calls Main as its first function for all C-language programs.
A declaration creates a name in a declaration space. The only declaration that can have the same name is a constructor, method, indexer or an operator. These are the only entities that can be overloaded in a type. We cannot have a field and a method with the same name in a declaration space. Each time we create a new class, struct or interface, a new declaration space is created. The only entities in a class/struct that can share the same name as the class/struct is a instance constructor or a static constructor. Base class and derived classes live in separate declaration space, hence the derived class can hide names created in the base class. An enum also creates its own declaration space.
Delegates and Events
A delegate is extremely important for C# as it is one of the four entities that can be placed in a namespace. This makes it shareable among classes. Delegates are fully object oriented as they entirely enclose or encapsulate an object instance and a method. A delegate defines a class and extends System. Delegate. It can call any function as long as the methods signature matches the delegates. This makes delegates ideal for anonymous invocation. The methods signature only includes the return type and the parameter list. If two delegates have the same parameter and return type, that is, they share the same signature, we consider them as different delegates.
The C# programming language has exactly 78 keywords in all. But there are only two types of conversions, implicit and explicit. As the name suggest, an explicit conversion has to be specifically supplied by using a cast and at all other times, the conversion is considered to be implicit. Conversion comes into play when converting from a source type to a target type, which is finally the return value of the operator.
We all like to express ourselves in different ways. Some smile, some laugh, some screech and some even shout. The C# programming language lays a lot of emphasis on expressions and has one of the largest chapters in its documentation devoted to it. An expression is a sequence of operators and operands providing some computational activity. An expression can be built in n number of ways. It can be as simple as a value resulting in a certain type. It can also consist of a variable with a type. A namespace can also occur in an expression but it must be only at the beginning. A type like a namespace can also occur on the left hand side of an expression. The usual gang of idiots like properties, indexers, properties can also be included in an expression.
There are only two types of entities that can contain members, namely, types and namespaces. These members can be accessed using the same rules i.e. the name of an entity to be followed by a dot '.' and then the member name to be given. This cardinal rule is never ever broken. All derived classes inherit every member of a base class with the exception of constructors and destructors. This rule applies to private members also. Regardless of inheriting all the members, the base class members may/may not be accessible to the derived class members. Thus, the access modifiers have nothing to contribute to inheritance. In other words, the rules of inheritance ignore the access modifiers.
Compiler Error Massages
We will come across a large number of errors that state Internal Compiler Error. These errors are extremely dangerous as the compiler itself cannot figure them out. They fall under two broad categories. One, we may have made some dumb mistake which the developers at Microsoft may have not even thought off, thus confusing the compiler thoroughly. Secondly, there could be a genuine bug in the compiler.
Thus, the very first compiler error number is thrown when the compiler is totally confused and doesn't know how to react to the error. The compiler will resort to this error as the last error before breaking down. Please understand the irony. The guys who wrote the compiler could have made it the last error number but symbolically made it the first.
C# Classes-Threads,WinForms and XML
This book presents myriad fascinating concepts about C# classes. It is classified into three sections, with each of them converging on distinct facets of classes available in the .Net framework. The curtains are raised with the chapter on Threads in Section I, which presents a nascent introduction to this topic. The topics of Events and Mutexes are also explored in detail. The next chapter is on the Internet related classes. Here, the utility of the Web classes and their role in building server and client applications are highlighted. The last chapter in this section takes on a different hue. It encapsulates the crux of writing programs to implement the concept of 'remoting'. This involves the composition of a client and a server program on different machines.
Threads, Events and Mutexes
Threads operate on the premise that the computer is definitely more expeditious than a human at executing tasks, resulting in the computer idling away most of its processing time, waiting for the human operator. Threads are a means of overcoming this wastage of processing time. They perform multiple tasks on a computer in rapid succession, thereby, creating the illusion of these tasks being executed simultaneously. No application can ever be comprehensive without employing threads. Before we try to infer what a thread does in the programming context, let us rummage through a few examples given below.
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: April 1, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles