Namespace in C#
Namespaces group related classes and types, and they define
categories in which we can include any new class that provides related
functionality.
namespace MyCompany.r4r
{
class MyClass
{
// some code here
}
}
namespace MyCompany.r4r
{
class MyClass1
{
// some code here
}
}
|
Classes and Object in C#
Defining Classes: To define a new type or class, first declare it, and then define
its methods and fields. Declare a class using the
class keyword. The
complete syntax is as follows: [attributes] [access-modifiers] class
identifier [:base-class]
{
class-body
}
For Example.
public class Test
{
public static int Main( )
{
Console.Writeline("This is Class");
}
}
|
Defining Object: A distinction is drawn between
value types and reference types. The primitive C# types (int, char, etc.) are
value types, and are created on
the stack. Objects, however, are reference types, and are created on the heap,
using the keyword new, as in the
following:
t does not actually contain the value for the
test class
object; it contains the address of that (unnamed) object that is created
on the heap. t itself is just a
reference to that object.
How to create a program in
C#?
Step 1: Start notepad from Start ->
Program Files -> Accessories -> Notepad so that you can write the HelloWorld
program. The program you write in C# is also called as source code.
Step 2: Write the HelloWorld program,
you can either type the program shown below into notepad or just copy-paste it
from here-
public class Helloworld
{
public static void Main()
{
System.Console.WriteLine("you are welcome in world of C#");
}
}
|
Step 3: Once you have finished typing
your program you should Save the source code file. In fact after making any
changes to your source code, you should always save the file. To save the
file for the first time in notepad click on File menu -> Save As. In the Save As
dialog select the directory from the Save In dropdown where you want to save
your files, I generally save my files to C:\csharp, and then in the File name
textbox, enter the file name as HelloWorld.cs (although you can provide any name
you want but it should have an extension.cs). and click Save. Once you have
saved the
source code, and if you make any further modifications, in notepad use the
Ctrl+S keyboard short-cut to save your source code file.
Step 4: Since you have finished
writing the source code its time to compile it. Since we are using a
command-line compiler that ships with the .NET SDK, start the command prompt
from Start -> Program Files -> Accessories -> Command Prompt. Or go to Start ->
Run, type cmd and press enter. Now from the command prompt navigate to the
directory where you have stored the source code file by issuing the following
DOS commands.cd\ -To navigate to the root of the derived csharp - To navigate to
the csharp directory. Once you are in the csharp directory where you
saved the source code file earlier, its time to run the C# Compiler csc.exe.
Issue the following command to compile our HelloWorld.cs
program:csc HelloWorld.cs
Step 5: If the compilation of the
source code was successful then a new executable (Exe) file by the name
HelloWorld.exe will be created in the directory you compiled the source code. To
execute the program simply type the name of the executable file at the command
prompt.
Points to Remember
- C# code can be written in any text editor like notepad.
- C# Source Code files are saved with the extension.cs.
- C# is a case-sensitive language so you have to be careful while typing.
- C# runs on the .NET Platform, hence you need to install the .NET SDK in
order to compile C# programs.
- The C# compiler is contained within the file csc.exe, which generally
resides in the C:\windows\Microsoft. NET\Framework\v1.0.4322 directory.
Tolal:0 Click:
Show All Comments