Question

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;

Was it helpful?

Solution

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.)

OTHER TIPS

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.

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.

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.

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;
        }
    }

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

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top