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# Pointers 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
Pointers were present in C++ but the Java designers took them out. C# has brought them back. However, C#, pointers can only be declared to hold the memory addresses of value types. That is, we cannot have pointers for reference types. The rest is very similar to C++. This is illustrated with the help of the following example:

int i = 5;
int *p;
p = &i; // take address of i
*p = 10; // changes the value of i to 10


One major difference between C++ and C# is that the ‘*’ applies to the type. That is, as opposed to C++, in C#, the following statement would declare two pointers p1 and p2:

int * p1, p2;


Just like C++, the dereference operator ‘->’ is used to access elements of a struct type.

Keyword 'unsafe'
The keyword unsafe is used while dealing with pointers. The code dealing with pointers has to be written within the unsafe block{ }. The name reflects the risks that you might face while using it.

static void Main() {
//can't use pointers here
unsafe
{
//you can declare and use pointer here
}
//can't use pointers here
}


We can also declare a whole class as unsafe as shown below:

unsafe class Class1 {
//you can use pointers here!
}


If you wish you can declare a method as unsfe as well as shown in the following piece of code:

unsafe void MyMethod() {
//you can use pointers here
}

Next >>> Lesson No. 11: C# Pointers 2





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