First C# program


Developing C# console Application using Visual studio:
Start[menu]-> Run -> devnev -- opens Visual studio if installed
then,
File[Menu]-> New -> Project-> Select Visual C# in Installed Templates -> Console Application->Name-> Click on Ok..

Structure of C# console Application:

using namespace hierarchy
namespace namespace name

{
    class ClassName
    {
        public static void Main(string[] args)
        {
             //statements
        }
    }
}

Here is a program that shows "Welcome to Memorizedotnet.blogspot.com"  in the screen.

using System;                              // Importing namespace

namespace FirstProgram
{

class Program                            // Class declaration
{
      public static void Main()        // Method declaration
      {
          Console.WriteLine ("Welcome to Memorizedotnet.blogspot.com"); // Statement
       }                                        // End of method
}                                              // End of class
}

The using directive was used to make the System namespace available to program,to use the Console class.


Method:

A method performs an action in a series of statements, called a statement block, a pair of braces containing zero or more statements. We defined a method.
Syntax:
       <modifier> <return type> <Method Name>([arguments])
Based on the return type methods are classified into two types
  1. Void method
  2. Non-void method
Void method: If a method does not returns any value we can be declare method return type as void.
Example:
       Public void Message()
      {
           Console.WriteLine("Welcome to Memorize.NET");
       }
Non-Void method: If a method returns some value we should declare method return type a concern returning value datatype.
Example:
              Public int add(int a, int b)
              {
                   return a+b;
              }

 C# recognizes a method called Main as signaling the default entry point of execution.The Main method may optionally return an integer (rather than void) in order to return a value to the execution environment. The Main method can also optionally accept an array of strings as a parameter (that will be populated with any arguments passed to the executable). 
Example:
                static int Main (string[] args)
                  {
                      ......
                  }
Console:
Console is a class which is used to provide the members for supporting console application.
WriteLine: It is a predefined method,which will display given value on the command prompt window and it will move the cursor to the next line.
Write: It is a predefined method,which will display given value on the command prompt window and it will make the cursor to be in the same line.
ReadLine: It is a predefined method,which reads string from the command prompt.
Read: It is a predefined method,which reads single character from the command prompt.

Declaring Variable:
Variable represents a temporary value which can be changed.A variable name can start with alphabet, @, _(underscore). But variable cannot start with a numeric value.
Syntax:
           <Datatype><variable_name>=<value>;
Example:
           int a=10;
           int@ a=10;
           int _a=10;
           int 4a=10; //invalid
Local variables: The variables which are declared with int the method are called as local variables.

Comments:
C# offers two different styles of source-code documentation:
Single-line comments: A single-line comment begins with a double forward slash and continues until the end of the line.
Example:
                int x = 3; // Comment about assigning 3 to x
Multiline comments: A multiline comment begins with /* and ends with */.
Example:
                       int x = 3; /* This is a comment that
                           spans two lines */


Compilation:
The C# compiler compiles source code, specified as a set of files with the .cs extension,into an assembly. An assembly is the unit of packaging and deployment in .NET. An assembly can be either an application or a library. A normal console or Windows application has a Main method and is an .exe file. A library is a .dll and is equivalent to an .exe without an entry point. Its purpose is to be called upon or referenced by an application or by other libraries. The .NET Framework is a set of libraries.

We can either use an IDE such as Visual Studio to compile or call csc manually from the command line.


To compile manually,first save a program to a file such as FirstProgram.cs
                        Use csc Program.cs in commandline
To compile using Visual studio,
project[menu]-> FristProgram properties.. -> startup object -> select  FristProgram.Program-> press ctrl+F5.

    Click here for more  : C#.NET Language Basics                   



No comments:

Post a Comment