string
is an alias in C# for System.String
.
So technically, there is no difference. It's like int
vs. System.Int32
.
As far as guidelines, it's generally recommended to use string
any time you're referring to an object.
e.g.
string place = "world";
Likewise, I think it's generally recommended to use String
if you need to refer specifically to the class.
e.g.
string greet = String.Format("Hello {0}!", place);
This is the style that Microsoft tends to use in their examples.
It appears that the guidance in this area may have changed, as StyleCop now enforces the use of the C# specific aliases.
Whereas one approach is to implement the ICloneable
interface (described here, so I won't regurgitate), here's a nice deep clone object copier I found on The Code Project a while ago and incorporated it into our code.
As mentioned elsewhere, it requires your objects to be serializable.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>
public static class ObjectCopier
{
/// <summary>
/// Perform a deep copy of the object via serialization.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>A deep copy of the object.</returns>
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", nameof(source));
}
// Don't serialize a null object, simply return the default for that object
if (ReferenceEquals(source, null)) return default;
using var Stream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
The idea is that it serializes your object and then deserializes it into a fresh object. The benefit is that you don't have to concern yourself about cloning everything when an object gets too complex.
In case of you prefer to use the new extension methods of C# 3.0, change the method to have the following signature:
public static T Clone<T>(this T source)
{
// ...
}
Now the method call simply becomes objectBeingCloned.Clone();
.
EDIT (January 10 2015) Thought I'd revisit this, to mention I recently started using (Newtonsoft) Json to do this, it should be lighter, and avoids the overhead of [Serializable] tags. (NB @atconway has pointed out in the comments that private members are not cloned using the JSON method)
/// <summary>
/// Perform a deep Copy of the object, using Json as a serialization method. NOTE: Private members are not cloned using this method.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T CloneJson<T>(this T source)
{
// Don't serialize a null object, simply return the default for that object
if (ReferenceEquals(source, null)) return default;
// initialize inner objects individually
// for example in default constructor some list property initialized with some values,
// but in 'source' these items are cleaned -
// without ObjectCreationHandling.Replace default constructor values will be added to result
var deserializeSettings = new JsonSerializerSettings {ObjectCreationHandling = ObjectCreationHandling.Replace};
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), deserializeSettings);
}
Best Answer
What is the cause?
Bottom Line
You are trying to use something that is
null
(orNothing
in VB.NET). This means you either set it tonull
, or you never set it to anything at all.Like anything else,
null
gets passed around. If it isnull
in method "A", it could be that method "B" passed anull
to method "A".null
can have different meanings:NullReferenceException
.null
intentionally to indicate there is no meaningful value available. Note that C# has the concept of nullable datatypes for variables (like database tables can have nullable fields) - you can assignnull
to them to indicate there is no value stored in it, for exampleint? a = null;
(which is a shortcut forNullable<int> a = null;
) where the question mark indicates it is allowed to storenull
in variablea
. You can check that either withif (a.HasValue) {...}
or withif (a==null) {...}
. Nullable variables, likea
this example, allow to access the value viaa.Value
explicitly, or just as normal viaa
.Note that accessing it via
a.Value
throws anInvalidOperationException
instead of aNullReferenceException
ifa
isnull
- you should do the check beforehand, i.e. if you have another non-nullable variableint b;
then you should do assignments likeif (a.HasValue) { b = a.Value; }
or shorterif (a != null) { b = a; }
.The rest of this article goes into more detail and shows mistakes that many programmers often make which can lead to a
NullReferenceException
.More Specifically
The
runtime
throwing aNullReferenceException
always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized).This means the reference is
null
, and you cannot access members (such as methods) through anull
reference. The simplest case:This will throw a
NullReferenceException
at the second line because you can't call the instance methodToUpper()
on astring
reference pointing tonull
.Debugging
How do you find the source of a
NullReferenceException
? Apart from looking at the exception itself, which will be thrown exactly at the location where it occurs, the general rules of debugging in Visual Studio apply: place strategic breakpoints and inspect your variables, either by hovering the mouse over their names, opening a (Quick)Watch window or using the various debugging panels like Locals and Autos.If you want to find out where the reference is or isn't set, right-click its name and select "Find All References". You can then place a breakpoint at every found location and run your program with the debugger attached. Every time the debugger breaks on such a breakpoint, you need to determine whether you expect the reference to be non-null, inspect the variable, and verify that it points to an instance when you expect it to.
By following the program flow this way, you can find the location where the instance should not be null, and why it isn't properly set.
Examples
Some common scenarios where the exception can be thrown:
Generic
If ref1 or ref2 or ref3 is null, then you'll get a
NullReferenceException
. If you want to solve the problem, then find out which one is null by rewriting the expression to its simpler equivalent:Specifically, in
HttpContext.Current.User.Identity.Name
, theHttpContext.Current
could be null, or theUser
property could be null, or theIdentity
property could be null.Indirect
If you want to avoid the child (Person) null reference, you could initialize it in the parent (Book) object's constructor.
Nested Object Initializers
The same applies to nested object initializers:
This translates to:
While the
new
keyword is used, it only creates a new instance ofBook
, but not a new instance ofPerson
, so theAuthor
the property is stillnull
.Nested Collection Initializers
The nested collection
Initializers
behave the same:This translates to:
The
new Person
only creates an instance ofPerson
, but theBooks
collection is stillnull
. The collectionInitializer
syntax does not create a collection forp1.Books
, it only translates to thep1.Books.Add(...)
statements.Array
Array Elements
Jagged Arrays
Collection/List/Dictionary
Range Variable (Indirect/Deferred)
Events (C#)
(Note: The VB.NET compiler inserts null checks for event usage, so it's not necessary to check events for
Nothing
in VB.NET.)Bad Naming Conventions:
If you named fields differently from locals, you might have realized that you never initialized the field.
This can be solved by following the convention to prefix fields with an underscore:
ASP.NET Page Life cycle:
ASP.NET Session Values
ASP.NET MVC empty view models
If the exception occurs when referencing a property of
@Model
in anASP.NET MVC View
, you need to understand that theModel
gets set in your action method, when youreturn
a view. When you return an empty model (or model property) from your controller, the exception occurs when the views access it:WPF Control Creation Order and Events
WPF
controls are created during the call toInitializeComponent
in the order they appear in the visual tree. ANullReferenceException
will be raised in the case of early-created controls with event handlers, etc., that fire duringInitializeComponent
which reference late-created controls.For example:
Here
comboBox1
is created beforelabel1
. IfcomboBox1_SelectionChanged
attempts to reference `label1, it will not yet have been created.Changing the order of the declarations in the
XAML
(i.e., listinglabel1
beforecomboBox1
, ignoring issues of design philosophy) would at least resolve theNullReferenceException
here.Cast with
as
This doesn't throw an
InvalidCastException
but returns anull
when the cast fails (and whensomeObject
is itself null). So be aware of that.LINQ
FirstOrDefault()
andSingleOrDefault()
The plain versions
First()
andSingle()
throw exceptions when there is nothing. The "OrDefault" versions returnnull
in that case. So be aware of that.foreach
foreach
throws when you try to iterate on anull
collection. Usually caused by unexpectednull
result from methods that return collections.More realistic example - select nodes from XML document. Will throw if nodes are not found but initial debugging shows that all properties valid:
Ways to Avoid
Explicitly check for
null
and ignorenull
values.If you expect the reference sometimes to be
null
, you can check for it beingnull
before accessing instance members:Explicitly check for
null
and provide a default value.Methods you call expecting an instance can return
null
, for example when the object being sought cannot be found. You can choose to return a default value when this is the case:Explicitly check for
null
from method calls and throw a custom exception.You can also throw a custom exception, only to catch it in the calling code:
Use
Debug.Assert
if a value should never benull
, to catch the problem earlier than the exception occurs.When you know during development that a method could, but never should return
null
, you can useDebug.Assert()
to break as soon as possible when it does occur:Though this check will not end up in your release build, causing it to throw the
NullReferenceException
again whenbook == null
at runtime in release mode.Use
GetValueOrDefault()
fornullable
value types to provide a default value when they arenull
.Use the null coalescing operator:
??
[C#] orIf()
[VB].The shorthand to providing a default value when a
null
is encountered:Use the null condition operator:
?.
or?[x]
for arrays (available in C# 6 and VB.NET 14):This is also sometimes called the safe navigation or Elvis (after its shape) operator. If the expression on the left side of the operator is null, then the right side will not be evaluated, and null is returned instead. That means cases like this:
If the person does not have a title, this will throw an exception because it is trying to call
ToUpper
on a property with a null value.In
C# 5
and below, this can be guarded with:Now the title variable will be null instead of throwing an exception. C# 6 introduces a shorter syntax for this:
This will result in the title variable being
null
, and the call toToUpper
is not made ifperson.Title
isnull
.Of course, you still have to check
title
fornull
or use the null condition operator together with the null coalescing operator (??
) to supply a default value:Likewise, for arrays you can use
?[i]
as follows:This will do the following: If
myIntArray
isnull
, the expression returnsnull
and you can safely check it. If it contains an array, it will do the same as:elem = myIntArray[i];
and returns the ith element.Use null context (available in C# 8):
Introduced in
C# 8
, null contexts and nullable reference types perform static analysis on variables and provide a compiler warning if a value can be potentiallynull
or have been set tonull
. The nullable reference types allow types to be explicitly allowed to benull
.The nullable annotation context and nullable warning context can be set for a project using the
Nullable
element in yourcsproj
file. This element configures how the compiler interprets the nullability of types and what warnings are generated. Valid settings are:enable
: The nullable annotation context is enabled. The nullable warning context is enabled. Variables of a reference type, string, for example, are non-nullable. All nullability warnings are enabled.disable
: The nullable annotation context is disabled. The nullable warning context is disabled. Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.safeonly
: The nullable annotation context is enabled. The nullable warning context is safeonly. Variables of a reference type are non-nullable. All safety nullability warnings are enabled.warnings
: The nullable annotation context is disabled. The nullable warning context is enabled. Variables of a reference type are oblivious. All nullability warnings are enabled.safeonlywarnings
: The nullable annotation context is disabled. The nullable warning context is safeonly. Variables of a reference type are oblivious. All safety nullability warnings are enabled.A nullable reference type is noted using the same syntax as nullable value types: a
?
is appended to the type of the variable.Special techniques for debugging and fixing null derefs in iterators
C#
supports "iterator blocks" (called "generators" in some other popular languages).NullReferenceException
can be particularly tricky to debug in iterator blocks because of deferred execution:If
whatever
results innull
thenMakeFrob
will throw. Now, you might think that the right thing to do is this:Why is this wrong? Because the iterator block does not actually run until the
foreach
! The call toGetFrobs
simply returns an object which when iterated will run the iterator block.By writing a
null
check like this you prevent theNullReferenceException
, but you move theNullArgumentException
to the point of the iteration, not to the point of the call, and that is very confusing to debug.The correct fix is:
That is, make a private helper method that has the iterator block logic and a public surface method that does the
null
check and returns the iterator. Now whenGetFrobs
is called, thenull
check happens immediately, and thenGetFrobsForReal
executes when the sequence is iterated.If you examine the reference source for
LINQ
to Objects you will see that this technique is used throughout. It is slightly more clunky to write, but it makes debugging nullity errors much easier. Optimize your code for the convenience of the caller, not the convenience of the author.A note on null dereferences in unsafe code
C#
has an "unsafe" mode which is, as the name implies, extremely dangerous because the normal safety mechanisms which provide memory safety and type safety are not enforced. You should not be writing unsafe code unless you have a thorough and deep understanding of how memory works.In unsafe mode, you should be aware of two important facts:
To understand why that is, it helps to understand how .NET produces
NullReferenceException
in the first place. (These details apply to .NET running on Windows; other operating systems use similar mechanisms.)Memory is virtualized in
Windows
; each process gets a virtual memory space of many "pages" of memory that are tracked by the operating system. Each page of memory has flags set on it that determine how it may be used: read from, written to, executed, and so on. The lowest page is marked as "produce an error if ever used in any way".Both a null pointer and a null reference in
C#
are internally represented as the number zero, and so any attempt to dereference it into its corresponding memory storage causes the operating system to produce an error. The .NET runtime then detects this error and turns it into theNullReferenceException
.That's why dereferencing both a null pointer and a null reference produces the same exception.
What about the second point? Dereferencing any invalid pointer that falls in the lowest page of virtual memory causes the same operating system error, and thereby the same exception.
Why does this make sense? Well, suppose we have a struct containing two ints, and an unmanaged pointer equal to null. If we attempt to dereference the second int in the struct, the
CLR
will not attempt to access the storage at location zero; it will access the storage at location four. But logically this is a null dereference because we are getting to that address via the null.If you are working with unsafe code and you get a
NullReferenceException
, just be aware that the offending pointer need not be null. It can be any location in the lowest page, and this exception will be produced.