Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7669035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:30:19+00:00 2026-05-31T15:30:19+00:00

I made a function and I was to pass array of strings in it

  • 0

I made a function and I was to pass array of strings in it like string[] aa= null
In function definition I made some update in it and then I found that its value was not updated when function was executed/returned .I am unable to understand which things in C# we are required to mention ref keyword . Doesn’t it always pass parameters by reference why need to mention ref ? which are the objects which require to be mentioned with ref in order to pass by reference ?

Code was like this ,just trying to show what was I doing,I was unable to update value without use of ref .

string[] temp = null 
foo(ref temp); 
//function definition
void foo (ref string[] temp) 
{
temp = {"Hello World ","You must be updated now"}
}
foreach(string s in temp)
System.Console.WriteLine(s)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:30:21+00:00Added an answer on May 31, 2026 at 3:30 pm

    First, you’re pseudo code should work. But, before we get to that, there are three things at play here: value types, reference types, and the “ref” keyword.

    Value types are typically your simple basic types like int, double, etc. string is a weird one as it is considered a value type.

    More complex types, such as arrays and classes are reference types.

    When you pass value types like ints and doubles then you are passing a copy of the value so if you pass int x = 10 into a method a change to x in the method won’t be reflected once you leave the method. On the other hand, if you pass MyClass class1, any changes to the properties within class1 will be reflected outside the function. Just don’t try to new up a new class1 inside your method because that won’t change outside the caller.

    If you want to change a value type within a method, you pass by ref. If you want to new up a new class or array, then you pass by ref.

    One more thing: it’s not that black-and-white between using out vs. ref. You would use out only if the design for the method was to always create your class or array only inside the method. You would use ref on a reference type if you wanted to allow the possibility to create a new object. Like,

        //function definition 
        void foo (ref string[] temp)  
        {
            if(temp == null)
            {
                temp = new string[] { "Hello World ", "You must be updated now" };
            }
            else
            { 
                 // do something with the existing temp
            }
        } 
    

    Finally, if this is your actual code:

            string[] temp = null;
            foo(ref temp);
    
            foreach (string s in temp)
                System.Console.WriteLine(s);
    

    Later:

        //function definition 
        void foo (ref string[] temp)  
        {
            temp = new string[] { "Hello World ", "You must be updated now" };
        } 
    

    Then, it should actually work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made some function that generates an email template. Code it generates is pure
I'm trying to pass the array to the generate function and use its elements
I made a function in C, that concatenates a variable number of strings. That's
How can I pass arguments to my lua dll function? I made a simple
I made this function to verify a user's twitter credentials. Its running on two
Hi have made this function which is made to replicate an error that I
I have made a function to cound the weeks in a year, and that
I made this simple function to filter the data. I add the symbols that
As i am reading manual of php for array functions, i see that some
I'm trying to pass an array to jquery's .ajax() function's data parameter. The first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.