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

  • Home
  • SEARCH
  • 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 550615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:18:26+00:00 2026-05-13T11:18:26+00:00

I have the following code: public int Method(MyEnum myEnum) { switch (myEnum) { case

  • 0

I have the following code:

public int Method(MyEnum myEnum)
{
    switch (myEnum)
    {
        case MyEnum.Value1: return 1;
        case MyEnum.Value2: return 2;
        case MyEnum.Value3: return 3;
    }
}

public enum MyEnum
{
    Value1,
    Value2,
    Value3
}

And I get the error: "Not all code paths return a value". I do not understand how that switch statement could ever not jump to one of the specified cases.

Can an enum somehow be null?

  • 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-13T11:18:27+00:00Added an answer on May 13, 2026 at 11:18 am

    There’s nothing to say that the value of myEnum will be one of those values.

    Don’t mistake enums for being a restrictive set of values. It’s really just a named set of values. For example, I could call your method with:

    int x = Method((MyEnum) 127);
    

    What would you want that to do? If you want it to throw an exception you can do that in a default case:

    switch (myEnum)
    {
        case MyEnum.Value1: return 1;
        case MyEnum.Value2: return 2;
        case MyEnum.Value3: return 3;
        default: throw new ArgumentOutOfRangeException();
    }
    

    Alternatively you could use Enum.IsDefined upfront, if you want to do some other work before the switch statement. That has the disadvantage of boxing… there are some ways round that, but they’re generally more work…

    Sample:

    public int Method(MyEnum myEnum)
    {
        if (!IsDefined(typeof(MyEnum), myEnum)
        {
            throw new ArgumentOutOfRangeException(...);
        }
        // Adjust as necessary, e.g. by adding 1 or whatever
        return (int) myEnum; 
    }
    

    This assumes there’s an obvious relationship between the underlying values in MyEnum and the value you want to return.

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

Sidebar

Ask A Question

Stats

  • Questions 310k
  • Answers 310k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The <%@ Page %> directive is not required. Without it,… May 13, 2026 at 10:15 pm
  • Editorial Team
    Editorial Team added an answer git log --follow --format=%ad --date default <FILE> | tail -1… May 13, 2026 at 10:15 pm
  • Editorial Team
    Editorial Team added an answer Here's another: http://qooxdoo.org They say: "No HTML, CSS nor DOM… May 13, 2026 at 10:15 pm

Related Questions

I have the following code: public class DataReader<T> where T : class { public
I have decided it is impossible to do the following (equivalent) enum operations via
I have implemented the IEquatable interface in a class with the following code. public
I have the following code, which splits up a Vector into a string vector
I have the following assignment for homework. Requirements Design a class called TokenGiver with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.