File tree Expand file tree Collapse file tree 13 files changed +120
-139
lines changed
snippets/csharp/System/NullReferenceException/Overview Expand file tree Collapse file tree 13 files changed +120
-139
lines changed Original file line number Diff line number Diff line change 1- // <Snippet8>
2- using System ;
1+ using System ;
32
43public class Array1Example
54{
65 public static void Main ( )
76 {
7+ // <Snippet8>
88 string [ ] values = [ "one" , null , "two" ] ;
99 for ( int ctr = 0 ; ctr <= values . GetUpperBound ( 0 ) ; ctr ++ )
1010 Console . Write ( "{0}{1}" , values [ ctr ] . Trim ( ) ,
1111 ctr == values . GetUpperBound ( 0 ) ? "" : ", " ) ;
1212 Console . WriteLine ( ) ;
13+
14+ // The example displays the following output:
15+ // Unhandled Exception:
16+ // System.NullReferenceException: Object reference not set to an instance of an object.
17+ // </Snippet8>
1318 }
1419}
15- // The example displays the following output:
16- // Unhandled Exception:
17- // System.NullReferenceException: Object reference not set to an instance of an object.
18- // at Example.Main()
19- // </Snippet8>
Original file line number Diff line number Diff line change 1- // <Snippet9>
2- using System ;
1+ using System ;
32
43public class Array2Example
54{
65 public static void Main ( )
76 {
7+ // <Snippet9>
88 string [ ] values = [ "one" , null , "two" ] ;
99 for ( int ctr = 0 ; ctr <= values . GetUpperBound ( 0 ) ; ctr ++ )
1010 Console . Write ( "{0}{1}" ,
1111 values [ ctr ] != null ? values [ ctr ] . Trim ( ) : "" ,
1212 ctr == values . GetUpperBound ( 0 ) ? "" : ", " ) ;
1313 Console . WriteLine ( ) ;
14+
15+ // The example displays the following output:
16+ // one, , two
17+ // </Snippet9>
1418 }
1519}
16- // The example displays the following output:
17- // one, , two
18- // </Snippet9>
Original file line number Diff line number Diff line change 1- // <Snippet10>
2- using System ;
1+ using System ;
32
43public class Array3Example
54{
65 public static void Main ( )
76 {
7+ // <Snippet10>
88 int [ ] values = null ;
99 for ( int ctr = 0 ; ctr <= 9 ; ctr ++ )
1010 values [ ctr ] = ctr * 2 ;
1111
1212 foreach ( int value in values )
1313 Console . WriteLine ( value ) ;
14+
15+ // The example displays the following output:
16+ // Unhandled Exception:
17+ // System.NullReferenceException: Object reference not set to an instance of an object.
18+ // at Array3Example.Main()
19+ // </Snippet10>
1420 }
1521}
16- // The example displays the following output:
17- // Unhandled Exception:
18- // System.NullReferenceException: Object reference not set to an instance of an object.
19- // at Example.Main()
20- // </Snippet10>
Original file line number Diff line number Diff line change 1- // <Snippet11>
2- using System ;
1+ using System ;
32
43public class Array4Example
54{
65 public static void Main ( )
76 {
7+ // <Snippet11>
88 int [ ] values = new int [ 10 ] ;
99 for ( int ctr = 0 ; ctr <= 9 ; ctr ++ )
1010 values [ ctr ] = ctr * 2 ;
1111
1212 foreach ( int value in values )
1313 Console . WriteLine ( value ) ;
14+
15+ // The example displays the following output:
16+ // 0
17+ // 2
18+ // 4
19+ // 6
20+ // 8
21+ // 10
22+ // 12
23+ // 14
24+ // 16
25+ // 18
26+ // </Snippet11>
1427 }
1528}
16- // The example displays the following output:
17- // 0
18- // 2
19- // 4
20- // 6
21- // 8
22- // 10
23- // 12
24- // 14
25- // 16
26- // 18
27- // </Snippet11>
Original file line number Diff line number Diff line change @@ -63,9 +63,10 @@ public class Page
6363 public Uri URL ;
6464 public string Title ;
6565 }
66+
6667 // The example displays the following output:
6768 // Unhandled Exception:
6869 // System.NullReferenceException: Object reference not set to an instance of an object.
69- // at Example .Main()
70+ // at Chain1Example .Main()
7071 // </Snippet6>
7172}
Original file line number Diff line number Diff line change 1- // <Snippet7>
2- using System ;
1+ using System ;
32
43public class Chain2Example
54{
65 public static void Main ( )
76 {
7+ // <Snippet7>
88 var pages = new Pages ( ) ;
99 Page current = pages . CurrentPage ;
1010 if ( current != null )
@@ -16,11 +16,12 @@ public static void Main()
1616 {
1717 Console . WriteLine ( "There is no page information in the cache." ) ;
1818 }
19+
20+ // The example displays the following output:
21+ // There is no page information in the cache.
22+ // </Snippet7>
1923 }
2024}
21- // The example displays the following output:
22- // There is no page information in the cache.
23- // </Snippet7>
2425
2526public class Pages
2627{
Original file line number Diff line number Diff line change 1- // Do something
2- using System ;
3-
4- Console . WriteLine ( "Hello World" ) ;
1+ //NullReturnExample.NoCheckExample();
2+ NullReturnExample . ExampleWithNullCheck ( ) ;
Original file line number Diff line number Diff line change 22
33 <PropertyGroup >
44 <OutputType >Exe</OutputType >
5+ <Nullable >disable</Nullable >
56 <TargetFramework >net9.0</TargetFramework >
67 </PropertyGroup >
78
Original file line number Diff line number Diff line change @@ -13,8 +13,9 @@ public static void Main(string[] args)
1313 //names.Add("Major Major Major");
1414 }
1515}
16+
1617// Compilation displays a warning like the following:
17- // Example1.cs(10) : warning BC42104: Variable //names// is used before it
18+ // warning BC42104: Variable //names// is used before it
1819// has been assigned a value. A null reference exception could result
1920// at runtime.
2021//
@@ -23,5 +24,5 @@ public static void Main(string[] args)
2324// The example displays output like the following output:
2425// Unhandled Exception: System.NullReferenceException: Object reference
2526// not set to an instance of an object.
26- // at Example .Main()
27+ // at UseBeforeAssignExample .Main()
2728// </Snippet1>
Original file line number Diff line number Diff line change 11// <Snippet3>
2- using System ;
32using System . Collections . Generic ;
43
54public class NRE2Example
@@ -23,9 +22,10 @@ private static List<string> GetData()
2322 return null ;
2423 }
2524}
25+
2626// The example displays output like the following:
2727// Unhandled Exception: System.NullReferenceException: Object reference
2828// not set to an instance of an object.
29- // at Example .PopulateNames(List`1 names)
30- // at Example .Main()
29+ // at NRE2Example .PopulateNames(List`1 names)
30+ // at NRE2Example .Main()
3131// </Snippet3>
You can’t perform that action at this time.
0 commit comments