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

  • Home
  • SEARCH
  • 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 890369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:43:16+00:00 2026-05-15T13:43:16+00:00

Before someone shouts out the answer, please read the question through. What is the

  • 0

Before someone shouts out the answer, please read the question through.

What is the purpose of the method in .NET 4.0’s ExpressionVisitor:

public static ReadOnlyCollection<T> Visit<T>(ReadOnlyCollection<T> nodes, Func<T, T> elementVisitor)

My first guess as to the purpose of this method was that it would visit each node in each tree specified by the nodes parameter and rewrite the tree using the result of the elementVisitor function.

This does not appear to be the case. Actually this method appears to do a little more than nothing, unless I’m missing something here, which I strongly suspect I am…

I tried to use this method in my code and when things didn’t work out as expected, I reflectored the method and found:

public static ReadOnlyCollection<T> Visit<T>(ReadOnlyCollection<T> nodes, Func<T, T> elementVisitor)
{
    T[] list = null;
    int index = 0;
    int count = nodes.Count;
    while (index < count)
    {
        T objA = elementVisitor(nodes[index]);
        if (list != null)
        {
            list[index] = objA;
        }
        else if (!object.ReferenceEquals(objA, nodes[index]))
        {
            list = new T[count];
            for (int i = 0; i < index; i++)
            {
                list[i] = nodes[i];
            }
            list[index] = objA;
        }
        index++;
    }
    if (list == null)
    {
        return nodes;
    }
    return new TrueReadOnlyCollection<T>(list);
}

So where would someone actually go about using this method? What am I missing here?

Thanks.

  • 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-15T13:43:16+00:00Added an answer on May 15, 2026 at 1:43 pm

    It looks to me like a convenience method to apply an aribitrary transform function to an expression tree, and return the resulting transformed tree, or the original tree if there is no change.

    I can’t see how this is any different of a pattern that a standard expression visitor, other than except for using a visitor type, it uses a function.

    As for usage:

    Expression<Func<int, int, int>> addLambdaExpression= (a, b) => a + b;
    
    // Change add to subtract
    Func<Expression, Expression> changeToSubtract = e => 
    { 
        if (e is BinaryExpression) 
        { 
            return Expression.Subtract((e as BinaryExpression).Left,
                                       (e as BinaryExpression).Right); 
        }
        else
        {
            return e;
        }
    };  
    
    var nodes = new Expression[] { addLambdaExpression.Body }.ToList().AsReadOnly();
    var subtractExpression = ExpressionVisitor.Visit(nodes, changeToSubtract);
    

    You don’t explain how you expected it to behave and why therefore you think it does little more than nothing.

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

Sidebar

Related Questions

Before someone said that I did not read I may say that I read
someone got this error before? what is the problem, i cannot figure out the
This is a noob question from someone who hasn't written a parser/lexer ever before.
before I start I want to point out that I tagged this question as
A similar question has been asked before by someone else , but there were
I have a piece of code that was written by someone else before the
Does someone knows why the mysql_real_escape_string() function adds three backslashes before quotes, or double
I'm hoping someone has run into this sort of problem before, and can give
I used it before, and was reminded of it when someone asked about a
Before asking the question let me preface with the fact that I am new

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.