cookieChoices = {};

Thursday, 28 November 2013

Console Application Introduction



Agenda:
1. Solution and Project in VS.NET
2. Importance of Main – Entry point of an application
3. Different forms of Main


1. Solution is a Collection of Projects. One Solution can have many projects and every project added to the solution can of same language or different language.
2. The Solution file has the extension .sln.
3. A Project is a collection of files including Source Code (.vb/.cs), Resources(.resx), Configuration(.config) files etc…
4. The project file has the extension .vbproj or .csproj
5. A project when build (compilation + linking) generates an EXE/DLL as output, which is called as PE.

Time to create our first MS.Net application: Goto File à New à Project à Select Visual C# on Left and Console Application on right. Name = “FirstConApp” – This is the project name Location = “C:Projects\\”.
class Program
{
public static void Main()
 {
Console.WriteLine("Hello C#");
                }
}

To Build the Solution Goto Menu: Build à Build Solution (Ctrl + Shift + B) To Run the Application

1. Debug à Start Debugging (F5) – Here we can use break points and debug the application.
2. Debug à Start Without Debuggin (Ctrl + F5) - We can see the output in console window.

When the project is build the following output file is generated: Drive:\...\<ProjectName>\bin\debug\<ProjectName>.Exe Note: In VS.NET 2008, at the time of creating a project we can specify the Framework Version to be used which can be either 2.0, 3.0 or 3.5.


Mostly the console based applications are developed in situations where the application should either run as a top level process or as child process (invoked from another parent process). Commandline Arguments: This is the best way of providing input to the console based application.
using System;
class Program
{
static void Main(string[] args) 'args is an array of commandline arguments.
 {
Console.WriteLine("Hello " + args[0]); 'args(0) is the first command line argument.
}
}



To give Commandline arguments in Visual Studio: View à Solution Explorer à Select Project à Right Click à Properties à Debug Tab à Command Line Arguments à Provide string separated by space (Args1 Args2 Args3) Or Give at command prompt in console window Drive:\. . . \ ProjectName\ in\debug\<ProjectName>.exe Args1 Args2 Args3

Return value of Main:
using System;
class Program
{
static int Main(string[] args) 'args is an array of commandline arguments.
                {
Console.WriteLine("Hello " + args[0]); 'args(0) is the first command line argument.
                                return 0;
                }
 }



1. When the application is not used as child process, the return value of main is not usefull because what ever may return the value, main ends the application ends and OS will clean all the memory allocated to it.
2. The return value of Main is used by the current application to communicate its state to the parent process when the application terminated. The Integer return value can be predefined with some meaning and the same will be used by the parent process to know the state of child process which terminated. The return value of an application is called as EXIT CODE of the application.

Multiple Startup classes in one project

One Project can have multiple modules but only one of many modules with valid Main can be treated as Startup or Entry Point of the application. To Add another File to Project: Goto Solution Explorer à Right Click on Project à Add à Class
using System;
class Program2
{
static void Main()
{
Console.WriteLine("Hello Program2");
}
 }
To Set Startup Object: View à Solution Explorer à Project à Right Click à Properties à Application Tab à Starup Object à Select Class whose Main should be used as entry point. Note: The class declared as startup object cannot have more that one form of Main which are valid for entry point. All classes in the project can be placed in either in same file or in different files. In either case the compiler is going to compile them together. i.e. for a dotnet language compiler files cannot be used as boundries and all the files are compiled together as one unit.

 To open a closed Solution / Project: File à Open à Project / Solution à Give the Path of .sln file.



 To develop an application without using VS.NET (compiling at command prompt)

1. Open Notepad
2. Write the code as given above and Save as C:\Demo.CS
3. Goto Start à Programs à Microsoft Visual Studio 2008 à Visual Studio Tools à VS 2008 Command Prompt (so that the directory of MS.NET Framework is set in “PATH”)
4. CSC.EXE C:\Demo.CS => Generates Demo.exe
5. Run Demo.exe


Framework Folder: C:\Windows\Microsoft.Net\Framwork\V3.5.XXXX\

No comments:

Post a Comment