C# Fundamentals

Ksuresh
Posted by in C# category on for Beginner level | Points: 250 | Views : 24385 red flag
Rating: 3 out of 5  
 3 vote(s)

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 

  1.  Encapsulation
  2.  PolyMorphism and
  3.  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.

Page copy protected against web site content infringement by Copyscape

About the Author

Ksuresh
Full Name: suresh reddy
Member Level: Starter
Member Status: Member
Member Since: 3/24/2011 3:29:04 AM
Country: India

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Vishvvas on: 7/22/2011 | Points: 25
Good attempt Suresh but it would be always good practice to confirm all the concepts with already defined standards and research and your attention is invited to following facts
e.g. (i)OOPS is not because of complexity of earlier techniques but its a way to make programs model the way people think and interact/deal with real world.Its one of the programming paradigm (other paradigms are imperative paradigm- languages such as Pascal or C,logic programming paradigm (Prolog),functional programming paradigm-languages such as ML, Haskell or Lisp)
(ii)Encapsulation doesn't provide any security to data but it privides a mechanism to restrict the access to data. Encapsulation is materialized through use of classes and it can't be said to have unit
(iii)Inheritance is not synonymous to reusablity rather reusability can be also achieved through aggregration, composition, association etc.
(iv)C# is object oriented language but also violates few principles of OOPS. Pure OOPS languages are Smalltalk, Scala, Eiffel, JADE etc
Hope this helps.
Posted by: Ksuresh on: 7/26/2011 | Points: 25
Thank You Vishvvas
your feed back helps me learn more concepts with perfectly.
for your response point,
i. i accepted with your first point explanation.
ii.yes, i will update my article as encapsulation provides a mechanism to restrict the data.
iii. Here i am not saying that ' inheritance is synonymous to re usability' but inheritance means as like re usability process.
iv.yes, i agree with you according to the reference of http://stackoverflow.com/questions/3356464/is-c-a-100-object-oriented-programming-language.

Login to post response

Comment using Facebook(Author doesn't get notification)