Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSharp.NET Tutorial – Day 6


In previous article, we started learning C# Programming language and discussed History of the language, design goals of C# listed by ECMA, versions of C#, new features added in C# 2.0, new features added in C# 3.0, new features added in C# 4.0, features added in C# 5.0, etc.
Please find below the link for accessing the article
 CSharp.NET Tutorial Day 6

Now, in this article we will discuss what is procedural Programming, entry point of a programming, what is object oriented approach, what is class and Object, CPP program, Java Program, what are modifiers, how to write a C# program, how to compile the C# Program and explanation of each line in the example program, and also discuss what is the namespace and how we can import a namespace in the program

Programming structure under various approaches

Procedural Programming
A procedural program like C language is a collection of members (Variables, Functions).

Eg: - collection of members

void main( ) -> Entry point
{
-> call the members from here for execution
}

Eg: - collection of members

void main( ) -> Entry point
{
-> call the members from here for execution
}

The members of a program should explicitly call for execution from main function because most of the programming languages start their execution from main, which is the entry point of a program.  A procedural program does not provide security of the code as well as reusability of the code.

Object Oriented Approach

Object oriented programming came into existence in 70's to resolve the drawbacks of procedural language like security and reusability.

In object oriented programming, program is also a collection of members, but we will define under a special container known as “class,” which provides the basic security for the content present in it.



What is a Class?
A “class” is a user defined type, which contains members like “functions” and “variables” and we can also call it as a “Wrapper” and it provides security for its content.

Members defined under a ‘class’ cannot be accessed directly.  They can only be accessed using the ‘object’ of a class in which they are defined.

Object of a Class
First of all, we must be aware of “types” cannot be consumed directly.  To consume a type, first we need to create a copy of it because memory allocation is done not for a type but for the copy of the type.

Eg:-
int = 100 à Invalid
int x=100 à Valid

As class is also a type, the same rules should be applied to it also.  To consume a class, first we need to create a copy of the class which is known as object of class and using that object we can invoke (or) call members of a class.

CPP program
class Example
{
collection of members
};
void main( ) <-- entry point
{
-create the object of a class
-using the object invokes members of the class
}

C++ language suffers from a criticism that it was not fully object oriented because as per the standards of object oriented programming, each and every member of a class should be inside the class, because if it is inside the class it should be called using object of the class which is getting created under "Main" only.

The solution for the above problem was given by ‘Java’ with a concept of “Static” members, which do not require the object of a class for invocation.

So if “Main” function of a class declared as static, it can start the execution without any object creation and under main object it gets created for invoking other members of the “class.”

Java Program

class Example
{
call of members
static void main( ) -->entry point
{
-create the object of class
-using the object invokes members of class
}
}

While designing C# language, the designers have adopted same guidelines given by Java by defining Main as static so that it can execute without object of a class.  So, a C# program looks like a java program that we have seen above.

Note:  If at all a ‘class’ contains only ‘Main’ method in it to execute the class, we don’t require to create object of a class.

Now, we will see minimum prerequisites in order to work with C# programming

Hardware
2GB to 3GB Hard Disk space
Minimum 1GB RAM

Software
  • Windows Operating system Service pack3 (Recommended)
  • Vista (Not recommended)
  • Windows7
  • Visual studion.NET 2010
  • SQL Server 2005/2008
  • Oracle
  • MS-Office
Books
C# 2008 for developers (Dietel series)
(Sans publications)
C#3.0 unleashed (Sans)

Websites
www.msdn.com
www.csharpcorner.com
www.dot-net-programs.blogspot.in

Syntax to define a class in C#

(Modifiers) class<Name>
{
statements
}

“Modifiers” are special keywords like public, static, sealed etc, which can be used on a ‘class,’ but it is optional.

C# is a case sensitive language, so while writing the code we need to adopt the following restrictions.

1) All keywords should be used in lower case only.
2) While consuming the libraries from the base class, libraries adopt proper case convention that is first character of every word should be in upper case.

Syntax of Main Method

static void/int Main(string[ ] args)
{
statements
}

Main method should be explicitly defined as static, if at all we want to execute it without an object of class.

“Main” can be either non-value returning or can also return “int” as output.

If required, main method can be passed with ‘string array’ as a parameter.

Writing a Program
Open Notepad and write the following code in it.

class Example
{
static void Main()
{
System.Console.WriteLine ("My First Program");
}
}

Save the above program as “Example.cs” in your desired folder
(Eg: - C:\Csharp)

Note:  A C# Program should be saved with “.cs” extension

Compiling the Program
We need to compile the program by using C# compiler through visual Studio Command Prompt as following:
"csc<file name>"

To open Visual Studio Command prompt, follow the following steps

-> start menu
-> Programs
-> MS Visual studio
-> Visual studio Tools
-> Visual studio command prompt, click on it

After opening the visual studio first go to the folder, where the program has been saved and compile the program as below

C:\CSharp>csc Example.cs

Once the program is compiled successfully, it generates a file named Example.exe, which contains IL code in it.

Executing the Program
To execute the above program, run it from your command prompt as below.

C:\CSharp>Example

Now, we will discuss what is the System.Console.WriteLine

System.Console.WriteLine
Console is a predefined class under the Base Class Libraries which provides a set of IO functionalities, which can be performed on IO devices and it also contains static methods like

Write
WriteLine
ReadLine

System is a namespace, where a namespace is a logical container of types, which are used in two different scenarios.
1) Grouping types which are related to each other
2) To overcome the naming collision that is defining of multiple types with the same name by putting them under different namespaces.

Importing a Namespace
If we want to consume any class that is defined under a namespace, we must refer to the class by pre-fixing with the namespace every time, which increases the complexity in writing the code as well as volumes of the code.

To overcome the above problem, we can utilize a process known as importing of namespace i.e. on the top of a class, we can use a statement “using” followed with the namespace name, so that all the types under that namespace can be consumed without prefixing with namespace again.

Eg:

using System;
class UsingDemo
{
static void Main()
{
Console.WriteLine ("Importing namespace");
}
}


CSharp.NET Tutorial - Day 7


This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here

Share the post

CSharp.NET Tutorial – Day 6

×

Subscribe to Dot Net Programming (c#, Asp.net, Ado.net, Wcf, Wpf, Ajax, Linq)

Get updates delivered right to your inbox!

Thank you for your subscription

×