0% found this document useful (0 votes)
13 views

Understanding The HELLO WORLD Application Code

C # notes

Uploaded by

rajuvathari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Understanding The HELLO WORLD Application Code

C # notes

Uploaded by

rajuvathari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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


The first line of our program was:

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

need to write in order to access the classes defined in Collection


namespace which is a sub/internal namespace of System namespace.

The class Keyword


 All of our C# programs contain at least one class.
 The Main() method resides in one of these classes.
 Classes are a combination of data (fields) and functions (methods)
that can be performed on this data in order to achieve the solution
to our problem.
 Classes in C# are defined using the class keyword followed by the
name of class.

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)
{
-------
-------
}

Printing on the Console


The following line prints Hello World on the Console screen:

Console.WriteLine(“HelloWorld”);

Here we called WriteLine(), a static method of the Console class defined


in the System namespace. This method takes a string (enclosed in double
quotes) as its parameter and prints it on the Console window.

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.

The comment ends with the end of the line:


// This is my main method of program
static void Main(string[] args)
{
------
}

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 */
-------
}

C# introduces another kind of comment called 'documentation


comments'. C# can use these to generate the documentation for your
classes and program. These are line comments and start with triple slash
mark (///):

/// These are documentation comments

The System.Console Class


 In C#, the Console class is used to represent the standard input,
output, and error streams for the console applications.
 You are not allowed to inherit Console class.
 This class is defined under System namespace. This class does not
contain any constructor.
 This class contains a set of static methods like WriteLine(),
ReadLine(), Write(), Read() which can be execute by calling with its
class name because those are static method.
Write()
 This method is used to display any message to the user in the
output stream.
 After displaying the message blinking cursor remains in the same
line.

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.

 Environment class is a static class that provides the system


configuration, Current program execution Environment as well some
properties for string manipulation such as news line, System
Namespace represents the Environment Class.

You can access the particular property or function as:


Lab Program # 1)
Write a C# program to show the machine details like machine
name, Operating System, Version, Physical Memory and calculate
the time since the Last Boot Up. (Hint: Use System. Environment
Class)

using System;

namespace Lab1

class Program

static void Main(string[] args)

int tickCount = 0;

tickCount = Environment.TickCount;

Console.WriteLine("The time since the Last Boot Up: " + tickCount);

Console.WriteLine("OSVersionis="+Environment.OSVersion.Version.ToStr
ing());

Console.WriteLine("Machine name is= " +Environment.MachineName);

Console.WriteLine("OS Platform: "


+Environment.OSVersion.Platform.ToString());

foreach (String s3 in Environment.GetLogicalDrives())

Console.WriteLine("Drive: " + s3 ); Console.ReadLine();

OUTPUT

(Execute this program in your System and Write the output below)

You might also like