-1

I have never seen this before - pointers in C#:

public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string> {
    public String(char* value); // <-- string has a constructor that takes a char pointer?
...
}

I'm curious to get a little understanding.

Is a char pointer something like a pointer in C/C++, i.e. a int value of the same size as a memory location?

I also wonder if my lack of understanding pointers is why I have never fully grasped what the ref keyword does, in other words are they related?

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • You should learn what pointers are in `c`. it's exactly the same thing. and for the `ref` part: just read **MSDN**, or try to use it once. – gdoron Apr 15 '12 at 00:48
  • they are memory locations - int values with enough bytes to represent any location in the process address space, no? – Aaron Anodide Apr 15 '12 at 00:49
  • @gdoron, yeah, i find MSDN to be a gamble - sometimes it's clear as day and I'm glad I went there and other times i wish I had just put it on stackoverflow and let someone who can answer it in their sleep put it in nice concise words for me... that's why this is such a popular place right? – Aaron Anodide Apr 15 '12 at 00:51
  • if someone could elaborate on why this question is not constructive i'll endeavor to improve in the future - if the answer is RTFM then i disagree but i understand where you're coming from at the same time – Aaron Anodide Apr 15 '12 at 01:20

3 Answers3

3

Pointer types are part of C# language. They work just like pointers in C do and allows you working with unmanaged memory, with blittable types and arrays of blittable types. They are not related to ref/out parameters (these use managed pointers, which is different thing). Since using pointers can break type safety if used incorrectly, you can use them only in unsafe context.

Ňuf
  • 6,027
  • 2
  • 23
  • 26
1

Pointers are used for performance optimization in my understanding, where you break into the field of "unsafe", and manipulate memory address directly.

In my experience, image processing is one of the areas that you get significant benefits from pointers, like

http://imrannazar.com/Using-Pointers-in-C

Unsafe Pointer iteration and Bitmap - why is UInt64 faster?

There must be other typical scenarios, but a normal C# application does not require pointers generally speaking.

Community
  • 1
  • 1
Lex Li
  • 60,503
  • 9
  • 116
  • 147
1

C# was designed to simplify memory management to provide programmers which is a quicker and easier way to program. That comes at the cost of performance (and lazy programming) as a garbage disposal has to come by and kill all the broken pointers. For instance, in a game update loop this creates a lot of stuff for the garbage disposal to collect:

       Vector3 myVector;
       void update(int x, int y, int z)
       {
             myVector = new Vector3(x, y, x); //This will kill your Xbox 360 with enough objects
       }

In c# everything is held as a reference. This is a bit different from C/C++ where the programmer has different ways referencing data in RAM.

As a way to provide more flexibly to programmers, especially in areas which require high performance, a programmer is allowed to use pointers and references. This is called "unsafe" variables because it is no longer managed by the garbage disposal. In other words, if there is a memory leak, the garbage disposal will not come by and delete it.

Another reason is for compatibility between C#/C++. If you get into interpolating code between the two languages, you will quickly realize that C# needs to know how to provide compatibly for pointers being passed and obtained by the C++ side. So say if I want to pass C++ code a pointer, I pass

    someInterpatedFunc(ref myVariable);

Hope that answers some questions :)

Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94