2

I am new to C# . I have knowledge in c, like in c we can declare a variable as pointer type through which I will be able to access to that variable address with the help &variable name , so how I can use this concept in C#.

Can anyone help me out to solve this problem ?

divya
  • 196
  • 11
  • http://stackoverflow.com/questions/3069448/how-to-declare-a-void-pointer-in-c-sharp – Matteo Umili Jul 16 '15 at 13:44
  • http://www.c-sharpcorner.com/UploadFile/rajeshvs/PointersInCSharp11112005051624AM/PointersInCSharp.aspx – Issa Jaber Jul 16 '15 at 13:48
  • You _very rarely_ need to use pointers in C#. Read up on reference vs value types - I suspect you won't need to either after you understand how reference types work. – D Stanley Jul 16 '15 at 14:04

2 Answers2

0

in c# pointers are only allowed in unsafe context.

you may took a look here : https://msdn.microsoft.com/en-us/library/y31yhkeb.aspx

You should avoid unsafe context if possible.

Chaka
  • 377
  • 1
  • 9
0

In C#, all instance variables are pointers. In fact, when you pass an object as a method's argument, you are passing a new pointer to the same object. This way, the object is never passed by value (see NOTE), and you are able to modify the same instance from different scopes.

Another important thing to note:

public void SomeMethod(object instance)
{
     instance = null;
}

In here you are not setting the instance to null, but instead, changing your pointer to point to a null value, so your original instance will remain untouched.

public void SomeMethod(object instance)
{
     instance = new object();
}

In here you are not modifying the instance value either. You are just changing your pointer to point to a new completely object, leaving the original instance untouched again.


NOTE: Although objects are never passed by value, the pointers to them actually are. As I said, when passing an object to a method, you are creating a new pointer to it, so you can think of the pointer being passed by value.

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • "In C#, all instance variables are pointers" - only for _reference types_. "Although objects are never passed by value" - value types are. – D Stanley Jul 16 '15 at 14:01
  • `System.Object` are always reference types. Value types are of type `System.ValueType` (struct) – Matias Cicero Jul 16 '15 at 14:03
  • 1
    "`System.Object` are always reference types." - not true - `int` inherits from `Object` but it is a value type. – D Stanley Jul 16 '15 at 14:05
  • Although `System.ValueType` inherits from `System.Object`, it's just for the matter of having all the protected methods from it, such as `ToString()`, and `GetHashCode()`. Internally, the CIL does differentiate between the two, as you can see here: [How to declare a ValueType in CIL?](http://stackoverflow.com/a/15026066/2401769). You can see that classes with `extends [mscorlib]System.Object` are reference types, and classes with `extends [mscorlib]System.ValueType` are in fact value types. – Matias Cicero Jul 16 '15 at 14:12
  • I'm not a CLR expert but from a C# language point of view (which is what the question is referencing) it's wrong to say "all instance variables are pointers" and "all objects are reference types". It would be more accurate to say "all instance variables _of reference types_ are pointers" and "all _classes_ are reference types and all _structs_ are value types". – D Stanley Jul 16 '15 at 15:40