© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
90 replies
wcasa

✅ reference type values are strange..

static void bar(ref int[] baz, int newElem)
{
    int newLength = baz.Length + 1;
    int [] quux = new int [newLength];

    for(int i = 0; i < baz.Length; i++)
        quux[i] = baz[i];

    quux[newLength - 1] = newElem;

    baz = quux;
}
    
static void Main() 
{
    int[] foo = {1, 2, 3, 4};
    foreach(int i in foo)
        Console.Write(i + " ");
    
    Console.WriteLine();
    bar(ref foo, 42);
    
    foreach(int i in foo)
        Console.Write(i + " ");
}
static void bar(ref int[] baz, int newElem)
{
    int newLength = baz.Length + 1;
    int [] quux = new int [newLength];

    for(int i = 0; i < baz.Length; i++)
        quux[i] = baz[i];

    quux[newLength - 1] = newElem;

    baz = quux;
}
    
static void Main() 
{
    int[] foo = {1, 2, 3, 4};
    foreach(int i in foo)
        Console.Write(i + " ");
    
    Console.WriteLine();
    bar(ref foo, 42);
    
    foreach(int i in foo)
        Console.Write(i + " ");
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

String : Immutable reference type
C#CC# / help
3y ago
Expressing intent that method parameters are immutable for reference type arguments
C#CC# / help
3y ago
Reference type variables themselves are stored on the stack, is that correct?
C#CC# / help
2y ago
✅ Strange colleague
C#CC# / help
2y ago