How do you mark a field as nullable in a class for a SQLite table?

For columns that should be nullable in a class to be used for a SQLite database, can the column be marked as nullable by appending "?"

IOW, if I want "TimeOfTheSeason" to be nullable, is this the way to do it:

public class PlatypiRUs
{
    [SQLite.PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string PlatypusId { get; set; }
    public string PlatypusName { get; set; }
    public DateTime EOW { get; set; }
    public DateTime? TimeOfTheSeason { get; set; }
}

Upvotes: 2

Views: 1894

Answers (1)

Sampath
Sampath

Reputation: 65978

Yes you can do that.

DateTime cannot be used as a constant but you could make it a nullable type (DateTime?) instead.

Give the DateTime? a default value of null, and if it is set to null at the start of your function, then you can initialize it to any value you want.

static void TestMethod(DateTime? dt = null)
{
    if (dt == null)
    {
        dt = new DateTime(1981, 03, 01);
    }

    //...
}

You can call it with a named parameter like this:

TestMethod(dt: new DateTime(2010, 03, 01));

And with the default parameter like this:

TestMethod();

Upvotes: 2

Related Questions