x
[quotes_form]
Common Errors in Background & Significance Section of a Grant Application & How to Correct Them

Object-Oriented Programming with C#

Around 20 years ago, C# was designed and developed by Microsoft as a language to be used on its platform. It is a multi-paradigm high-level language that is primarily used for web application development. C# covers a wide variety of features that make it advanced yet remarkably simple to use.

C# was developed by Anders Hejlsberg, as a part of .Net framework. Later it was approved by ECMA and standardized by ECMA and ISO (Zhang, An, and Chen, 2012). The idea was to create a modern yet simple, general-purpose programming language that would support object-oriented programming. Unlike C++, C# is an entire object-oriented programming language that assists the software developer in building software and applications on the Microsoft platform.

 

Basic Features of C#

C#, the C family’s programming language, possesses somewhat similar syntaxes to C and C++. It was first named COOL, which was an abbreviation of “C for an object-oriented language.” The building fundamentals for C# were to create a structured high-level programming language that should be simple, object and component-oriented, scalable, and type-safe. It has a very rich library and is quite fast. (Rahman and Latif, 2010)

Let’s discuss some of the features of C# in detail.  

  • Compiling

C# is a part of the Microsoft .Net framework; compiling in C# is performed by Common Language Runtime (CLR). It makes the execution of programs easier by providing a managed environment for execution.

  • Memory Management

Memory management is an automated feature in C#. This saves a lot of time developers could have wasted in manual memory allocation. C# uses a garbage collection method for memory management. The garbage collector becomes active under certain conditions and usually goes through three phases, freeing space in memory occupied by the objects that are no longer in use.

  • Simple

C# is a simple, general-purpose, and modern language that provides flexibility to the software developers with a wide range of features. These features are adequate and robust enough to develop any kind of application with advanced usability and functionalities. Its structure is simple and easy to understand.

C# is an open-source programming language, that offers cross-platform integration and has continuously been evolving since its inception.

  • Applications

C# is used to develop numerous applications that are executable on the .Net framework and used widely in several ways. With C#, you can create applications for the following:

  • Web Applications
  • Mobile Applications
  • Back-end services
  • Client-server applications
  • Windows client applications
  • Azure Cloud Applications
  • Database Applications
  • Distributed components

 

Encapsulation

One of the fundamental concepts of object-oriented programming is encapsulation. It provides incorporation of data, its types, and functions applicable to it, to act as one unit. Encapsulation also provides information hiding, and unauthorized accessibility restrictions by defining their scope and permission to accessibility.

Encapsulation provides data safety as well as benefits the programmers in several ways:

  • It protects the data in case of unexpected errors.
  • It offers accessibility options outside the class code of each class member.
  • Secures data and class members from unauthorized use and access.
  • It provides flexibility and code extension to reduce complexity.
  • It provides an improved and maintainable code by decreasing coupling among the class members.
 
Access Specifiers

The implementation of encapsulation is achieved with the help of access specifiers. Access specifiers define the visibility and reach of a class and its members known as its scope. C# uses access specifiers to prevent or allow access to the class’s implementation code, its members, and functions.

Public

When a class is declared public, its functions and members are accessible and visible publically. The Public access specifier permits a class to allow its members, such as variables and functions, to be accessible to functions and objects outside the class. 

Private

A private access specifier applies to a class member. It provides minimum access to the class member, and that is only to its member function. Member variables with a private access specifier are hidden from other members and functions. 

Protected

A protected access specifier is also a member-level access modifier. The scope of a protected member is only within its class and objects of its child class. 

Internal

An internal access specifier exposes member variables and member functions of a class to other objects and functions in the current compiled code or assembly. It means that a member or a function of a class that has been assigned internal access will be usable and accessible by any other class or function defined in the same application or program. In simple words, its scope is within a program.

Protected internal

A protected or internal is a combination of both a protected and internal access specifier and will implement the rules of both protected as well as an internal specifier. It is used to define the scope of a member. Using this, a member’s scope will be limited to the current program or members of the class derived from the base class the member resides in.

Abstraction

Encapsulation and abstraction are interconnected concepts of object-oriented programming. While encapsulation combines objects and functions as one single unit and provides security and maintainability, abstraction permits visibility and accessibility to the relatable information only, such as the functionality rather than displaying the code. In short, with the help of encapsulation, the abstraction level is implemented by the programmer.

Abstraction in C# is achieved through abstract classes. An abstract class is a special class that is meant to be inherited. The subclass that inherits an abstract class will implement an abstract class partially or not at all. By including the ability to have constructors in abstract classes, we can design components or functionalities that are supposed to be implemented by the derived classes. (Xinogalos, Satratzemi, and Dagdilelis, 2006)

The following are the properties of an abstract class:

  • Abstract classes are used during inheritance.
  • The override keyword must be used by the declared abstract method in the derived or child class. In this way, the abstract class is inherited in the child class. 
  • Structures cannot inherit abstract classes.
  • Abstract classes can have constructors or destructors.
  • They are capable of implementing functions using non-abstract methods.
  • Multiple inheritances are not supported by abstract classes.
  • An abstract class can’t be static.

 

Inheritance

This is also a very important aspect of object-oriented programming that adds ease of reusability for programmers. Inheritance is a feature that allows a child class to be defined, which can inherit behaviors and attributes of an already existing class. The parent class or base class is the one that allows its members to be inherited, whereas the child class is also known as the derived class is the one that inherits.

C# supports single but transitive inheritance. That means only one class can be inherited from the parent class. However, the child class can also become a parent class for another one to be inherited.

 

Polymorphism

Polymorphism in programming is referred to as having more than one method of performing a single or same method. In C#, polymorphism is achieved by using multiple methods in classes for the same task. This is known as method overloading. 

