0

I'm looking at a function:

public void foo(int? id) {
...
}

called like:

foo(new int?());

What is happening here? What is the value of id inside the function when it is passed new int?()? It doesn't appear to be anything - the function is using it for a SQL stored procedure, for which I can see in a trace that it is being passed as null. I'm expecting this stored procedure to be called with a definite, actual integer, and I'm wondering why this code might not be specifying one, unless there is something I'm not understanding about this.

Gerald
  • 521
  • 1
  • 6
  • 16
  • `int?` is a *nullable integer*. You're basically passing in `null` to your function and wonder why it isn't a number. If you want to pass a number, why aren't you? –  Aug 29 '17 at 15:14
  • what does the procedure do with that value? insert it into a table? – Mong Zhu Aug 29 '17 at 15:16
  • @Amy Well, I said "probably" - I base my assumption on hints like "I'm looking at a function" and the overall wording of the question. But I could be wrong, I admit. – Fildor Aug 29 '17 at 15:18
  • `int?` is syntax sugar for `Nullable`. A struct type, using a new expression to initialize a struct it is standard C# syntax. Most programmers would prefer `foo(null);` to keep their code readable. – Hans Passant Aug 29 '17 at 15:19
  • @Amy That is correct - it is not my code. – Gerald Aug 29 '17 at 15:19
  • If you feel this is not a duplicate please let me know. I do feel that the accepted answer in the marked duplicate addresses the first 2 questions quit well. The questions about your stored procedure are specific to how your code is structured, if you have questions about what that code does or how it functions I would recommend creating a new question with the specifics about that code. – Igor Aug 29 '17 at 15:25
  • "I'm expecting this stored procedure to be called with a definite, actual integer, " why? what does the stored procedure do with that value? what does it do in the case of `null` ? – Mong Zhu Aug 29 '17 at 15:26
  • It looks up *another* ID in the database based on *that* ID, then uses that new ID in a filter in an update statement - when that filter is WHERE newID = null, it does not find the record to update. – Gerald Aug 29 '17 at 16:09

3 Answers3

3

Because the default value for any Nullable<T> type is null.

See : What is the default value of the nullable type "int?" (including question mark)?

int? is a shortcut / syntactic sugar for Nullable<int>

Pac0
  • 21,465
  • 8
  • 65
  • 74
2

int? is shorthand for Nullable<int>, so this:

new int?()

is actually passing null.

Ric
  • 12,855
  • 3
  • 30
  • 36
2

What is the value of id inside the function when it is passed new int?()?

new int?() is the call of the parameterless constructor of the type nullable int. If you don't pass any value in it it will have the default value null. There is a second constructor which can take an int number as parameter and initialize it new int?(23)

If you want to see how the constructors work in detail have a look at the source code

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76