Essential Types in .NET Part-2

By | June 17, 2023

Nullable Value Types

string word = string.Empty; //zero length string
word = null; //set to null reference

double grat = 0;
grat = null; //Can not convert null to double because ut is a non-nullable value type

Nullable<T>

-It can have a value or it can be null

var grat = new Nullable<double>();
//grat is null
var gratHasValue = grat.HasValue(); //false
//get the default value
double defaultValue = grat.GetValueOrDefault(); //0.0
//implicit conversion from double to Nullable<double>
grat = 3.8;
double currentGrat = grat.Value;
//explicit conversion from Nullable<double> to double
currentGrat = (double)grat;
//C# use ? with variable declaration
double? grat = 3.8;

The Problem with Nulls What is new?

-NullReferenceException is a bug. -Provide syntax to identify when we expect a null

-Inconsistency with value types -Make the default expectations of ref types not null

-Reduce likelihood of NullReferenceExceptions

Enable by project for Nullable

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

Arrays and Collections

.NET arrays are derived from System.Array

Arrays are fixed size,zero-indexed and can be multidimensional

Collection Categories

-Generic Collections.

-Specialized Collections.

-Concurrent Collections.

-Immutable Collections


Events and Event Handlers

Events : Communicate between objects

(User interface events,Network events,Timer events,Data-binding events)

Publishers(Event Senders)=> Subscribers(Event Receivers)

What happens?

-The sender raises the event

-The runtime add it is to an event queue

-The queue is processed and each registered handler is invoked

class SampleForm {
  //register an event with .NET
  public event EventHandler? Click;
}

SampleForm form = new SampleForm();
public MainWindow(){
   InitializeComponent();
   form.Click += Form_Click;
}

private void FormClick(object? sender,EventArgs e){
  //do something when the form is clicked
}

Records

-Useful for immutable data

-Records are immutable unless you opt out of immutability

public record class CollectibleCar(string Brand,decimal Price,int Year);

public class CollectibleCarClass{
  public string Brand { get; set; }
  public decimal Price { get; set;} = 0;
  public int Year { get; set; }
  //add the immutable constructor etc..
}
  • Classes : two objects are equal if they refer to the same object in memory
  • Struct and Records : two objects are equal if they are of the same type and store the same values
//use named parameters
var car1 = new CollectibleCar(Brand: "Lexus", Price: 11300M, Year: 2015);

//use positional parameters
CollectibleCar car2 = new("Lexus", 11300M, 2015);
CollectibleCar car3 = new("Infinity", 11000M, 2014);


Console.WriteLine(car1 == car2); //true because values are the same
Console.WriteLine(car1 == car3); //false,because values are different

public record class CollectibleCar(string Brand, decimal Price, int Year);

Leave a Reply

Your email address will not be published. Required fields are marked *