Monday, January 5, 2009

C# Programming in VS 2005


A program consists of English-language instructions called source code . The syntax for these instructions is strictly defined by the language.Source code consists of a series of statements. A statement is an instruction to the complier. Each instruction must be formed correctly, and one task you'll face when learning C# will be to learn the correct syntax of the language. For example, in C#, every statement ends with a semicolon.Each instruction has a semantic meaning that expresses what you are trying to accomplish.
Compiling your source code means running a compiler and identifying the source code file. You run the compiler by opening a command prompt (DOS box) and entering the program name csc. Compiler is to turn your source code into a working program. It turns out to be just slightly more complicated than that because .NET uses an intermediate language called Microsoft Intermediate Language (MSIL, sometimes abbreviated as IL).The compiler reads your source code and produces IL. When you run the program, the .NET Just In Time (JIT) compiler reads your IL code and produces an executable application in memory. The IL code is actually stored in a .exe file, but this file does not contain executable code. It contains the information needed by the JIT to execute the code when you run it.

Simple source code file :
namespace NotePad
{
class HelloWorld
{
// every console app starts with Main
static void Main( )
{
System.Console.WriteLine("Hello world!");
}
}
}
The C# language to develop four types of applications:

-Console applications, which display no graphics
A console application runs in a console window





-Windows applications, which use the standard Windows interface
A Windows application runs on a PC's desktop



-Web applications, which can be accessed with a browser
An ASP.NET application runs on a web server and delivers its functionality through a browser




-Web services, which can be accessed using standard Internet protocols and which provide services such as current stock quotes, ISBN to title conversions, etc., that can be used by other applications



Intoduction to VS 2005 and C#

The .NET platform is a development framework that provides a new way to create Windows applications. However, .NET goes beyond traditional Windows programming to facilitate creating web applications quickly and easily.Its goal was to radically reduce the amount of boilerplate code you have to write, and to make the creation of web and desktop applications easier by "encapsulating" much of the "plumbing" of a typical application in objects, likes code to connect to databases, the Internet, or your filesystem, .NET provides fully tested controls that you can drag onto your form.
The Framework specifies how .NET programming constructs such as intrinsic types, classes, and interfaces are implemented.The most important components of the Framework are the Common Language Runtime (CLR), described in the preceding section, and the Framework Class Library (FCL ), which provides an enormous number of predefined types or classes for you to use in your programs.
C# was created specifically for .NET. Although .NET may become cross-platform some day soonthere already exists a working open-source Unix versionfor now, the overwhelming majority of .NET programs will be written to run on a machine running one of the Windows operating systems.
The C# language is disarmingly simple, but C# is highly expressive when it comes to implementing modern programming concepts. C# includes all the support for structured, component-based, object-oriented programming that one expects of a modern language built on the shoulders of C++ and Java.The goal of C# is to provide a simple, safe (bugs early in the development process and reliable code), object-oriented, Internet-centric, high-performance language for .NET development. C# is simple because there are relatively few keywords.


Basic C# in Visual Studio 2005


Statements
Program instruction is called a statement and each statement ends with a semicolon (;). The compiler starts at the beginning of a source code file and reads down, executing statement after statement in the order encountered. This would be entirely straightforward, and terribly limiting, were it not for branching . Branching allows you to change the order in which statements are evaluated. Examples :
int newvariable;
newvariable = 23;
int oldvariable = newvariable;

Types
Every object you create or use in a C# program must have a specific type (e.g., you must declare the object to be an integer or a string or a Dog or a Button).




Variables
A variable is an instance of an intrinsic type (such as int). You can initialize a variable by writing its type, its identifier, and then assigning a value to that variable. An identifier is just an arbitrary name you assign to a variable, method, class, or other element. Examples for using variable:
class Values
{
static void Main( )
{
int mycount = 7;
System.Console.WriteLine("First value, mycount: {0}",mycount);
mycount = 5;
System.Console.WriteLine("Second value, mycount: {0}",mycount);
}
}

Constants
A constant is like a variable in that it can store a value. However, unlike a variable, you cannot change the value of a constant while the program runs. Example constants :
class Values
{
static void Main( )
{
const int FreezingPoint = 32; // degrees Fahrenheit
const int BoilingPoint = 212;

System.Console.WriteLine("Freezing point of water: {0}",
FreezingPoint );
System.Console.WriteLine("Boiling point of water: {0}",
BoilingPoint );
//BoilingPoint = 21;

}
}