In this article we are going learn OOPs princeples, sample program in C#, C# keywords and identifiers.
Introduction
In the way of development of programs using FORTAN and COBAL like erlier languages the complexity was increasing for larger programs. So, the developers found out structured programing language like C. Later on these structured programing languages also became complex in developing large projects , so developers come up with the Object Oriented programing (OOPs) language.
Structure programing works only for what is happening inside the code but does not explains which data is affecting (not works around data).
Object Oriented Programing works on both; around the code (what is happening) and around the data (which data is affecting).
To achieve the Object Oriented Programing concept we have to fallow three principles of OOPS conecpt and those are
- Encapsulation
- PolyMorphism and
- Inheritance.
Let us learn about each concept
Encapsulation:
It means binding the Code and Data together; encapsulation provides a mechanism to restrict the data.
C# basic unit of encapsulation is Class, C# uses a class specification to consrtuct objects. Objects are instances of the class. Code and data are memebers of the class, the data defined in the class is field.
Polymorphism:
According to greek meaning it is "manyforms". We can describe polymorphism based on programing language like this. "Method name is same but its signature is different", it means same method name we can use more than one time within a class but its signature should be different. Signature is nothing but type of parameters or number of parameters passing to that method.
method name(signature)
{
}
For example, You drive an vehicle, which has properties like wheel size, engine size, gas tank size, and other properties. The automobile you drive is a concrete implementation of the automobile interface. It has additional features, like sliding doors, logo type, cd changer slot count, moon roof lever location, or other various properties that are specific to the make/model of the vehicle.
In OO programming, Vehicle would be the base class, and each vehicle manufacturer would have its own implementation. For instance, Maruti has K-series engine technology, which is its own implementation. Volvo uses diesel engines, which is the TDI technology. More importantly, you may add a feature in the vehicle and implement make/model implementation, such as Car, Truck, or Suv etc.
- Polymorphism helps in decreasing the complexity of program.
Inheritence:
It means "Reusability". Inheritence is the process by which one object can import the properties of another object.
We can achive the reusability using hierarchies, if we will not use hierarchies then each object would have to explicitly defines all of its characteristics (spcific and general characteristics). By using inheritance process an object need only to define those qualities that make it special within its class. It can inherit its general characteristics form parent or base class object.
The concept of inheritance is used to make things from general to more specific e.g. take Animal is the general class in that we can classify animals as Mammal and Reptile () these are two derived classes of super classs Animal and dog is a child class for both Mammal and Animal classes .
C# is a complete Object Oriented Programming language from Microsoft that is inherently supported in .NET. Lets learn about C# now.
C# sample program
Create a sample.cs file in Notepad and write following code.
using System;
class sample
{
static void Main()
{
Console.WriteLine("Hello World !");
Console.Read();
}
}
Now open the Visual Studio Command Prompt and write following line of code.
C:\> Csc sample.cs
You should be get a .exe file named sample.exe. Run this exe file by double clicking and you will see a console window opens and write the text “Hello World!” Press Enter key and that window closes.
Now let me define each line of statement one by one.
static void Main():
static: a method that is declared with static keyword that method is called before an object of its class has been created.
void: it means the Main() method doesn't return a value.
In the above program if you type "main and writeline" insted of "Main and WriteLine" then compile time errors will occur. Because C# is case sensitive language and you will be forced to use the method or class name the way it has been defined in C#.
To print the result in console application Console.WriteLine(“ Hello World !”) is used and in web application Response.WriteLine(“Hello World !”) is used. In C#, all variables must be declared before they are used.
C# keywords
Keywords determines the features built in the language. C# has two types of keywords reserved and contextual keywords. The reserved keywords cannot be used as names for variables, classes and methods.
The Contextual keywords are those that have a special meanings. In special cases they act as keywords.
C# Reserved keywords
abstract |
as |
base |
bool |
break |
byte |
case |
catch |
char |
checked |
class |
const |
continue |
decimal |
deafult |
delegate |
do |
double |
else |
enum |
event |
explicit |
extern |
false |
finally |
fixed |
float |
for |
foreach |
goto |
if |
implicit |
in |
int |
interface |
internal |
is |
lock |
long |
namespace |
new |
null |
object |
operator |
out |
override |
params |
private |
protected |
public |
readonly |
ref |
return |
sbyte |
sealed |
short |
sizeof |
stackalloc |
static |
string |
struct |
switch |
this |
throw |
true |
try |
typeof |
unit |
ulong |
unchecked |
unsafe |
ushort |
using |
virtual |
volatile |
void |
while |
|
|
|
C# Context keywords
add |
dynamic |
from |
get |
global |
group |
into |
join |
let |
orderby |
partial |
remove |
select |
set |
value |
var |
where |
yeild |
|
|
Identifiers
An identifier is a name assigned to a variable name, method name or any userdefined names. Identifiers may start with Underscore (_) but should not start with a digit. Reserved keywords of C# can be used as an identifier if it preceded by "@" otherwise not.
ex: "@for" is identifier
"for" is not a identifier
Reference: I have taken reference of the C# 4.0 Complete reference book that I am going through these days.
Hope this article was useful. Do let me know your comment or feedback.
In case you want to learn ASP.NET with Tips and Tricks, I found .NET Tips and Tricks very useful.
In the next article, we are going to learn Data types, control statements etc. Thanks for reading.