Understanding The HELLO WORLD Application Code
Understanding The HELLO WORLD Application Code
Namespace in C#
A Namespace is simply a logical collection of related classes in C#.
We bundle our related classes in some named collection calling it a
namespace. Namespaces are used to organize the classes.
It helps to control the scope of methods and classes in larger .Net
programming projects.
It is also referred as named group of classes having common
features. The members of a namespace can be namespaces,
interfaces, structures, and delegates.
Defining a Namespace
To define a namespace in C#, we will use the namespace keyword
followed by the name of the namespace and curly braces containing
the body of the namespace as follows:
Syntax:
namespace name_of_namespace
{
// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates
}
The using keyword above allows us to use the classes in the following
'System' namespace. By doing this, we can now access all the classes
defined in the System namespace like we are able to access the Console
class in our Main method later.
One point to remember here is using allows you to access the classes in
the referenced namespace only and not in its internal/child namespaces.
Hence we might
Example:
class Student
{
---- Members
}
The Main() Method
The Main() is an entry point in every program.
The main method should be defined as a static member in the class
by using static keyword to be execute first under the class without
object usage.
A main method can be defined as a non-value returning method by
using void keyword and also can be defined as value returning
method but of integer type only.
Ex:
static void Main(string[] args)
{
-------
-------
}
Console.WriteLine(“HelloWorld”);
Comments
Comments are the programmer's text to explain the code, are
ignored by the compiler and are not included in the final executable code.
C# uses syntax for comments that is similar to Java and C++. The text
following double slash marks (// any comment) are line comments.
C# also supports the comment block. In this case, the whole block is
ignored by the compiler. The start of the block is declared by slash-
asterisk (/*) and ends with asterisk-slash mark (*/):
public static void Main()
{
/* These lines of text
will be ignored by the compiler */
-------
}
Example:
Console.Write(“Tungal BCA Jamkhandi”);
Output : Tungal BCA Jamkhandi _
WriteLine()
This method is used to display required message to the user on the
output stream.
After displaying the message blinking cursor moves to a new line.
Example:
Console.WriteLine(“Tungal BCA Jamkhandi”);
Output: Tungal BCA Jamkhandi
_
Read()
Read method reads the single character and convert that character
into the ASCII value that means it returns the value of type integer.
ReadLine()
This will read an input stream from the console window and return
the input string when user presses the enter key.
And it convert any type of value to string type we mostly use
ReadLine() instead of Read().
Clear()
This method is used to delete the contents of screen and is same as
clrscr() in C / C++.
Example:
using System;
namespace ConsoleClassExample
{
class Consoleclass
{
static void Main(string[] args)
{
Console.Write("BCA ");
Console.WriteLine("Tungal BCA");
Console.WriteLine("Jamkhandi");
Console.ReadLine();
}
}
}
Output:
BCA Tungal BCA
Jamkhandi
The System.Environment Class
In C#, Environment Class provides information about the current
platform and manipulates, the current platform. It is useful for
getting and setting various operating system-related information.
using System;
namespace Lab1
class Program
int tickCount = 0;
tickCount = Environment.TickCount;
Console.WriteLine("OSVersionis="+Environment.OSVersion.Version.ToStr
ing());
OUTPUT
(Execute this program in your System and Write the output below)