Polymorphism is of two types: static and dynamic, and either can be used at a time.

Static Polymorphism

Static polymorphism is also called early binding or static binding. In this method, a function is linked with the object during the time of compilation. Two techniques are used for the implementation of static polymorphism.

Function overloading: It means having the same function name defined in multiple ways and that too within the same scope. The function definitions should be different from each other by type and argument numbers in the list of arguments. But function declarations having only a return type difference cannot be overloaded.

Operator overloading: C# enables you to overload its built-in operators by using it with types defined by the users to perform various operations. The overloaded operators become functions with proper names by using the keyword operator before the C# operator. C# allows overloading of Unary, Binary, and Comparison operators. Conditional and Logical operators can be overloaded indirectly, whereas assignment operators cannot be overloaded.

 

Dynamic Polymorphism

Dynamic polymorphism is also known as late binding. It is achieved through method overriding in which the response to the function is decided during run time. Method overriding is defined as having methods with similar names but diverse implementations. The reference variable of the parent class is used to call the overridden method. The method decision is made by taking into account the object that is referred by the reference variable.

 

Class

A class is a data structure. It combines the data types contained in it, and the methods that will be implied on them, how the objects will be, their attributes and characteristics. A class name defines the type of objects it will hold. A class consists of objects, class members, and member functions.

Objects are also called instances of the class that can be used. Classes and objects are linked with each other.

Class members define the properties, behavior, and methods of class data. C# uses several class members that are

  • Methods: a block of code included separately to define operational statements a class can perform.
  • Property: used to read/ write or modify the value of class variables. It uses Get and Set accessors which are block codes used to get and set values.
  • Fields: variables used in C# for storage and allocation.
  • Events: notifications or messages to describe a possible reaction to a user action.
  • Operators: used to define certain operations that can be performed on operands. Such as arithmetic, logic, relational, etc.
  • Constructors: it is a class method and is automatically enforced as soon as the class object of a certain type is created. It is a special function and holds the same name as that of the class.
  • Constants: a keyword used to declare a field as constant in a class.
  • Destructors are used to destroy class instances when collected by the garbage collector and are of no use.
  • Indexers: used to index the objects or instances of a class.
  • Types: it is used to indicate the data type a variable holds. There are two data types, value type, and reference type. A class is a data structure. It combines the data types contained in it, and the methods that will be implied on them, how the objects will be, their attributes and characteristics. A class name defines the type of objects it will hold. A class consists of objects, class members, and member functions. Objects are also called instances of the class that can be used. Classes and objects are linked with each other. Class members define the properties, behavior, and methods of class data. C# uses several class members that are
    • Methods: a block of code included separately to define operational statements a class can perform.
    • Property: used to read/ write or modify the value of class variables. It uses Get and Set accessors which are block codes used to get and set values.
    • Fields: variables used in C# for storage and allocation.
    • Events: notifications or messages to describe a possible reaction to a user action.
    • Operators: used to define certain operations that can be performed on operands. Such as arithmetic, logic, relational, etc.
    • Constructors: it is a class method and is automatically enforced as soon as the class object of a certain type is created. It is a special function and holds the same name as that of the class.
    • Constants: a keyword used to declare a field as constant in a class.
    • Destructors are used to destroy class instances when collected by the garbage collector and is of no use.
    • Indexers: used to index the objects or instances of a class.
    • Types: it is used to indicate the data type a variable holds. There are two data types, value type, and reference type. A class is a data structure. It combines the data types contained in it, and the methods that will be implied on them, how the objects will be, their attributes and characteristics. A class name defines the type of objects it will hold. A class consists of objects, class members, and member functions. Objects are also called instances of the class that can be used. Classes and objects are linked with each other. Class members define the properties, behavior, and methods of class data. C# uses several class members that are
      • Methods: a block of code included separately to define operational statements a class can perform.
      • Property: used to read/ write or modify the value of class variables. It uses Get and Set accessors which are block codes used to get and set values.
      • Fields: variables used in C# for storage and allocation.
      • Events: notifications or messages to describe a possible reaction to a user action.
      • Operators: used to define certain operations that can be performed on operands. Such as arithmetic, logic, relational, etc.
      • Constructors: it is a class method and is automatically enforced as soon as the class object of a certain type is created. It is a special function and holds the same name as that of the class.
      • Constants: a keyword used to declare a field as constant in a class.
      • Destructors are used to destroy class instances when collected by the garbage collector and are of no use.
      • Indexers: used to index the objects or instances of a class.
      • Types: it is used to indicate the data type a variable holds. There are two data types, value type, and reference type. 

 

References
  1. An, J.P., Chen, P., & Zhang, Y. (2012). Research of Hybrid Programming with C#.net and Matlab. Journal of Physics Procedia, 24(C), 1677-1681. https://doi.org/10.1016/j.phpro.2012.02.247
  2. Latif, A.M.A., & Rahman, S.E.A. (2010, June). DisBlue+: A Distributed Annotation-based C# Compiler. Journal of Egyptian Informatics Journal, 11(1), 1-10. https://doi.org/10.1016/j.eij.2010.06.001
  3. Black, A.P. (2013, October). Object-Oriented Programming: Some History, and Challenges for the Next Fifty Years. Journal of Information and Computation, 231, 3-30. https://doi.org/10.1016/j.ic.2013.08.002
  4. Dagdilelis, V., Satratzemi, M., & Xinogalos, S. (2006, September). An Introduction to Object-Oriented Programming with a Didactic Microworld: ObjectKarel. Journal of Computers & Education, 47(2), 148-171. https://doi.org/10.1016/j.compedu.2004.09.005