4

I have variable with below

  int? a=null ;
    
  string? b= null;

I need to assign a=b ;

What is best way to assign in c# 9

a= Convert.ToInt32(b);

It is assigning 0 ..hw to assign null if string also null.. i need to know in c# 9

EDIT: Thanks to @john.. i ended up with below code

  if(b is not null) 
     a = Convert.ToInt32(b); 

1 Answer 1

12

I would just be very explicit about it:

int? a = b is null ? null : Convert.ToInt32(b);
Sign up to request clarification or add additional context in comments.

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.