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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:10:27+00:00 2026-05-29T10:10:27+00:00

Often, while investigating some code I’m developing, I’ll throw in a Console.WriteLine here or

  • 0

Often, while investigating some code I’m developing, I’ll throw in a Console.WriteLine here or there too see a value when the program is run. The drawback to Console.WriteLine is that I have to wrap an expression in parenthesis and possibly break it apart. For example given this expression:

a().b().c();

let’s suppose I want to print the value of b(). I’ll have to do something like:

var val = a().b();

Console.WriteLine(val);

val.c();

That’s alot of editing just to see a value.

My solution has been to use this extension method:

public static T Disp<T>(this T obj)
{
    Console.WriteLine(obj);
    return obj;
}

I can inject a call to Disp in any method chain without altering the value of the overall expression. To see the result of b() in the above example, I’d do:

a.().b().Disp().c()

My question is, is there already some method like Disp in .NET? Is there a better way to implement Disp? Are there drawbacks to this technique?

update 2012-02-09

I also added an overloaded version which accepts a format string:

public static T Disp<T>(this T obj, string format)
{
    Console.WriteLine(string.Format(format, obj));
    return obj;
}
  • 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-29T10:10:27+00:00Added an answer on May 29, 2026 at 10:10 am

    I don’t see a problem with it, although you might consider making it more general with an Action<T> parameter:

    public static T Tap<T>(this T obj, Action<T> act)
    {
        act(obj);
        return obj;
    }
    

    This is similar to Tap in Ruby.

    Instead of your Dump method you can then do:

    a.().b().Tap(Console.WriteLine).c()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often use this code pattern: while(true) { //do something if(<some condition>) { break;
While developing products, we often need to create proprietary tools to test some of
I often run into the situation where I want to disable some code while
While looking through the Java API source code I often see method parameters reassigned
In threads when dealing with cancelation, you often you see code like this while
While developing ASP.NET applications I often need to parse a boolean value given in
Code like this often happens: l = [] while foo: # baz l.append(bar) #
While doing some refactoring I've found that I'm quite often using a pair or
I often listen to music while I code. Switching songs while in the middle
While writing code, I often run into the problem of addressing trivial dependancies. For

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.