Search This Blog

Thursday, 9 March 2023

C# Basic Boiler plating code for absolute beginners

 The following syntaxes are helpful in boiler-plate coding of any application. Here, we shall focus on the C# language. Please note, we are speaking of the common syntaxes that are used in every application. This is not a cheatsheet of all possible C# syntaxes.

We have seen in the post: Coding Best Practices - Part 1 article, about writing boiler-plate code.

Boiler-plate code is just creating the structure of an application, without a logic.

In .Net, everything is an object, hence the basic operating paradigm happens to be OOPs.

C# is a modern language created for .Net and also operates on OOPs.

In any application developed using the OOPs paradigm, following entities become very important

  • Namespaces: Logical grouping of code entities such as class, interface, enum, delegate, events
  • Class: A blueprint of a real-world object
    • Properties: Special variables with getters, setters. The main purpose is to share information with other classes, assemblies (executable version of a project)
    • Methods: Functions that contain logic.
  • Interface: A code contract, that is agreed upon by classes as required
  • Relationships:
    • Inheritance: Parent-Child Relations. In C#, we use ":" to indicate an inheritance / contract implementation
      • Eg: class Circle : Shape{...}
        • Here Shape is a normal class
      • Eg: class Circle: Shape, IPaintContract{...}
        • Here, IPaintContract is an interface
    • Association: Here one class contains a property, parameter of another class
      • class Circle

        {

           //Property of type Compass. Here Compass is a class

           public Compass DrawingTool {get; set;}

        }

         

        Class Compass{ ... }

Lets take an example of an application such as Employee Management System

  • It would contain
    • Classes like Person, Employee, Project, Department etc.
    • Namespaces for grouping a category of classes
    • Relationships
    • Contracts like IEmployeeContract

Here are the syntaxes for

  • Namespace: Syntax: namespace <namespace_name>{ ... }

    • namespace EMP

      {

      }

  • Class: Syntax: <access-specifier> class <class_name>{ ... }

    • Here, <access-specifier> can be one out of
      • public
        • Shareable and accessible in a given assembly and across different assemblies
      • private
        • Not shareable / accessible outside the class scope
      • protected
        • Shareable / accessible only to classes related through inheritance
      • internal
        • Shareable and accessible only within the assembly where it is created.

    • public class Person {

                         

       }

       

      public class Employee {

                         

       }

       

  • Interface

    • interface IEmpContract

      {

      }

       

  • Relationship. In this case, the relationship of inheritance

    • Class Employee : Person, IEmpContract{

      ...

      }