Guy
Guy

Reputation: 67380

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?

Upvotes: 4

Views: 3837

Answers (11)

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

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#.

Upvotes: 1

Chris Ammerman
Chris Ammerman

Reputation: 15316

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.

Upvotes: 1

MJVC
MJVC

Reputation:

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

Upvotes: 0

Bob King
Bob King

Reputation: 25866

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.

Upvotes: 5

Mark Cidade
Mark Cidade

Reputation: 100047

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.

Upvotes: 5

David Thibault
David Thibault

Reputation: 8736

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
}

Upvotes: 3

Fire Lancer
Fire Lancer

Reputation: 30165

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.

Upvotes: 1

Bryan Donaldson
Bryan Donaldson

Reputation:

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

Upvotes: 1

CiNN
CiNN

Reputation: 9880

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

Upvotes: 1

user7116
user7116

Reputation: 64148

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.

Upvotes: 12

Nate Kohari
Nate Kohari

Reputation: 2224

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.

Upvotes: 4

Related Questions