Welcome to the all new exciting young world of C#. Why do we say it is young? Simply because it’s still in the process of evolving and quite unexplored, underutilized, its full potential still obscure to many users in the programming community. C# (pronounced as C sharp) is the brainchild of the Microsoft Foundation, and part of its .Net framework, a common platform to develop various applications in wide areas of technology starting from the desktop, to the internet and distributed computing under a single roof on a single base. Don’t panic if you are hearing these words for the first time as we’ll be explaining everything in due course.
C# is largely based on the Object Oriented Programming (OOP) concept and hence a general understanding of the concept will enable you to write C# programs effortlessly and maintain accuracy at the same time. Therefore a general discussion on Object Oriented Programming Concept is inevitable before we even start programming C#.
All computer programs are made up of a set of instructions and the set of data on which the instructions are going to work. Hence by this we can easily then make our programs either, more instruction oriented or data oriented. In the early days, i.e., prior to OOP, all programs were woven around the code and the code was given utmost importance. But when large distributed systems were built there raised a need for maintaining the data on which the code was acting on a distributed environment or in large central storage facilities. This made the data more vulnerable to outside influences, and for data critical applications it became a basic necessity that the data is protected. Hence the programming paradigm shifted from code centric to datacentric and hence OOP was born. OOP organizes the code to a key principle that “access to the code is controlled by the data”, i.e., here one has to define the data and the code that has access to the data together.
The discussion of OOP concepts cannot just end without mentioning the three most important features of OO related to C#, they are:
Encapsulation: This feature enables the programmer to bind the data and the code together into a single piece and thus protecting both from outside influence. Hence a black box of code and data is created which is self contained. An instance of such a black box is called an object. The basic unit of encapsulation in C# is the class. With a class construct you can declare both the data and the code that manipulates the data in a single block. The code and data that form the class are called the members of the class. A C# method is called a function.
Polymorphism: This feature is incorporated into C# to enable the code re-usability (another important feature of OOP) in the level of methods. Methods can be declared in the general sense and they have access to all the classes they are implemented in. (Don’t worry about if you don’t understand it here, we would elaborate on this when we learn about interfaces in subsequent chapters).
Inheritance: This feature enables a class property to have access to other class properties in the same program code. This is another way of saying a hierarchical classification of data, where the child class can inherit and have access to the properties of its parent class. These come especially handy when more than one class needs to manipulate upon the properties of a single base class. We would learn more about this in subsequent chapters.
With the above OOP concepts at the back of our mind (as these are highly useful when we start dealing with classes and objects in subsequent chapters), let us step into the Visual C# 2005 Express IDE for a walk through of what’s where and write our first sample program.