Passing null arguments to C# methods

前端 未结 7 2021
野的像风
野的像风 2020-12-14 06:46

Is there a way to pass null arguments to C# methods (something like null arguments in c++)?

For example:

Is it possible to translate the following c++ functi

7条回答
  •  旧时难觅i
    2020-12-14 07:10

    Starting from C# 2.0, you can use the nullable generic type Nullable, and in C# there is a shorthand notation the type followed by ?

    e.g.

    private void Example(int? arg1, int? arg2)
    {
        if(arg1 == null)
        {
            //do something
        }
        if(arg2 == null)
        {
            //do something else
        }
    }
    

提交回复
热议问题