Null Reference Basics
Part of the Logic & Flow section of Coddy's C# journey — lesson 31 of 66.
In C#, a null reference represents the absence of an object. Understanding how to work with null values is essential for writing robust code.
Declare a reference type variable (like string) without assigning a value:
string name = null;The variable 'name' now holds a null reference, which means it doesn't point to any object in memory.
Trying to use a null reference without checking can cause runtime errors:
string name = null;
// This will throw a NullReferenceException
int length = name.Length;You can check if a reference is null before using it:
string name = null;
if (name != null)
{
// Safe to use name here
int length = name.Length;
}You can also assign a non-null value to the variable:
string name = null;
name = "John";
// Now it's safe to use name
int length = name.Length; // length will be 4Challenge
EasyCreate a method called ProcessName that takes a string parameter called name. The method should:
- Check if the name is null
- If the name is null, print: "Name is null"
- If the name is not null, print: "Name length is X" (where X is the length of the name)
Cheat sheet
A null reference represents the absence of an object in C#.
Declare a reference type variable with null:
string name = null;Using null references without checking causes runtime errors:
string name = null;
// This will throw a NullReferenceException
int length = name.Length;Check for null before using:
string name = null;
if (name != null)
{
// Safe to use name here
int length = name.Length;
}Assign a non-null value to make it safe to use:
string name = null;
name = "John";
int length = name.Length; // length will be 4Try it yourself
using System;
class Program
{
public static void ProcessName(string name)
{
// Write your code here
}
static void Main(string[] args)
{
string input = Console.ReadLine();
// Convert "null" string to actual null
string name = input.ToLower() == "null" ? null : input;
ProcessName(name);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety