C to C #

Is this function declaration in C #:

void foo(string mystring) 

same as in C:

 void foo(char *) 

i.e. In C #, does the called function get a pointer backstage?

+4
source share
11 answers

In this particular case, it is more like:

 void foo(const char *); 

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

+11
source

C # has pointers behind the scenes, although they are more like smart pointers in C ++, so the original pointers are encapsulated. A char * is actually not the same as System.String, since a pointer to char usually means the beginning of a character array, and a C # line is an object with a length field and an array of characters. The pointer points to an external structure that points to something like a wchar_t array, so there is some indirectness with a C # string and wider characters to support Unicode.

+4
source

No. In C # (and all other .NET languages) String is a first-class data type. It is not just an array of characters. You can convert back and forth between them, but they do not behave the same. There are several string manipulation methods (for example, "Substring ()" and "StartsWith") that are available for the String class, which do not apply to arrays at all, that the character array is just an instance.

+4
source

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

+3
source

For value types (int, double, etc.), the function receives a copy of the value. For other objects, this is a link pointing to the source object.

Strings are special because they are immutable. Technically, this means that it will pass the link, but in practice it will behave like a value type.

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

 public void Foo(ref int value) { value = 12 } public void Bar() { int val = 3; Foo(ref val); // val == 12 } 
+2
source

not in line C # is unicode. in C # it is not called a pointer, but a link.

0
source

If you mean whether access to the contents of the character space is allowed, the answer is yes.

0
source

Yes, because the line has a dynamic size, so there should be a lot of memory behind the scenes

However, they do NOT match.

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

0
source

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

0
source

Anything that is not a "value type" that essentially enumerates, Boolean, and built-in numeric types will be passed "by reference", which is probably the same as a C / C ++ mechanism for passing by reference or a pointer. Syntactically and semantically, it is essentially identical to C / C ++ following a link.

Note, however, that C # strings are immutable, so although it is passed by reference, you cannot edit a string without creating a new one.

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

0
source

Although they are really equivalent in the semantic sense (that is, the code does something with a string), C #, like Java, completely hides pointers from everyday use, dropping them into areas such as transitions to native OS - even then framework classes exist that tolerate them well, such as SafeFileHandle.

In short, don't go away thinking about pointers to C #.

0
source

Source: https://habr.com/ru/post/1276697/


All Articles