<p>Is this function declaration in C#:</p>

<pre><code>void foo(string mystring)
</code></pre>

<p>the same as this one in C:</p>

<pre><code>void foo(char *)
</code></pre>

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

[{'answer_id': 64710, 'author': 'Nate Kohari', 'author_id': 1282, 'author_profile': 'https://Stackoverflow.com/users/1282', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 64713, 'author': 'user7116', 'author_id': 7116, 'author_profile': 'https://Stackoverflow.com/users/7116', 'pm_score': 5, 'selected': True, 'text': '<p>In this specific instance, it is more like:</p>\n\n<pre><code>void foo(const char *);\n</code></pre>\n\n<p>.Net strings are immutable and passed by reference. However, in general C# receives a pointer or reference to an object behind the scenes.</p>\n'}, {'answer_id': 64717, 'author': 'CiNN', 'author_id': 2566, 'author_profile': 'https://Stackoverflow.com/users/2566', 'pm_score': 1, 'selected': False, 'text': '<p>no in c# string is unicode.\nin c# it is not called a pointer, but a reference.</p>\n'}, {'answer_id': 64724, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>If you mean - will the method be allowed to access the contents of the character space, the answer is yes. </p>\n'}, {'answer_id': 64725, 'author': 'Fire Lancer', 'author_id': 6266, 'author_profile': 'https://Stackoverflow.com/users/6266', 'pm_score': 1, 'selected': False, 'text': '<p>Yes, because a string is of dynamic size, so there must be heap memory behind the scenes</p>\n\n<p>However they are NOT the same.</p>\n\n<p>in c the pointer points to a string that may also be used elsewhere, so changing it will effect those other places.</p>\n'}, {'answer_id': 64727, 'author': 'David Thibault', 'author_id': 5903, 'author_profile': 'https://Stackoverflow.com/users/5903', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n\n<p>You can force value types to pass a reference by using the <code>ref</code> keyword:</p>\n\n<pre><code>public void Foo(ref int value) { value = 12 }\npublic void Bar()\n{\n    int val = 3;\n    Foo(ref val);\n    // val == 12\n}\n</code></pre>\n"}, {'answer_id': 64733, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 3, 'selected': False, 'text': "<p>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 <em>and</em> a character array. The pointer points to the outer structure which points into something like a <strong>wchar_t</strong> array, so there's some indirection with a C# string and wider characters for Unicode support.</p>\n"}, {'answer_id': 64755, 'author': 'Bob King', 'author_id': 6897, 'author_profile': 'https://Stackoverflow.com/users/6897', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 64809, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>As far as I know, all classes in C# (not sure about the others) are reference types.</p>\n'}, {'answer_id': 64924, 'author': 'Chris Ammerman', 'author_id': 2729, 'author_profile': 'https://Stackoverflow.com/users/2729', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<p>Also note that you can\'t pass an argument as "const" in C#, regardless whether it is a value type or a reference type.</p>\n'}, {'answer_id': 65099, 'author': 'Jesse C. Slicer', 'author_id': 3312, 'author_profile': 'https://Stackoverflow.com/users/3312', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n\n<p>Long story short, don't go out of your way thinking of pointers in C#.</p>\n"}]
['https://Stackoverflow.com/questions/64689', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1463/']