views:

2347

answers:

11
+3  Q: 

C pointers in C#

Is this function declaration in C#:

void foo(string mystring)

the same as this one in C:

void foo(char *)

i.e. In C#, does the called function receive a pointer behind the scenes?

+3  A: 

Essentially, yes. In C#, string (actually System.String) is a reference type, so when foo() is called, it receives a pointer to the string in the heap.

Nate Kohari
+10  A: 

In this specific instance, it is more like:

void foo(const char *);

.Net strings are immutable and passed by reference. However, in general C# receives a pointer or reference to an object behind the scenes.

sixlettervariables
A: 

no in c# string is unicode. in c# it is not called a pointer, but a reference.

CiNN
The char* may also be unicode, eg UTF-8. char* only defines the size per element, not the encodeing.
Fire Lancer
A: 

If you mean - will the method be allowed to access the contents of the character space, the answer is yes.

A: 

Yes, because a string is of dynamic size, so there must be heap memory behind the scenes

However they are NOT the same.

in c the pointer points to a string that may also be used elsewhere, so changing it will effect those other places.

Fire Lancer
Strings in C# are immutable, which is important to understand when thinking of passing "references" to string variables around.
Will
+2  A: 

For value types (int, double, etc.), the function receives a copy of the value. For other objects, it's a reference pointing to the original object.

Strings are special because they are immutable. Technically it means it will pass the reference, but in practice it will behave pretty much like a value type.

You can force value types to pass a reference by using the ref keyword:

public void Foo(ref int value) { value = 12 }
public void Bar()
{
    int val = 3;
    Foo(ref val);
    // val == 12
}
David Thibault
+3  A: 

There are pointers behind the scenes in C#, though they are more like C++'s smart pointers, so the raw pointers are encapsulated. A char* isn't really the same as System.String since a pointer to a char usually means the start of a character array, and a C# string is an object with a length field and a character array. The pointer points to the outer structure which points into something like a wchar_t array, so there's some indirection with a C# string and wider characters for Unicode support.

Mark Cidade
+3  A: 

No. In C# (and all other .NET languages) the String is a first-class data type. It is not simply an array of characters. You can convert back and forth between them, but they do not behave the same. There are a number of string manipulation methods (like "Substring()" and "StartsWith") that are available to the String class, which don't apply to arrays in general, which an array of characters is simply an instance of.

Bob King
A: 

As far as I know, all classes in C# (not sure about the others) are reference types.

A: 

Anything that is not a "value type", which essentially covers enums, booleans, and built-in numeric types, will be passed "by reference", which is arguably the same as the C/C++ mechanism of passing by reference or pointer. Syntactically and semantically it is essentially identical to C/C++ passing by reference.

Note, however, that in C# strings are immutable, so even though it is passed by reference you can't edit the string without creating a new one.

Also note that you can't pass an argument as "const" in C#, regardless whether it is a value type or a reference type.

Chris Ammerman
A: 

While those are indeed equivalent in a semantic sense (i.e. the code is doing something with a string), C#, like Java, keeps pointers completely out of its everyday use, relegating them to areas such as transitions to native OS functions - even then, there are framework classes which wrap those up nicely, such as SafeFileHandle.

Long story short, don't go out of your way thinking of pointers in C#.

Jesse C. Slicer