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 9068325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:09:53+00:00 2026-06-16T17:09:53+00:00

This post is not really about the bug in the code down below, I

  • 0

This post is not really about the bug in the code down below, I can myself imagine dozens of ways to solve it, its more principally related to simplicity, elegance and performance!

I had to do a very simple text parser, something like that:

string ParseWord(ref string Line, ref int Pos) {
    // Inspect Line from Pos, increment Pos and return next Word
}

void ParseText(string[] Lines) {
    foreach (string Line in Lines) {
        int Pos = 0;
        string Word;
        while ((Word = ParseWord(ref Line, ref Pos)) != null) {
            // Do something with Word
        }
    }
}

Unfortunately it does not work that way, because the “foreach” variable “Line” is passed by reference which is not allowed.

As you might have noticed by now I am usually a C++ programmer.
My thought was that passing the string as reference would be faster, cause it does not have to be copied each time.

First question: Is that assumption still valid in c#?

Second question: How can I pass a reference to that “Line” and still use “foreach” – there is no “const” in c#

Third question: Am I thinking some inappropriate C++ way and something like that would we done different in c#?

  • 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-06-16T17:09:54+00:00Added an answer on June 16, 2026 at 5:09 pm

    No, that’s not a valid assumption. By default, C# passes strings by reference, which means a pointer is efficiently passed under the hood.

    The only reason to pass a string using ref or out is if the method being called needs to modify the string and that modification needs to be reflected in the calling method. Because strings are immutable, “changing” the string in the target method actually creates a new string, which will not affect the string that was originally passed. In this case, using ref or out will cause the pointer in the calling method to be updated to point to the new, modified string.

    If you aren’t changing the string, then just pass the string normally and it’s very efficient. I would recommend articles such as C# Concepts: Value vs Reference Types.

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

Sidebar

Related Questions

I am not sure if I can post this sort of question (apologies in
After reading more and more about IoC containers, I read this post about not
I have this code in the asp.net application start evert, and I'm not really
I read this post, but I do not think it really answered my question.
Really simple question about C++ constness. So I was reading this post , then
Caveat : I'm not sure if this post might be better suited for SuperUser
I thought I would post this here not so much as a question but
I've tagged this post as WordPress, but I'm not entirely sure it's WordPress-specific, so
I am having the same problem as this post but the answer does not
I'm not sure if this is the right place to post this question but

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.