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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:44:34+00:00 2026-05-13T21:44:34+00:00

As C# lacks support for freestanding functions, I find it hard to find a

  • 0

As C# lacks support for freestanding functions, I find it hard to find a place to put conversion functions. For example, I’d like to convert an enum to a number. In C++, I would make the following freestanding function for this:

UINT32 ConvertToUint32(const MyEnum e);

How can I elegantly do this in C#? Should I make a dummy static class for holding the function, and if so, how can I find a meaningful name for it? Or should I make a partial class Convert?

Any ideas?

Thanks in advance.

Update: In retrospect, my example was not very well chosen, as there exists a default conversion between enum and int. This would be a better example:

Person ConvertToPerson(const SpecialPersonsEnum e);
  • 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-13T21:44:34+00:00Added an answer on May 13, 2026 at 9:44 pm

    I’d go with:

    namespace ExtensionMethods
    {
        public static class MyExtensions
        {
            public static int ConvertToInt(this MyEnum e)
            {
                var m;
    
                // ... Implementation
    
                return m;
            }
        }   
    }
    

    Then you’d simply use MyEnum.ConvertToInt(); The same can be done for multiple conversions all from within the same class. Extension methods are in a nutshell, damn sexy.


    Also, Eric’s comment about Type Converters got me googling. Pretty awesome, however I’m not sure how to use them with an Enum, but for other conversions, they’re clean as a whistle to implement. Have a look here:

    • http://www.pluralsight.com/community/blogs/fritz/archive/2005/12/09/17343.aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems it hasn't been updated in a while, and lacks support for things like
I'm getting this warning from sccanf() : ../../../../ext/oedipus/oedipus.c:211: warning: conversion lacks type at end
My Leopard has no manuals for Wget and compinit, for example. It lacks in
I would like to do some simple layout math. I remember the syntax below
I've tried Apache FTP Server but it lacks document and support. And it's totally
Magento is a great product but out-of-the-box it really lacks recurring billing support. I've
I'm looking for an internal representation format for text , which would support basic
I want to use a case statement in Pervasive as it lacks support for
I would like to map the elements of a Scala tuple (or triple, ...)
I avoided CodePlex because of it's lack of support for proper SVN and was

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.