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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:27:14+00:00 2026-06-04T04:27:14+00:00

Not exactly how sure how to title this question, so I hope the title

  • 0

Not exactly how sure how to title this question, so I hope the title works.

The question is, can I use something similar to implicit type syntax on method calls. For example, this is the implicit type syntax I am referring to:

var x = new Y(){Foo = "Bar", Id = 1};

And I want to do something like this:

var x = myInstance.CreateItem(){Foo = "Bar", Id = 1};

Is there anything in C# that supports something like this? I don’t want to do:

x.Foo = "Bar";
x.Id = 1;
...

Please note that CreateItem returns a dynamic type. The CreateItem method and its class cannot be modified.

I would settle for something similar to the With statement in VB.

Thanks in advance.

UPDATE: Attempting Mark Brackett’s solution yielded this code:

TaskItem item = outlook.CreateItem(OlItemType.olTaskItem)._((Action<dynamic>)(i => 
            {
                i.Subject = "New Task";
                i.StartDate = DateTime.Now;
                i.DueDate = DateTime.Now.AddDays(1);
                i.ReminderSet = false;
                i.Categories = "@Work";
                i.Sensitivity = OlSensitivity.olPrivate;
                i.Display = true;

            }));

…

public static class Extension
{
    public static T _<T>(this T o, System.Action<dynamic> initialize) where T : class
    {
        initialize(o);
        return o;
    }

}

The only problem now is that the extension method doesn’t seem to be binding to System._ComObject because I get this error: System._ComObject’ does not contain a definition for ‘_’.

  • 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-04T04:27:15+00:00Added an answer on June 4, 2026 at 4:27 am

    It’s called an “object initializer”, and no – it’s not available for return values (or really, anytime other than with a new statement).

    Syntax wise, about the closest I can think of would be to use an Action delegate (which requires changes to the Builder):

    MyItem CreateItem(Action<MyItem> afterCreate) {
       var i = new MyItem();
       if (afterCreate != null) afterCreate(i);
       return i;
    }
    
    var x = Builder.CreateItem(i => { i.Foo = "Bar"; i.Id = 1; });
    

    If you’re in a JavaScripty mood for short method names for commonly used functions, and can’t change the Builder, I guess an extension method would also work:

    public static T _<T>(this T o, Action<T> initialize) where T : class {
        initialize(o);
        return o;
    }
    
    var x = Builder.CreateItem()._(i => { i.Foo = "Bar"; i.Id = 1; });
    

    Here’s a twist for you. CreateItem() returns a dynamic type

    Contrary to my comment, dynamic does require a few changes. You need to cast to object or the lambda will complain, and then you need to specify dynamic as T or it’ll be inferred as object. Or, create your extension method with Action<dynamic> and no type arguments.

    object CreateItem() {
        return (object)Builder.CreateItem();
    }
    
    public static dynamic __(this object o, Action<dynamic> initialize) {
        initialize(o);
        return o;
    }
    
    var x = CreateItem().__(i => { i.Foo = "Bar"; i.Id = 1; });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: I am not exactly sure how to title or tag this question, so
The question title is a bit strange because I'm not exactly sure how to
Sorry for the vague title, but I'm not exactly sure how I can explain
Sorry about the title, I'm not exactly sure how to explain this with a
I'm getting the error mentioned in the title and not sure what exactly I
Not exactly sure how to look this up, but I'm not finding the solution
Note: I am not exactly sure what to name the question, so if someone
Not sure exactly what I need to do to make this work, so my
I'm not sure exactly how to explain this, so I'll just start with an
I'm not sure exactly where this is happening, but what I'm trying to do

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.