Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What are the C# class design principles?

user-image
Question added by Salem Al Badawi , Web Developer , Electronic Company for High Technology
Date Posted: 2013/09/02
Salem Al Badawi
by Salem Al Badawi , Web Developer , Electronic Company for High Technology

An important list of principles is represented by the acronym SOLID. 

 

S -:

Single responsibility principle A class should have only one responsibility. For example,

a class shouldn’t be both responsible for saving itself to

the database and for displaying to the user.

O    -:

Open/closed principle An object should be open for extension but closed for

modification. For example, by using a common interface,

new objects can integrate with existing code without

modifying the existing code.

L -:

Liskov substitution principle A base type should be replaceable with subtypes in each

and every situation. For example, a Duck that can swim

and an inherited ElectricDuck that can swim only if the

batteries are full. Suddenly, code needs to check whether

the Duck is an ElectricDuck to replace empty batteries.

I -:

Interface segregation principle Use client-specific interfaces instead of one general interface.

A user of an interface should not have to implement

all kinds of methods that he doesn’t use.

D -:

Dependency Inversion principle Depend upon abstractions, not concretions. For example,

when you use SomeServiceType inside your class,

you shouldn’t depend on the actual implementation

of SomeServiceType. Instead you should depend on an

interface or abstract class. This way, you are less coupled

to the actual implementation

Mahmoud Manaa
by Mahmoud Manaa , Network Engineer , Zain - Kuwait

1.     Choose a (Meaningful) name for the class.

·        IncomeTax for a class hold the income tax Percentage calculation Logic

·     A Generic Name Like Person(Not Man-not Women)

2. Constructors is a Great Solution For Initializing Problems.

3.     Consider Inheritance Role in class Declaration ( e.g. Abstract cannot be inherited to An other Class but can derive an object from it  )

 

using System;

public class ParentClass

{   

public ParentClass()

    {   

      Console.WriteLine("Parent Constructor.");   

  }    

public void print()    

{        

Console.WriteLine("I'm a Parent Class.");   

  }

}

public class ChildClass : ParentClass

{  

  public ChildClass()   

  {        

Console.WriteLine("Child Constructor.");  

   }    

public static void Main()    

{        

ChildClass child = new ChildClass();

        child.print();

    }

}

 

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.