# Nullable\_T

***

**1. Checking for Null with Nullable**

```csharp
int? number = null;
if (number.HasValue)
{
    Console.WriteLine(number.Value);
}
else
{
    Console.WriteLine("Number is null");
}
```

**2. Assigning Default Value to Nullable**

```csharp
int? age = null ?? 0; // Assigns 0 if age is null
```

**3. Coalescing Operator with Nullable**

```csharp
string name = person?.Name ?? "No name specified";
```

**4. Conditional Operator with Nullable**

```csharp
int result = age.HasValue ? age.Value : 0;
```

**5. Null Conditional Operator with Nullable**

```csharp
Console.WriteLine(person?.Address?.Street ?? "No address specified");
```

**6. Using Nullable in LINQ Queries**

```csharp
var users = db.Users.Where(u => u.Email != null);
```

**7. Nullable in Dictionary Keys and Values**

```csharp
Dictionary<int?, string> nullableDictionary = new Dictionary<int?, string>();
```

**8. Nullable in List**

```csharp
List<int?> nullableList = new List<int?>();
```

**9. Nullable in Array**

```csharp
int?[] nullableArray = new int?[10];
```

**10. Nullable in Interface**

```csharp
interface IPerson
{
    string Name { get; }
    int? Age { get; }
}
```

**11. Nullable in Delegate**

```csharp
Func<int?, bool> isPositive = num => num.HasValue && num.Value > 0;
```

**12. Nullable in Event**

```csharp
public event EventHandler<int?> MyEvent;
```

**13. Nullable in Constructor**

```csharp
public MyClass(int? id)
{
    this.Id = id;
}
```

**14. Nullable in Property**

```csharp
public class Person
{
    public string Name { get; set; }
    public int? Age { get; set; }
}
```

**15. Nullable in Field**

```csharp
private int? _age;
public int? Age
{
    get { return _age; }
    set { _age = value; }
}
```

**16. Nullable in Indexer**

```csharp
public class ArrayWrapper<T>
{
    private T[] _array;
    public T this[int? index]
    {
        get { return _array[index.Value]; }
        set { _array[index.Value] = value; }
    }
}
```

**17. Nullable in Method**

```csharp
public bool IsValid(int? id)
{
    return id.HasValue && id.Value > 0;
}
```

**18. Nullable in Generic Type**

```csharp
public class NullableList<T> where T : struct
{
    private List<T?> _list;
}
```

**19. Nullable in Enum**

```csharp
public enum MyEnum
{
    Option1,
    Option2,
    None = null
}
```

**20. Nullable in Switch Case**

```csharp
switch (age)
{
    case null:
        Console.WriteLine("Age is not specified");
        break;
    case 18:
        Console.WriteLine("Age is 18");
        break;
    default:
        Console.WriteLine("Age is other value");
        break;
}
```

**21. Nullable in String Interpolation**

```csharp
string message = $"The age is {age ?? "not specified"}";
```

**22. Nullable in Conditional Attribute**

```csharp
[Conditional("DEBUG")]
public void Log(string message)
{
    Console.WriteLine(message);
}
```

**23. Nullable in Interlocked Operations**

```csharp
Interlocked.CompareExchange(ref _age, age, null);
```

**24. Nullable in Custom Attributes**

```csharp
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class NullableAttribute : Attribute
{
    public bool AllowNulls { get; set; }
}
```

**25. Nullable in Extension Methods**

```csharp
public static bool IsNullOrEmpty(this string? value)
{
    return string.IsNullOrEmpty(value);
}
```

**26. Nullable in Anonymous Types**

```csharp
var person = new { Name = "John", Age = (int?)null };
```

**27. Nullable in XML Serialization**

```csharp
[XmlElement(IsNullable = true)]
public int? Id { get; set; }
```

**28. Nullable in JSON Serialization**

```csharp
public class Person
{
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? Age { get; set; }
}
```

**29. Nullable in Entity Framework Core**

```csharp
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? Age { get; set; }
}
```

**30. Nullable in Dapper**

```csharp
var age = connection.QuerySingleOrDefault<int?>("SELECT Age FROM People WHERE Id = @id", new { id = 1 });
```

**31. Nullable in AutoMapper**

