nullable var using implicit typing in c#?
Asked Answered
N

7

8

Is there anyway to have the var be of a nullable type?

This implicitly types i as an int, but what if I want a nullable int?

var i = 0;

Why not support this:

var? i = 0;

Nicotinism answered 11/2, 2009 at 15:54 Comment(1)
Is your question "how do I do this in C#?" or "Why doesn't C# allow this?" The first one we can answer, but the second one belongs to Microsoft (we can only speculate).Particularism
A
6

Why support it? If that's what you mean, you should say var i = (int?)0; instead.

(Well, you should probably just say int? i = 0, but for more complicated types, etc.)

Antabuse answered 11/2, 2009 at 15:58 Comment(0)
M
6

var is typed implicitly by the expression or constant on the right hand side of the assignment. var in and of itself is not a type so Nullable<var> is not possible.

Muzzleloader answered 11/2, 2009 at 16:0 Comment(1)
Right, but they could do some classic syntactic sugar magic, where var? i = 0 compiles into var i = (int?)0, so it's easier to type for us, and should be easy-ish to implement for them. Easier than having to completely rework var to make it a typeLias
P
2

The problem deals with nullable types.

For instance, you cannot create a nullable string, which in turn prevents you from creating a nullable var, since var could be a string.

Principality answered 11/2, 2009 at 16:2 Comment(0)
B
0

My answer is kind of along these lines. "var" is implicitly typed. It figures out what type it is by the value supplied on the right-hand side of the assignment. If you tell it that it's nullable it has no idea what type to be. Remember, once it's assigned, that's the type it's going to be forever.

Benedick answered 11/2, 2009 at 16:9 Comment(0)
A
0

Try this - is this what you're talking about?

    static void Main(string[] args)
    {
        for (int i=0; i < 10; i++)
        {
            var j = testMethod();
            if (j == null)
            {
                System.Diagnostics.Debug.WriteLine("j is null");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(j.GetType().ToString());
            }
        }
    }

    static int? testMethod()
    {
        int rem;
        Math.DivRem(Convert.ToInt32(DateTime.Now.Millisecond), 2, out rem);
        if (rem > 0)
        {
            return rem;
        }
        else
        {
            return null;
        }
    }
Amanda answered 11/2, 2009 at 16:24 Comment(0)
D
0

VAR is an implicit type determined by the compiler at the time of compilation. It is assigned upon first use. IE when you set var I = 1, var is an int because that is what the number 1 is. If you instead have var I = SomeFunction() where some function returns a nullable int than I will be set as int?. Now with that said var should not be used in a case where you want to control what the variable type is.

So bottom line, as per your example using var is wrong and should be explicitly set to int? from the start.

  • Darroll
Debouch answered 1/11, 2010 at 20:57 Comment(0)
A
0

I wouldn't even use var in this case. If it's a complex type or a simple one such as an int I would use type "object".

Example:

//Define all global class variables
List<int> m_lInts = New List<int>();
List<string> m_lStrs = New List<string>();

public static object FindMyObject(string sSearchCritera)
{
  //Initialize the variable
  object result = null;

  if (result == null)
  {
    //Search the ints list
    foreach(int i in m_lInts)
    {
      if (i.ToString() == sSearchCritera)
      {
        //Give it a value
        result = (int)i;
      }
    }
  }

  if (result == null)
  {
    //Search the ints list
    foreach(string s in m_lStrs)
    {
      if (s == sSearchCritera)
      {
        //Give it a value
        result = (string)s;
      }
    }
  }

  //Return the results
  return result;
}
Advantage answered 24/8, 2012 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.