Monday, January 24, 2011

Creating and running or first application


Creating and running or first application
 
1. Start Visual Studio.
2. On the File menu, under New, click Project. Or use Ctrl + Shift + N, alternatively we can open the new project dialog box from the start page by clicking on New Project. Figure 3.1 shows the new project window and the highlights are discussed in next points.



Figure 3.1
3. In Installed Templates Categories pane, expand Visual C#, and then click Windows.
4. In the Templates pane, click Console Application.
5. Type a name “Hello Word” for our project in the Name field.
6. Click Browse button and select the location where you want to save the project
7. Click OK.
The new project appears in Solution Explorer.
8. By default Program.cs file is added to the solution and it will be open for code editing. If Program.cs is not open in the Code Editor, right-click Program.cs in Solution Explorer and then click View Code. Or click on view code button after selecting program.cs file as shown in figure 3.2

Figure 3.2
9. Replace the contents of Program.cs with the code shown in code snippet 3.1.
Code Snippet 3.1
using System;

namespace Hello_world
{
    class Program
    {
        static void Main(string[] args)

       {
            Console.WriteLine("Hello World!");
            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
10.         Press F5 to run the project. A Command Prompt window appears that contains the line Hello World! Figure 3.3 shows the output of the program

Figure 3.3
Code Explanation 3.1:

Comments
// A Hello World! program in C#.

The first line contains a comment. The characters “// convert the rest of the line to a comment. This will be ignored by the compiler. we can also comment out a block of text by enclosing it between the /* and */ characters this is called multiline comment while the former was single lie comment. This is shown in the following example.
/* A "Hello World!" program in C#.
This program will display the string "Hello World!" on the screen. */

A C# console application must contain a Main method, as this is the entry and exit point of the application. The Main method is where we create objects of other classes and execute other methods.
The Main method is a static (C# Reference) method that can exist in a class or a struct. In our "Hello World!" example, it exist in a class named Program. we can declare the Main method in one of the following ways:
·         It can return void as in this example.
static void Main()
{
    //...
}
 
 
·         It can also return an integer.
static int Main()
{
    //...
    return 0;
}
 
 
·         With either of the return types, it can take arguments as in our example although we are not using those arguments so if we want we can eliminate them.
static void Main(string[] args)
{
    //...
}
 
 
-or-
static int Main(string[] args)
{
    //...
    return 0;
}
 
When we are using void as the return type for Main method then we need not to use any return statement but if we use int instead of void then return statement becomes a compulsion whether it returns any value or we bind it to return some value.
The parameter of the Main method, args, is a string array that contains the command-line arguments used to invoke the program.
The call to ReadKey at the end of the Main method prevents the console window from closing before you have a chance to read the output when you run your program in debug mode, by pressing F5.
Input and Output
C# programs use the input/output services provided by the run-time libraries of the .NET Framework. The statement System.Console.WriteLine("Hello World!"); uses the WriteLine method. This is one of the output methods of the Console class in the run-time library. It displays its string passed as parameter to the method on the standard output stream followed by a new line. Other Console methods are available for different input and output operations, which we will deal in detail later If we have already included System namespace like “using System;” directive at the beginning of the program, then we can directly use the System classes and methods without fully qualifying them. For example, you can call Console.WriteLine instead of System.Console.WriteLine: as we have done in our example
using System;
 
 
Console.WriteLine("Hello World!");
 
 
Compiling the program using Command line
We can compile the "Hello World!" program by using the command line instead of the Visual Studio Integrated Development Environment (IDE).

To compile and run from a command prompt

1. Open a Visual Studio Command Prompt window. A shortcut is available from the Start menu, under Visual Studio Tools. Alternatively, we can follow the instructions in How to: Set Environment Variables to enable command-line builds from a standard Command Prompt window.
2. Paste the code shown in the preceding procedure into any text editor and save the file as a text file. Name the file Hello.cs. C# source code files use the extension .cs.
3. In the Command Prompt window, navigate to the folder that contains Hello.cs.
4. Enter the following command to compile Hello.cs. If your program has no compilation errors, an executable file that is named Hello.exe is created.
csc Hello.cs
5. To run the program, enter the following command:
Hello

Sunday, January 23, 2011

General structure of a C# program



1. General structure of a C# program

A C# programs can consist of one or more files. Every file can contain zero, one or more namespaces. A namespace contains types for example classes, structs, interfaces, enumerations, and delegates, or other namespaces. See Code Snippet 2.1 for the structure or draft of a C# program that contains all of those elements.
Code snippet 2.1:
 
// A draft of a C# program 
using System;
namespace ProjectNamespace
{
    class MyClass
    {
    }
 
    struct MyStruct
    {
    }
 
    interface IMyInterface 
    {
    }
 
    delegate int MyDelegate();
 
    enum MyEnum 
    {
    }
 
    namespace MyNestedNamespace
    {
        struct MyAnotherStruct 
        {
        }
    }
 
    class MyMainClass
    {
        static void Main(string[] args) 
        {
            //My program starts here...
        }
    }
}
 
Code explanation 2.1:

using is the keyword used to include any namespace in our program
namespace is the keyword used to declare any new namespace will be dealt in detail 
class is keyword used to define a class will be dealt in detail
struct keyword is used to declare a structure as in c
interface keyword is used to create interface which can be implemented later we will discuss it later in detail
enum is used to create enums we will discuss it in detail in following articles
static void Main(string[] args) is the entry point for the program
 
 
Regards,
Meetu Choudhary

Introduction to C#


Introduction to C#

C# (pronounced "C sharp") is a programming language designed for building a variety of applications which can also run on the .NET Framework. C# is simple, powerful, type-safe, and object-oriented language. Maintaining the expressiveness and elegance of old C language innovations help C# for rapid application development.
Visual C# is an implementation of the C# language by Microsoft. Visual Studio supports Visual C# with a full-featured code editor, compiler, project templates, designers, code wizards, a powerful and easy-to-use debugger, and other tools. For speedy development of secure and robust applications .NET Framework class library provides access to many operating system services and other useful, well-designed classes.
C# is used to create traditional Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. In Visual C# 2010 we get an advanced code editor, convenient user interface designers, integrated debugger, and many more tools to make for easier development of applications with C# 4.0 of the .NET Framework.
.NET Framework Platform Architecture





Figure 1.1