www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons C# Interrfaces, Boxing, new Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
In this lesson we will discuss some miscellaneous C# concepts like interfaces, boxing and the new keyword.

The new operator
In C++, the new keyword instantiates an object on the heap. In C#, with reference types, the new keyword does instantiate objects on the heap but with value types such as structs, the object is created on the stack and a constructor is called. You can, create a struct on the stack without using new, but be careful! New initializes the object. If you don't use new, you must initialize all the values in the struct by hand before you use it (before you pass it to a method, for example) or it won't compile.

Boxing is converting any value type to corresponding object type and convert the resultant 'boxed' type back again, also called un-boxing.

int i = 123;
object box = i; // value of i is copied to the object box
if (box is int){ // runtime type of box is returned as boxed value type
Console.Write("Box contains an int");
// this line is printed

Just like Java, C# also has interfaces that only contain method signatures. There are no access modifier and everything is implicitly public. It doest not have any fields, not even static ones. A class can implement many interfaces but unlike Java there is no implements keyword. Syntax notation is positional where we have base class first, then base interfaces as shown below:

class X: CA, IA, IB {

Next >>> Lesson No. 21: C# readonly, is, as

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.