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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:03:37+00:00 2026-05-21T09:03:37+00:00

I was playing with optional parameters to see how they would work with interfaces

  • 0

I was playing with optional parameters to see how they would work with interfaces and I came across a strange warning. The setup I had was the following code:

 public interface ITestInterface
 {
     void TestOptional(int a = 5, int b = 10, object c = null);
 }

 public class TestClass : ITestInterface
 {

     void ITestInterface.TestOptional(int a = 5, int b = 5, object c = null)
     {
        Console.Write("a=" + a + " b=" + b + " c=" + c);
     }
 }

The compiler gives me the following warnings:

  • The default value specified for parameter ‘a’ will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
  • The default value specified for parameter ‘b’ will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
  • The default value specified for parameter ‘c’ will have no effect because it applies to a member that is used in contexts that do not allow optional arguments

If I run this with the following code:

class Program
{
    static void Main(string[] args)
    {
        ITestInterface test = new TestClass();
        test.TestOptional();
        Console.ReadLine();
    }
}

I get the output of “a=5 b=10 c=” as I’d expect.

My question is what is warning for? What contexts is it referring to?

  • 1 1 Answer
  • 2 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-21T09:03:38+00:00Added an answer on May 21, 2026 at 9:03 am

    The problem with optional arguments in C# is whether the callee sees the object as a TestClass or an ITestInterface. In the first case, the values declared in the class apply. In the second case the values declared in the interface apply. It is because the compiler uses the statically available type information to construct the call. In case of an explicit interface implementation the method is never called ‘for a class’, always ‘for an interface’

    The C# Language Specification in 10.6.1 states:

    If optional parameters occur in an implementing partial method declaration (§10.2.7) , an explicit interface member implementation (§13.4.1) or in a single-parameter indexer declaration (§10.9) the compiler should give a warning, since these members can never be invoked in a way that permits arguments to be omitted.

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

Sidebar

Related Questions

I've been playing around with optional parameters and came accross the following scenario. If
Playing with css border, margin, padding and width I came across an extra pixel...
While playing with standard library i've found a strange difference between python2 and python3.
Playing with Erlang, I've got a process-looping function like: process_loop(...A long list of parameters
playing around with a new samsung tablet i noticed they have mini apps on
Playing a video to start the application I have this code, I can't see
I'm playing with an idea of creating client that would use the torrent protocol
when playing a video inside a webview and looking at instruments - I see
In soundmanger2 http://www.schillmania.com/projects/soundmanager2/ I created the following sound object: soundManager.setup({ url: '<?php echo $html->url('/')
Playing around with type-classes I came up with the seemingly innocent class Pair p

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.