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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:38:04+00:00 2026-06-07T23:38:04+00:00

Why the following two code samples produce different output? Case 1 enum EnumType {

  • 0

Why the following two code samples produce different output?

Case 1

enum EnumType
{
    First,
    Second,
    Third
}

class ClassB
{
    public string Func(int index)
    {
        return "Func(int)";
    }

    public string Func(EnumType type)
    {
        return "Func(EnumType)";
    }
}

class Program
{
    static void Main(string[] args)
    {
        ClassB b = new ClassB();
        Console.WriteLine(b.Func(0));
        Console.WriteLine(b.Func(EnumType.First));
        Console.ReadLine();
    }
}

Output:

Func(int)
Func(EnumType)

Case 2

enum EnumType
{
    First,
    Second,
    Third
}

class ClassA
{
    public string Func(int index)
    {
        return "Func(int)";
    }
}

class ClassB : ClassA
{
    public string Func(EnumType enumType)
    {
        return "Func(EnumType)";
    }
}

class Program
{
    static void Main(string[] args)
    {
        ClassB b = new ClassB();
        Console.WriteLine(b.Func(0));
        Console.WriteLine(b.Func(EnumType.First));
        Console.ReadLine();
    }
}

Output:

Func(EnumType)
Func(EnumType)

I am puzzled. Does it mean that Func(EnumType) hides Func(int) declared in the base? If this is the case then why literal 0 is implicitly casted to EnumType in the second case without a warning?

EDIT:

There is even more interesting behavior when I try

Console.WriteLine(b.Func(0));         
Console.WriteLine(b.Func(1));
Console.WriteLine(b.Func(EnumType.First));

What is your guess the output should look like?

here it is:

 Func(EnumType)
 Func(int)
 Func(EnumType)

Any ideas why 0 and 1 are treated differently?

EDIT 2:

It turns out that literal 0 indeed has special meaning in C#.

Here and here I found an excellent description of this behavior (see the accepted answers).

  • 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-07T23:38:07+00:00Added an answer on June 7, 2026 at 11:38 pm

    Yes, it does hide the Func(int) declared in class A.

    Furthermore, please see enum (C# Reference)

    The default underlying type of enumeration elements is int

    You might also want to have a look at Polymorphism (C# Programming Guide)

    EDIT

    If you ere to change

    Console.WriteLine(b.Func(0));          
    Console.WriteLine(b.Func(1)); 
    Console.WriteLine(b.Func(EnumType.First)); 
    

    to

    int i = 0;
    Console.WriteLine(b.Func(i));          
    Console.WriteLine(b.Func(1)); 
    Console.WriteLine(b.Func(EnumType.First)); 
    

    you will find that the output should be

    Func(int)   
    Func(int)   
    Func(EnumType)
    

    It would seem that 0 is implicitly casted to the default enum value if passed directly to the function call.

    EDIT 2

    I checked the IL code and it does seem that it implicitly casts the 0 to an enum

    IL_0000: nop
    IL_0001: newobj instance void ClassB::.ctor()
    IL_0006: stloc.0
    IL_0007: ldloc.0
    IL_0008: ldc.i4.0
    IL_0009: callvirt instance string ClassB::Func(valuetype EnumType)
    IL_000e: call void [mscorlib]System.Console::WriteLine(string)
    IL_0013: nop
    IL_0014: ldloc.0
    IL_0015: ldc.i4.0
    IL_0016: callvirt instance string ClassB::Func(valuetype EnumType)
    IL_001b: call void [mscorlib]System.Console::WriteLine(string)
    IL_0020: nop
    IL_0021: call string [mscorlib]System.Console::ReadLine()
    IL_0026: pop
    IL_0027: ret
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following two code samples represent the same logic. Check to see if a
Consider the following two code samples: NSData *imgData = UIImagePNGRepresentation(imgFull); NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
The following code sample prints: T T[] T[] While first two lines are as
Consider the two following similar code samples. One where clause. bool validFactory = fields
I have the following two code blocks. Code block 1 var checkboxes = $(div.c1
The following two lines of code load an image from an SVG file and
I have the following two chunks of code, I am not so sure what
I have the following two lines of code: var BadResult = (100).ToString(B, new CustomFormatter
What is the difference between the following two snippets of code: using (Object o
Could someone please explain the difference between the following two lines of code: 1.

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.