null keyword - C# reference (2024)

The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null, except for nullable value types.

The following example demonstrates some behaviors of the null keyword:

class Program{ class MyClass { public void MyMethod() { } } static void Main() { // Set a breakpoint here to see that mc = null. // However, the compiler considers it "unassigned." // and generates a compiler error if you try to // use the variable. MyClass mc; // Now the variable can be used, but... mc = null; // ... a method call on a null object raises // a run-time NullReferenceException. // Uncomment the following line to see for yourself. // mc.MyMethod(); // Now mc has a value. mc = new MyClass(); // You can call its method. mc.MyMethod(); // Set mc to null again. The object it referenced // is no longer accessible and can now be garbage-collected. mc = null; // A null string is not the same as an empty string. string s = null; string t = String.Empty; // Logically the same as "" // Equals applied to any null object returns false. Console.WriteLine("t.Equals(s) is {0}", t.Equals(s)); // Equality operator also returns false when one // operand is null. Console.WriteLine("Empty string {0} null string", s == t ? "equals": "does not equal"); // Returns true. Console.WriteLine("null == null is {0}", null == null); // A value type cannot be null // int i = null; // Compiler error! // Use a nullable value type instead: int? i = null; // Keep the console window open in debug mode. }}

C# language specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also

null keyword - C# reference (2024)
Top Articles
IAsk AI: The Free AI Search Engine - Instant Factual Answers | Deepgram
Secure FTP - how to mitigate the risks and keep data secure
Spectrum Gdvr-2007
Patreon, reimagined — a better future for creators and fans
Inducement Small Bribe
Access-A-Ride – ACCESS NYC
How To Do A Springboard Attack In Wwe 2K22
Identifont Upload
Toyota Campers For Sale Craigslist
Comforting Nectar Bee Swarm
Lost Ark Thar Rapport Unlock
Zitobox 5000 Free Coins 2023
Acts 16 Nkjv
Is Csl Plasma Open On 4Th Of July
Flat Twist Near Me
Apnetv.con
Violent Night Showtimes Near Amc Fashion Valley 18
Mndot Road Closures
Jesus Revolution Showtimes Near Chisholm Trail 8
Strange World Showtimes Near Amc Braintree 10
Clairememory Scam
Select Truck Greensboro
Mlb Ballpark Pal
Miss America Voy Forum
Best Fare Finder Avanti
Uky Linkblue Login
Costco Gas Foster City
Divina Rapsing
Craigslist Southern Oregon Coast
Puretalkusa.com/Amac
Xfinity Outage Map Fredericksburg Va
Aliciabibs
Nottingham Forest News Now
Maths Open Ref
Mawal Gameroom Download
Lawrence Ks Police Scanner
Ucm Black Board
Quality Tire Denver City Texas
In Branch Chase Atm Near Me
The Wichita Beacon from Wichita, Kansas
Watchdocumentaries Gun Mayhem 2
Pensacola Cars Craigslist
Craigslist Tulsa Ok Farm And Garden
Www.craigslist.com Waco
If You're Getting Your Nails Done, You Absolutely Need to Tip—Here's How Much
Emily Browning Fansite
Wilson Tire And Auto Service Gambrills Photos
Sherwin Source Intranet
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
786 Area Code -Get a Local Phone Number For Miami, Florida
Leslie's Pool Supply Redding California
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6192

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.