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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:59:40+00:00 2026-05-26T00:59:40+00:00

I am experimenting with fluent extension methods. I have the following simple extension method

  • 0

I am experimenting with fluent extension methods.

I have the following simple extension method to perform a safe cast.

     public static T As<T>(this Object source)
         where T : class
     {
         return source as T;
     }

This worked well, but when I tried to make it intuitive to use valuetypes with an overload

     public static T As<T>(this ValueType source)
         where T : struct
     {
         return (T)source;
     }

I ran into problems. The method resolution logic always chooses the first method above, and gives a syntax error (accurately) that the struct is not a class.

Is there a way to handle the above, or should I go the route of removing the constraint while testing for and handling all types in the same method?

==== Edit: to answer questions ====

I am compiling this against the 3.5 framework. I’m not really trying to accomplish anything in particular; this is just an experiment with the above. My interest was piqued and I threw together some code.

I’m not particularly concerned with it remaining a ‘safe’ cast. That is how it started, and can be kept safe with default() — but that’s not really the focus of the question and code to ensure ‘safeness’ would just obscure.

As to the expressiveness, no value.As<int>() is not any more expressive than (int)value; but why should the user of the method have to ‘just know’ it only works with reference types? My trying to work it in was more about the expected behavior of the method than expressive writing.

The code snippet value.As<DateTime>(), gives the error “The type ‘System.DateTime’ must be a reference type in order to use it as parameter ‘T’ in the generic type or method ….As(object)”. From the error message I see it is resolving to use the top method above as it is the one requiring the reference type.

  • 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-26T00:59:41+00:00Added an answer on May 26, 2026 at 12:59 am

    In .NET 4, the second overload is chosen based on your code sample. (Also just tested against .NET 3.5, same result.)

    int myInt = 1;
    long myLong = myInt.As<long>(); // chooses ValueType version
    

    However, this only gets us to the scene of the next crash. The (T)source; results in an invalid cast exception. You could get around that by writing the method as

    public static T As<T>(this ValueType source)
        where T : struct
    {
        return (T)Convert.ChangeType(source, typeof(T));
    }
    

    However, I wonder what you’re actually looking to achieve, as I do not see the immediate benefit. (And for that matter, this isn’t safe like the source as T object version.) For example, how is

    long myLong = myInt.As<long>();
    

    Any more expressive or easier to use than

    long myLong = (long)myInt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While experimenting a bit with C++ templates I managed to produce this simple code,
Im experimenting with the following code private void timer1_Tick(object sender, EventArgs e) { Thread
I am experimenting with NHibernate (version 2.1.0.4000) with Fluent NHibernate Automapping. My test set
While experimenting with this question on collections in Spring.NET , I discovered that Spring
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I'm currently experimenting with build script, and since I have an ASP.net Web Part
While experimenting with iPhone app development, we have several AppIDs which should be deleted
I am experimenting with the c-language right at the moment, yet i have some
I experimenting with Flex Styling, and I came across an alignment issue. I have
I have a simple test that runs a query 5000 times. The linq version

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.