Home Backend Development C#.Net Tutorial Detailed introduction to the special case string of reference types in C#

Detailed introduction to the special case string of reference types in C#

Sep 18, 2017 am 11:02 AM
.net string

When programming in C#, the string (string) type is often used. It is also a reference type, but it is not used as a reference everywhere. It is a special case. I will list them one by one for your own convenience:

1) Direct assignment of strings: The string itself is a reference type, and an instance of the new object method should be used. However, for the convenience of everyone, Microsoft can directly define string variables and perform assignment operations. , for example: string a = "My Chinese Heart"; , this just simplifies our operations;

2) Assign a string to another string variable: Normal reference type The two reference variables will point to the same address, but when one string variable is assigned to another string variable, two different address spaces are created, for example:

 string a = "12345"; string b = a;
Copy after login

The above code is two Different address references, just assign the string content of a to b, the contents of a and b are the same;

3) Multiple assignments of the same string: Follow the general Assigning a value to a string variable only changes its content and does not change its address. However, strings are weird. When the same string variable is assigned a value again, it will reallocate the memory space and create a new address. , and then assign the address

to the original string variable. For example:

  string a= "123";  a = "456"
Copy after login

When a is assigned a value of "456" for the second time, it creates a new memory space. Then assign the newly created memory address to the a variable, and discard the previous "123" memory, waiting for garbage collection.

4) Passing a string as a function parameter: When a string is passed as a parameter of a function, it is a reference type. The address reference of the variable should be passed, and later in the function Modification of this parameter will change the value of the string, but let me tell you, it only passes a copy of the string to the

function body. Modifying the character in the function does not affect the transfer. The value of the parameter, of course, the transmission of the string can also be used as a reference type, the main REF can be added. :

When strings are used as references, the two reference types are compared for equality, only the addresses of the two references are compared for equality (unless you overload the Equal function), but when we compare strings, we find that in fact What they compare is the content of the string, not the reference address.

This is the reference string class overloaded with the equal function, which refers to comparing the content of the characters. At this point, the results of == and equal are actually are the same;

6) Memory residency of strings: When we create variables with the same string content, these string variables actually point to the same memory address , this is a bit like inlining in C++;

If there are any other special points, please ask the experts to give advice. If you have any different opinions, please leave a message. Everyone can learn from each other, so that novices can grow up step by step. ,hehe! ! !

The above is the detailed content of Detailed introduction to the special case string of reference types in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

What are the employment prospects of C#? What are the employment prospects of C#? Oct 19, 2023 am 11:02 AM

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

Golang function byte, rune and string type conversion skills Golang function byte, rune and string type conversion skills May 17, 2023 am 08:21 AM

In Golang programming, byte, rune and string types are very basic and common data types. They play an important role in processing data operations such as strings and file streams. When performing these data operations, we usually need to convert them to each other, which requires mastering some conversion skills. This article will introduce the byte, rune and string type conversion techniques of Golang functions, aiming to help readers better understand these data types and be able to apply them skillfully in programming practice.

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

See all articles