VB.NET: Если я передаю Строку ByVal в функцию, но не меняю струну, у меня есть одна или две строки в памяти?

Он вызывает файлы, включенные в этот конкретный пакет, который объявлен внутри класса BundleConfig в папке App_Start.

В этом конкретном случае вызов @Styles.Render("~/Content/css") вызывает "~ / Content / site.css".

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
7
задан Dan F 27 June 2009 в 12:51
поделиться

5 ответов

By the way, string interning is completely unrelated to that. The rule for passing parameters to functions is the same for all reference types (and really, all types), no matter how they are managed internally.

The rule is simple and you have stated it correctly: pass by value copies the reference, not the target. No heap space is copied here.

9
ответ дан 6 December 2019 в 14:07
поделиться

При передаче объектов ByVal создается копия указателя , а не сам объект. Вот демонстрация:

Module Module1
    Dim original As String = "Hello world"

    Sub PassByReferenceTest(ByVal other As String)
        Console.WriteLine("object.ReferenceEquals(original, other): {0}", _
            Object.ReferenceEquals(original, other))
    End Sub

    Sub Main()
        PassByReferenceTest(original)
        Console.ReadKey(True)
    End Sub
End Module

Эта программа выводит следующее:

object.ReferenceEquals(original, other): True

Итак, исходная строка и строка, которую мы передали по значению, существуют по одному и тому же адресу в адресе памяти. Вы не делаете копию самой строки.

2
ответ дан 6 December 2019 в 14:07
поделиться

Is short, no. It passes a ref to the string. Only one instance of the string itself.

0
ответ дан 6 December 2019 в 14:07
поделиться

No. it still uses the copy of the reference to the "Blah".
What makes you think, it will?

On a side note, string are interned.

string s = "hello";
string t = "hello";

s & t both refer to the same string (because it is interned). If you modify s or t, it will create a new string, then.

4
ответ дан 6 December 2019 в 14:07
поделиться

строка является ссылочным типом. Если вы передаете его по значению, то вы передаете значение ссылки.

Единственный способ получить еще одну копию в куче - это изменить значение переменной.

0
ответ дан 6 December 2019 в 14:07
поделиться
Другие вопросы по тегам:

Похожие вопросы: