How to convert a nullable int to common int

4

I have a method that receives an integer that can be null , in case some method executions happen, but when I use this same variable in a place that uses int that can not be null, it appears that the overload is incorrect.

So I would like to know how I can do this conversion, is there any method that does this as ToString ?

Code with problem

public HttpResponseMessage Metodo(int? variavel = chamadademetodo.metodo {

            if (variavel == null) {
                ...
            }

            var bla2= blablabla.metodo(variavel, DataContext); //aqui acusa problema
}
    
asked by anonymous 15.05.2017 / 19:51

2 answers

7

You can also use:

int variavelNNula = variavel.GetValueOrDefault();

In case the default of int would be zero.

    
15.05.2017 / 19:57
4

Normally, the following technique will resolve:

public HttpResponseMessage Metodo(int? variavel)
{
    var variavelNaoNula = variavel ?? 0;

    var bla2= blablabla.Metodo(variavelNaoNula, DataContext);
}
    
15.05.2017 / 19:54