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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:17:35+00:00 2026-05-12T05:17:35+00:00

I am new to C# 3.0 var type. Here I have a question about

  • 0

I am new to C# 3.0 var type. Here I have a question about this type. Take the following simple codes in a library as example:

 public class MyClass {
    public var Fn(var inValue)
    {
        if ( inValue < 0 )
        {
           return 1.0;
        }
        else 
        { 
           return inValue;
        }
    }
 }

I think the parameter is an anonymous type. If I pass in a float value, then the Fn should return a float type. If a double value type is passed in, will the Fn return a double type? How about an integer value type as input value?

Actually, I would like to use var type with this function/method to get different return types with various input types dynamically. I am not sure if this usage is correct or not?

  • 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-12T05:17:35+00:00Added an answer on May 12, 2026 at 5:17 am

    You can’t use var for return values or parameter types (or fields). You can only use it for local variables.

    Eric Lippert has a blog post about why you can’t use it for fields. I’m not sure if there’s a similar one for return values and parameter types. Parameter types certainly doesn’t make much sense – where could the compiler infer the type from? Just what methods you try to call on the parameters? (Actually that’s pretty much what F# does, but C# is more conservative.)

    Don’t forget that var is strictly static typing – it’s just a way of getting the compiler to infer the static type for you. It’s still just a single type, exactly as if you’d typed the name into the code. (Except of course with anonymous types you can’t do that, which is one motivation for the feature.)

    EDIT: For more details on var, you can download chapter 8 of C# in Depth for free at Manning’s site – this includes the section on var. Obviously I hope you’ll then want to buy the book, but there’s no pressure 🙂

    EDIT: To address your actual aim, you can very nearly implement all of this with a generic method:

    public class MyClass
    {
        public T Fn<T>(T inValue) where T : struct
        {
            Comparer<T> comparer = Comparer<T>.Default;
            T zero = default(T);
            if (comparer.Compare(inValue, zero) < 0)
            {
                // This is the tricky bit.
                return 1.0;
            }
            else 
            { 
                return inValue;
            }
        }
    }
    

    As shown in the listing, the tricky bit is working out what “1” means for an arbitrary type. You could hard code a set of values, but it’s a bit ugly:

    public class MyClass
    {
        private static readonly Dictionary<Type, object> OneValues
            = new Dictionary<Type, object>
        {
            { typeof(int), 1 },
            { typeof(long), 1L },
            { typeof(double), 1.0d },
            { typeof(float), 1.0f },
            { typeof(decimal), 1m },
        };
    
        public static T Fn<T>(T inValue) where T : struct
        {
            Comparer<T> comparer = Comparer<T>.Default;
            T zero = default(T);
            if (comparer.Compare(inValue, zero) < 0)
            {
                object one;
                if (!OneValues.TryGetValue(typeof(T), out one))
                {
                    // Not sure of the best exception to use here
                    throw new ArgumentException
                        ("Unable to find appropriate 'one' value");
                }
                return (T) one;
            }
            else 
            { 
                return inValue;
            }
        }
    }
    

    Icky – but it’ll work. Then you can write:

    double x = MyClass.Fn(3.5d);
    float y = MyClass.Fn(3.5f);
    int z = MyClass.Fn(2);
    

    etc

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

Sidebar

Related Questions

I have this code: public byte[] SerializeToBlob() { using (var buffer = new MemoryStream())
Let's have an example: using (var someObject = new SomeObject()) { var someOtherObject =
Following on from this question , I'm using a simple ViewModel class to test
I'm starting out with Backbone.JS, and have a question about how this should work.
I have a question about the following exception I received trying to complete a
I have a question about resetting the scroll position when I load new data
This is a two-parter question here. The first is a technical question about how
Sorry, I'm new to Java, so this question might be unclear. I have been
Update: Here's a similar question Suppose I have a DataTable with a few thousand
Duplicate : Function overloading by return type? Maybe this is a very silly question

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.