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:

• Simple, type-safe language that is easy to learn and powerful to cater to the growing needs of almost all type of developer community
• Provides good support for object-oriented programming and hence better re-usability and easy maintenance of code
• Well-defined set of basic types to accommodate almost all forms of data
• Combined with the environment that the .net framework provides for way to write managed code, it offers better code security, language interoperability and memory management.
• Unlike C++, it is a high-level component-oriented language and offers a high level of ease of use to the developer.
• Better support for handling XML data

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.


4. Usage of Structs -
Although struct has been used from the time of C++, it has been designed in C# as simplified form of class. Structs are used as value types and hence struct variables have same lifetime as defined for simple data types. While this feature helps in faster allocation of memory of such variable in the stack and cleanup when they go out of scope, it causes an overhead while passing them as parameters in functions due to the copying action during transfer of values for which they have to be passed as ref parameters. Also, option to specify the layout of the members of the struct variable in memory is provided.

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.

|How to Define Custom Error Pages for Error Handling In ASP.Net Application| Role of Web gardens and web farms in ASP.NET | Understanding Purpose and Usage of "event" Class Member Modifier in C# (C Sharp) | Understanding the Structure and Content of Web.Config File | Usage of C# (C Sharp) Query Keywords – group, by, into | Usage of C# (C Sharp) Query Keywords – join, on, equals, let |Usage of C# (C Sharp) Query Keywords – orderby, ascending, descending | Usage of C# (C Sharp) Query Keywords – select, from, where, in | Usage of Lambda Operator (=>) in C# (C Sharp) |Using C# (C Sharp) as a tool for object oriented programming | Using Dictionaries in .Net – An Overview | Using WCF for providing web service |

 


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.