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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:52:13+00:00 2026-06-13T07:52:13+00:00

Possible Duplicate: Is it possible to create an extension method to format a string?

  • 0

Possible Duplicate:
Is it possible to create an extension method to format a string?

I have this class:

public class Person 
{
    public string Name { get; set; }
    public uint Age { get; set; }

    public override string ToString()
    {
        return String.Format("({0}, {1})", Name, Age);
    }
}

The extension method:

public static string Format(this string source, params object[] args)
{
    return String.Format(source, args);
}

And I want to test it, but I have the following strange behavior:

Person p = new Person() { Name = "Mary", Age = 24 };

// The following works
Console.WriteLine("Person: {0}".Format(p));
Console.WriteLine("Age: {0}".Format(p.Age));

// But this gives me a compiler error:
Console.WriteLine("Name: {0}".Format(p.Name));

The compiler error:

Unable to access member ‘string.Format (string, params object [])’ with a reference to an instance. Qualify it with a type name.

Why? How can I solve this problem?

  • 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-13T07:52:14+00:00Added an answer on June 13, 2026 at 7:52 am

    Since p.Name is a string, you have an ambiguous call in the third scenario:

    string.Format(string)
    

    vs.

    {string instance}.Format(object[]);
    

    The resolver chooses the method that fits the signature best (in your case a string vs. an object[]) over the extension method.

    You can solve the problem by either renaming the extension method, or by casting the second parameter to an object to prevent the resolver from choosing the static method:

    Console.WriteLine("Name: {0}".Format((object)p.Name));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Create new class from a Variable in Java I have a string
Possible Duplicate: How to capitalize first letter of each sentence? public static string CapitalizeEachWord(this
Possible Duplicate: Create an alert on any view controller after Facebook request:didFailWithError: I have
Possible Duplicate: Create Excel file in Java How to save output in excel format
Possible Duplicate: C Macros to create strings I have a function which accepts one
Possible Duplicate: Create Generic method constraining T to an Enum is it possible to
Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface?
Possible Duplicate: Extension Method Performance Will I face performance issues when I´m using extension
Possible Duplicate: How can I create a temp file with a specific extension with
Possible Duplicate: Create instance of “Class” Using “Reflection” in JavaScript Java has Class.forName(XYZClass) to

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.