C# – Classes

Standard

 

  Class is a object, it uses heap memory (address of the memory location), encapsulate the code and data, able to inherited with other derived class, and able to instantiate.

base class

 The class inherited by one or more other classes is called base class. Base class members are used by derived class.

 

public class baseClass

{

 public string baseClassMember1 { get; set; }

 string baseClassMember2 { get; set; }

 protected string protectedVisibleInDerived = “This protected member visible in the derived class \r\n”;

 

public virtual string baseClassMethod()

{

 return “Base class virtual methods can be override in the derived class. \r\n”;

}

}

derived class

 Class inherit other classes called derived class

class derivedClass : baseClass

{

 public string derivedClassMember { get; set; }

 public override string baseClassMethod()

{

 return string.Format(“This method override the base class method using ‘override’ keyword. \r\n {0}”, protectedVisibleInDerived.ToString());

}

}

class Program

{

 static void Main(string[] args)

{

derivedClass dc = new derivedClass();

dc. baseClassMember1 = “Base class member inherited form derived class.\r\n”;

dc.derivedClassMember = “Derived class member.”;

 Console.WriteLine(dc.baseClassMember1);

 Console.WriteLine(dc.derivedClassMember);

}

}

partial class

 part of the class coded in one file and other part of this class coded either in same or different file with same name and with partial keyword with in the same namespace, that means same class separated in more than one file with partial keyword. But compiler will accumulate the code to treated as one class when compile. Example web form design and corresponding code separation file are declared as partial class.

public class

Class with public modifier accessible inside and outside scope of the assembly.

//Class library project

using System;

namespace publicClassLibrary

{

public class accessibleOutsideAssembly

{

public string accessibleProperty { get; set; }

string inaccessibleProperty { get; set; }

}

internal class derivedClass : accessibleOutsideAssembly

{

public string derivedClassProperty { get; set; }

}

}

//Console application

using System;

using publicClassLibrary;

namespace applicationAccessExternalAssembly

{

class Program

{

static void Main(string[] args)

{

accessibleOutsideAssembly aOA = new accessibleOutsideAssembly();

aOA.accessibleProperty = “This property from external assembly”;

Console.WriteLine(aOA.accessibleProperty);

Console.ReadLine();

}

}

}

private class

Least accessible modifier. Can not inherit other than private class. Can not use ‘private‘ modifier explicitly with in the namespace. Compiler error will be occurred as ‘Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal’

internal class

Classes with ‘internal’ access modifier accessible only with in the assembly. Can not access outside the assembly file. Internal class inherited with private and with internal classes. If inherited with public class compiler shows the error as “Inconsistent accessibility: base class ‘ClassFile.accessibleTest’ is less accessible than class ‘ClassFile.derivedClass

using System;

namespace ClassFile

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“This program gives compiler error.”);

Console.ReadLine();

}

}

internal class accessibleTest

{

public string testPrimaryPropertyFromMainClass { get; set; }

}

//compiler will prompt error with following message when compile following class definition “Inconsistent accessibility: base class ‘ClassFile.accessibleTest’ is less accessible than class ‘ClassFile.derivedClass”

public class derivedClass : accessibleTest

{

public static string derivedClassMethod()

{

return“Compiler will prompt error for Inconsistent accessibility.”;

}

}

}

Static class

It is useful when required to create the non – instance class. Static class can not instantiate, loads directly when compile. ‘Static’ keyword instruct to the compiler the class or method not allow to create the accidental instance.

  • Non static members can not be declared inside the static class. That means all the members should be static.

//public string publicStaticClassNonStaticMember = @”Non static member gives compiler error within the static Class “;

  • Non – static class can not use as base class with static class. That means non static class can not be inherited from static class vice versa. It should be derived from object.

// public class generalClass : staticClass

// {

// public static string publicDerivedClass = @”This class declaration //gives compiler error Static class ‘staticClass.generalClass’ cannot derive

//from type ‘staticClass.staticClass’. Static classes must derive from object.”;

// }

  • Static class can not instantiate.

//generalClass gc = new generalClass();//create instance with static class denied

using System;

namespace staticClass