```csharp
CreateMap<Person, PersonDTO>()
    .ForMember(dest => dest.Age, opt => opt.MapFrom(src => src.Age ?? 0));
```

**32. Nullable in Dapper.Contrib**

```csharp
var people = connection.Query<Person>("SELECT * FROM People WHERE Age IS NOT NULL");
```

**33. Nullable in Newtonsoft.Json**

```csharp
var json = JsonConvert.SerializeObject(person, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
```

**34. Nullable in GraphQL**

```csharp
public class PersonSchema : GraphQL.Types.Schema
{
    public Query Query { get; set; }

    public PersonSchema()
    {
        Query = new Query
        {
            FieldAsync<NullableType<IntGraphType>>("age", resolve: context => context.Source.Age)
        };
    }
}
```

**35. Nullable in EF Core 3.1**

```csharp
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? Age { get; set; }
}

public class DbContext : Microsoft.EntityFrameworkCore.DbContext
{
    public DbSet<Person> People { get; set; }
}
```

**36. Nullable in EF Core 5.0**

```csharp
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? Age { get; set; }
}

public class DbContext : Microsoft.EntityFrameworkCore.DbContext
{
    public DbSet<Person> People { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Allow null values for Age column
        optionsBuilder.UseSqlite("Data Source=mydb.db")
            .EnableNullChecks();
    }
}
```

**37. Nullable in SQL Server**

```sql
CREATE TABLE People (
    Id INT NOT NULL,
    Name VARCHAR(255) NOT NULL,
    Age INT NULL
);
```

**38. Nullable in PostgreSQL**

```sql
CREATE TABLE people (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    age INTEGER NULL
);
```

**39. Nullable in MySQL**

```sql
CREATE TABLE people (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    age INT NULL
) ENGINE=InnoDB;
```

**40. Nullable in Oracle**

```sql
CREATE TABLE people (
    id NUMBER(10) NOT NULL,
    name VARCHAR2(255) NOT NULL,
    age NUMBER(10) NULL
);
```

**41. Nullable in Azure SQL Database**

```sql
CREATE TABLE people (
    id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    age INT NULL
);
```

**42. Nullable in Amazon RDS for MySQL**

```sql
CREATE TABLE people (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    age INT NULL
) ENGINE=InnoDB;
```

**43. Nullable in Google Cloud SQL for PostgreSQL**

```sql
CREATE TABLE people (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    age INTEGER NULL
);
```

**44. Nullable in SQLite**

```sql
CREATE TABLE people (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    age INTEGER NULL
);
```

**45. Nullable in MongoDB**

```csharp
var person = new BsonDocument
{
    { "name", "John" },
    { "age", BsonValue.CreateNull() }
};
```

**46. Nullable in Redis**

```csharp
var key = "person:1";
var person = new HashEntry[]
{
    new HashEntry("name", "John"),
    new HashEntry("age", null)
};

redis.HashSet(key, person);
```

**47. Nullable in Azure Cosmos DB**

```csharp
var person = new Document
{
    { "id", "1" },
    { "name", "John" },
    { "age", null }
};

var client = new CosmosClient(connectionString);
var container = client.GetContainer("people");
await container.CreateItemAsync(person);
```

**48. Nullable in GraphQL.NET**

```csharp
public class PersonType : ObjectGraphType<Person>
{
    public PersonType()
    {
        Field(p => p.Name, nullable: true);
        Field(p => p.Age, nullable: true);
    }
}
```

**49. Nullable in MediatR**

```csharp
public class GetPersonQuery : IRequest<Person>
{
    public int Id { get; set; }
}

public class GetPersonQueryHandler : IRequestHandler<GetPersonQuery, Person>
{
    public async Task<Person> Handle(GetPersonQuery request, CancellationToken cancellationToken)
    {
        // Database call...
        return new Person { Name = "John", Age = null };
    }
}
```

**50. Nullable in Blazor**

```csharp
@page "/person"

<h1>@person.Name</h1>
<p>@person.Age ?? "Age not specified"</p>

@code {
    private Person person;

    protected override async Task OnInitializedAsync()
    {
        // API call...
        person = new Person { Name = "John", Age = null };
    }
}
```
