Using C# (C Sharp)as a tool for object oriented programming
The concept of object-oriented programming has become well-established due to its inherent advantages that it brings with it. Although many languages like C++, Java, etc. are available for implementing the concepts of object-oriented programming, C# has proved to be a powerful tool for its following key features:
type-safe language that is easy to learn and powerful to cater to the
growing needs of almost all type of developer community
Facilities that C# offers to support Object Oriented programming
Some of the concepts/areas that C# has provided easy way to achieve object oriented programming are as below:
1. Overloading consists of two types, Method overloading and Operator overloading
a. Method overloading implies the different methods defined in a class with the same name but with different number or type of parameters. Based on the parameters passed, the compiler will choose the right method automatically. This is something similar to the one in C++ except that default values for parameters are not allowed in C#. This type of overloading is used whenever there is a need to execute an operation with multiple types of input values. Example is Console.WriteLine().
b. Operator overloading is used when there is a requirement for logical operations to be executed on instances of a class. With this, arithmetic operators like +, -, /, etc. and also comparison operators like ==, !=, etc. can be overloaded to achieve better readability of the code.
2. Memory management Unlike in the traditional C++ language where every call to new method has to have a corresponding delete call for releasing the memory allocated in the heap, C# eases the programmer by offloading the memory clean up task to the garbage collector of the runtime. Also, internally, this helps improving the performance by using just only a single heap pointer for memory allocation in the managed heap since the garbage collector maintains the free memory as a one contiguous block.
3. Initialization and destruction of objects - In case of object creation with some pre-defined initialization parameters mandatory for its existence, constructor can be defined in the class with the necessary logic. For example, employee identification needs to be created as a part of an employee object can be coded in the constructor of the class. Static constructors can be used when there are static instances of members to be initialized. Usually objects having such constructors cannot be instantiated at all since these objects are ONLY called by the .Net runtime when the class is loaded. For defining a hard coded value in a program, C# provides option for defining a constant variable is provided by the usage of const keyword.
5. Inheritance - C# supports inheritance at interface and implementation level. Interface inheritance is designed similar to COM but not exactly. While GUIDs were used as a mandatory requirement for an interface, it is not so in C#. By way of defining interfaces, C# provides a good tool for contract-based programming. Implementation inheritance is similar to that in C++ except that C# does not allow multiple inheritances of base classes.
6. Support from the class library of the .net framework - C# language derives most of its features from the way it interacts with the .Net framework and its associated library. The class hierarchy of the framework provides a strong base for C# in the form of some base classes (like System.Object) to offer support for the features as below:
String handling: System.String is a powerful and versatile class designed to help perform large number of operations on the string which can save a considerable amount of time for developer in coding. Some of the complex operations include replacement of one string with the other, padding string with additional characters, retrieval of substring form the given string, etc.
Handling list of objects: System.Collection is a class which provides support for storing and retrieving a list of objects (of any data type) in two different forms, called ArrayList or Collection. The main difference between ArrayList and Collection is that while the former stores and retrieves the list items in sequential order based on the index, the latter stores in an order determined by the Collection itself. Hence, the usage of ArrayList and Collection is made based on the requirements of the context.
Reflection: is a facility offered by the framework to access the information regarding the assembly like the metadata, dependencies, etc. and also load the assembly. System.Reflection namespace includes the classes to provide programmatic access to the details of the assembly.
Threading: System.Threading namespace includes classes that are designed to support multi-threaded applications. Thread class has methods which can be called to support the multithreading functionality of the application.
C# is mainly targeted to suit the development of enterprise applications which can avail the maximum benefits of the Common Language Runtime of .net environment in which the application gets executed. By following the object-oriented design in developing C# applications, the benefits from both the environment and the language can be derived.