Date post: | 08-Feb-2017 |
Category: |
Technology |
Upload: | eng-teong-cheah |
View: | 205 times |
Download: | 2 times |
Namespace and Classes
Eng Teong Cheah
Microsoft MVP Windows Development
Xamarin: the complete mobile lifecycle solution
Namespace
Namespace
Namespace is a logical grouping of classes. These are used to organize the program and also provide assistance in avoiding name clashes.Example: System.Console.WriteLine();Here system is a namespace. Namespace are used to group many classes together.
namespace System
{
Static class Console
{
}
}
Classes
Classes
A class consists of data and behavior. Class variables shows it data and methods and events shows its behavior.Class can contain combination of any of the following fields:- Constructor
- Fields- Methods- Events- Delegates- Properties- Destuctors
Example: See a simplified example of a classclass operation
{
//data members
int a;
int b;
//constructor
operation(int i, int j)
{
this.a = i;
this.b = j;
}
//function members
public int sum()
{
return this.a + this.b;
}
//destructor
~operation()
{
GC.Collect();
}
}
Class modifiers
Class modifiers modify the visibility of a class. Modifiers denote access or behavior Class which is not nested have two access modifiers
InternalAn internal class is accessible only within files in the same assembly.
Public
public class has no restrictions on its accessibility.Nested class can have two more accessibility private and protected. Nested class behaves like any other member (field or methods). See example below for protected class:
. internal class printer
{
protected void print()
{
Console.WriteLine("everything is printed");
}
}
Build this class as project type class library. Add new project and add code below add reference to above build dll.
class program
{
public static void Main()
{
printclass.printer pr = new printclass.printer();
}
}
You will get following error message Error 1 ‘printclass.printer ’ is inaccessible due to its protection level. If we make printer class as public then we won’t get this error message.
Abstract class
A class that cannot be instantiated and cannot be sealed but can inherited, contains some methods which are only declared but not implemented is called Abstract Class. Abstract class is generally used for keeping complex code in one base class and derived class to be fairly simple. Example:
public abstract class printer
{
public abstract void print();
public void printall(string text)
{
Console.WriteLine(text);
}
}
Above class have two function on contains only body and other is fully implemented.
Sealed class
Sealed class cannot be inherited. Sealed class is used to prevent accidental inheritance. Sting is a sealed class by Microsoft. Example:
sealed class printer
{
public void printall(string text)
{
Console.WriteLine(text);
}
}
Any class deriving from above type will lead to an error. New keyword is use only with nested classes, new indicates that the class hides an inherited member of the same name.
Demo
Get Started TodayXamarin.com
•Website:
• http://techoschool.com/
•Get Started:
• http://xamarin.com
Reference