{

class Program

{

static void Main(string[] args)

{

// generalClass gc = new generalClass(); // create instance with static class denied

staticClass.publicStaticClassStaticMember = “Value assigned.”;

Console.WriteLine(staticClass.publicStaticClassStaticMember);

Console.ReadLine();

}

}

static class staticClass

{

static string privateStaticClassStaticMember = “This static class member not accessible out of this scope.”;

public static string publicStaticClassStaticMember = string.Empty;

//public string publicStaticClassNonStaticMember = “Non static member gives compiler error within the static Class “;

}

// public static class generalClass : staticClass

// {

// public static string publicDerivedClass = @”This class definition gives

//compiler error Static class ‘staticClass.generalClass’ cannot derive from type

//’staticClass.staticClass’. Static classes must derive from object.“;

// }

}

Sealed class

Class with ‘sealed‘ keyword can not inherit. If try to inherit, compiler shows “‘classFile.testClass’: cannot derive from sealed type ‘classFile.sealedClass’

public sealed class sealedClass

{

public string sealedClassMember1 { get; set; }

}

public class testClass : sealedClass

{

public string testClassMember = “This code gives compilation error.”;

}

Abstract class

Class with ‘abstract‘ keyword can not instantiate. This incomplete form of the abstract class implemented with derived class.

// abstractClass ac = new abstractClass(); // create instance with abstract class denied

public abstract class abstractClass

{

public string abstractClassMember1 { get; set; }

}

public class derivedClass : abstractClass

{

public string derivedClassMember = “Abstract class members are accessible through derived class.”;

}

Class Extension Method

Some scenario one class need to use another class member as its member instead of inherit the whole class. Extension methods are created using ‘this‘ keyword followed by instantiate the class.

using System;

namespace ExtensionClass

{

class Program

{

static void Main(string[] args)

{

testClass itc = new testClass();

Console.WriteLine(@“Extended class member also accessed through main class instance. \r\n”);

itc.testPrimaryPropertyFromMainClass = @“This content from base class. \r\n”;

Console.WriteLine(itc.testExtensionFromExtensionClass()); // Accessible thru main class instance see figure Extension_Method 1.1

Console.ReadLine();

}

}

class testClass

{

public string testPrimaryPropertyFromMainClass { get; set; }

}

static class testClassExtendedUsingThisRefernece

{

public static string testExtensionFromExtensionClass(this testClass tc)

{

//this member accessible thru instance of testClass see figure Extension_Method 1.2

return tc.testPrimaryPropertyFromMainClass + @“This content from extended method.”;

}

public static string ThisMethodNotExtended()

{

return“This method not extended.”;

}

}

}

Figure: Extension_Method 1.1

Figure: Extension_Method 1.2

using System;

namespace staticClass

{

class Program

{

static void Main(string[] args)

{

derivedClass dc = new derivedClass();

dc.baseClassMember1 = “Base class member inherited form derived class.\\r\\n”;

dc.derivedClassMember = “Derived class member.”;

Console.WriteLine(dc.baseClassMember1);

Console.WriteLine(dc.derivedClassMember);

Console.WriteLine(dc.baseClassMethod1());

Console.WriteLine(dc.baseClassMethod2());

baseClass bc = new derivedClass();

Console.WriteLine(bc.baseClassMethod1());

Console.WriteLine(bc.baseClassMethod2());

Console.ReadLine();

 /* Output : * * Base class member inherited form derived class. * Derived class member. * Derived class method1 * This derived class method2 override the base class method using \’override\’ keyword. * This protected member visible in the derived class * Base class method1 * This derived class method2 override the base class method using \’override\’ keyword. * This protected member visible in the derived class * */

}

}

 public class baseClass

{

public string baseClassMember1

{ get; set; }

string baseClassMember2

{ get; set; }

protected string protectedVisibleInDerived = “This protected member visible in the derived class \\r\\n“;

public string baseClassMethod1()

{ return “Base class method1 \\r\\n“; }

 public virtual string baseClassMethod2()

{ return “Base class method2. \\r\\n“; }

}

class derivedClass : baseClass

{

public string derivedClassMember { get; set; }

public string baseClassMethod1() { return “Derived class method1 \\r\\n“; }

public override string baseClassMethod2()

{ return string.Format(“This derived class method2 override the base class method using \’override\’ keyword. \\r\\n{0}”,protectedVisibleInDerived.ToString()); }

}

 }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s