15
Namespace and Classes Eng Teong Cheah Microsoft MVP Windows Development

Xamarin: Namespace and Classes

Embed Size (px)

Citation preview

Page 1: Xamarin: Namespace and Classes

Namespace and Classes

Eng Teong Cheah

Microsoft MVP Windows Development

Page 2: Xamarin: Namespace and Classes

Xamarin: the complete mobile lifecycle solution

Page 3: Xamarin: Namespace and Classes

Namespace

Page 4: Xamarin: Namespace and Classes

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

{

}

}

Page 5: Xamarin: Namespace and Classes

Classes

Page 6: Xamarin: Namespace and 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

Page 7: Xamarin: Namespace and Classes

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();

}

}

Page 8: Xamarin: Namespace and Classes

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.

Page 9: Xamarin: Namespace and Classes

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");

}

}

Page 10: Xamarin: Namespace and Classes

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.

Page 11: Xamarin: Namespace and Classes

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.

Page 12: Xamarin: Namespace and Classes

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.

Page 13: Xamarin: Namespace and Classes

Demo

Page 14: Xamarin: Namespace and Classes

Get Started TodayXamarin.com

Page 15: Xamarin: Namespace and Classes

•Website:

• http://techoschool.com/

•Get Started:

• http://xamarin.com

Reference