1 - Core C# Programming Constructs - Part I
1 - Core C# Programming Constructs - Part I
1 - Core C# Programming Constructs - Part I
Constructs
PART 1
Outline
• In this lesson, we will begin our formal investigation of the C#
programming language
• In particular we will discuss about
• Main() method
• fundamental C# data types
• var keyword
• C# keywords and operators for flow control
The Anatomy of a Simple C# Program
• C# demands that all program logic be contained within a type
definition
• Unlike other languages such as C++/C, in C# it is not possible to
create global functions or global points of data.
The Anatomy of …. Cont’d
}
Variations of Main Method - 3
// int return type, no parameters.
static int Main()
{
// Must return a value before exiting!
return 0;
}
Choosing a Main Method variation
• Your choice of how to construct Main() will be based on two
questions
• Do you want to return a value to the system when Main() has
completed and your program terminates?
• Do you need to process any user-supplied, command-line
parameters?
Specifying an Application Error Code
• Vast majority of your Main() methods will return void as the
return value
• The ability to return an int from Main() keeps C# consistent
with other C-based languages.
• By convention, returning the value 0 indicates the program has
terminated successfully
• While any other value (such as -1) represents an error condition
Specifying an Application Error Code
• The value 0 is automatically returned, even if you construct a
Main() method prototyped to return void
• An application’s return value is stored within a system
environment variable named %ERRORLEVEL%.
• If you were to create an application that programmatically
launches another executable, you can obtain the exit value using
the static System.Diagnostics.Process.ExitCode
property.
Processing Command-Line Arguments
• To access the command line arguments you can iterate
through the string array parameters of the Main() method
• You can also use the GetCommandLineArgs() method of
the System.Environment type
• The return value of this method is an array of strings
• When using this approach, it is no longer necessary to define
Main() as taking a string array as the input parameter
Processing Command-Line Arguments
• During the development cycle, you might wish to specify
possible command-line flags for testing purposes with in Visual
Studio
• To do so
• Double click on the properties icon in the solution view
• Select the Debug tab on the left side of the newly opened panel
• From there, specify values using the command-line arguments text
box
The System.Console Class
• A console user interface (CUI) is not as enticing as a graphical user
interface (GUI) or web-application
• But we will restrict the early examples of the course to console
programs
• This allows us to keep focused on the syntax of C# and the core
aspects of the .NET platform
• Thus, we don’t need to deal with the complexities of building
desktop GUIs or web sites at first
The System.Console Class Cont’d
• As its name implies, the Console class encapsulates IO and
error-stream manipulations for console-based applications.
• Some members of interest are presented on the next slide
• The Console class does provide ability to
• Change background and foreground colors
• Issue beep noises
The System.Console Class Cont’d
Member Meaning
Beep() This method forces the console to emit a beep of a
specified frequency and duration.
BackgroundColor These properties set the background/foreground
ForegroundColor colors for the current output. They may be assigned
any member of the ConsoleColor enumeration
BufferHeight These properties control the height/width of the
BufferWidth console’s buffer area.
Title This property sets the title of the current console
The System.Console Class Cont’d
Member Meaning
WindowHeight These properties control the dimensions of the
WindowWidth console in relation to the established buffer.
WindowTop
WindowLeft