4

I have been asked a question in the interview

public int Add(int? a, int? b)
{
    return a+b;
}

null is passed in a's place. how will you handle this?

I said

if (a == null ) { //do something }
else { // do something }

He didnot say anything.

Waiting for the reply.

2
  • btw have you passed the interview or not? Commented Dec 22, 2014 at 11:53
  • great :) which company? Commented Dec 23, 2014 at 4:35

6 Answers 6

5

As an interviewer I would have expected this:

public int Add(int? a, int? b)
{
    return (a ?? 0) + (b ?? 0);
}

It is called coalescing operator and exists in several languages and is made exactly for that purpose. It has a very low precedence so do not forget the ( )

Sign up to request clarification or add additional context in comments.

Comments

4

Whtever you have said its also correct but interviewer might wanted to know how updated you are..

 if(!a.HasValue)
    {
        a = 0;
    }

or shorter is :-

a = a != null ? a : 0;

and one more operator as suggested by Askolein is ?? as shown below :-

a ?? 0;

Comments

2

he probably wanted it to be

public int Add(int? a, int? b)
{
    a = a!= null ? a : 0;
    b = b!= null ? b : 0;
    return a+b;
}

or something of the sort, meaning you place 0 instead of null

1 Comment

More natural: a = a ?? 0; b = b ?? 0; It almost feels like there should exist an operator so you could say a ??= 0, but there does not. The type of a and b are still Nullable<>, so your code does not compile yet (a+b is Nullable<>, declared return type is not)!
2

It's not a very good question. The code as presented doesn't compile, and the shortest possible fix is actually to change the function signature to

public int? Add(int? a, int? b)

Addition is supported on nullable integers through lifting: adding null to another value will just yield null. This is almost certainly not what the interviewer was angling for; probably they intended you to treat null as if it was 0, for which a ?? 0 suffices. But maybe they wanted (a + b) ?? 0 instead, which returns 0 if either operand is null.

Well, on second thought, maybe it is a good question, to see if you would ask follow-up questions. The most pertinent one would be: "what do you want this function to do when presented with nulls?"

Comments

1

Another solution is to use the GetValueOrDefault method of Nullable<T> which will substitute the default value of T, which is zero for int, if it is null.

public int Add(int? a, int? b)
{
    return a.GetValueOrDefault() + b.GetValueOrDefault();
}

But really you should have asked what the interviewer wanted the method to do with nulls as this could also have been valid

public int Add(int? a, int? b)
{
    if(!a.HasValue)
        throw new ArgumentNullException("a");
    if(!b.HasValue)
        throw new ArgumentNullException("b");
    return a.Value + b.Value;
}

Comments

0

Today I learnt about Null-Conditional Operator ( C# 6.0 )

return (a?. + b?.);

also return (a + b); works fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.