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 ;
3
2
4
3
public class Array1Example
5
4
{
6
5
public static void Main ( )
7
6
{
7
+ // <Snippet8>
8
8
string [ ] values = [ "one" , null , "two" ] ;
9
9
for ( int ctr = 0 ; ctr <= values . GetUpperBound ( 0 ) ; ctr ++ )
10
10
Console . Write ( "{0}{1}" , values [ ctr ] . Trim ( ) ,
11
11
ctr == values . GetUpperBound ( 0 ) ? "" : ", " ) ;
12
12
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>
13
18
}
14
19
}
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 ;
3
2
4
3
public class Array2Example
5
4
{
6
5
public static void Main ( )
7
6
{
7
+ // <Snippet9>
8
8
string [ ] values = [ "one" , null , "two" ] ;
9
9
for ( int ctr = 0 ; ctr <= values . GetUpperBound ( 0 ) ; ctr ++ )
10
10
Console . Write ( "{0}{1}" ,
11
11
values [ ctr ] != null ? values [ ctr ] . Trim ( ) : "" ,
12
12
ctr == values . GetUpperBound ( 0 ) ? "" : ", " ) ;
13
13
Console . WriteLine ( ) ;
14
+
15
+ // The example displays the following output:
16
+ // one, , two
17
+ // </Snippet9>
14
18
}
15
19
}
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 ;
3
2
4
3
public class Array3Example
5
4
{
6
5
public static void Main ( )
7
6
{
7
+ // <Snippet10>
8
8
int [ ] values = null ;
9
9
for ( int ctr = 0 ; ctr <= 9 ; ctr ++ )
10
10
values [ ctr ] = ctr * 2 ;
11
11
12
12
foreach ( int value in values )
13
13
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>
14
20
}
15
21
}
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 ;
3
2
4
3
public class Array4Example
5
4
{
6
5
public static void Main ( )
7
6
{
7
+ // <Snippet11>
8
8
int [ ] values = new int [ 10 ] ;
9
9
for ( int ctr = 0 ; ctr <= 9 ; ctr ++ )
10
10
values [ ctr ] = ctr * 2 ;
11
11
12
12
foreach ( int value in values )
13
13
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>
14
27
}
15
28
}
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
63
63
public Uri URL ;
64
64
public string Title ;
65
65
}
66
+
66
67
// The example displays the following output:
67
68
// Unhandled Exception:
68
69
// System.NullReferenceException: Object reference not set to an instance of an object.
69
- // at Example .Main()
70
+ // at Chain1Example .Main()
70
71
// </Snippet6>
71
72
}
Original file line number Diff line number Diff line change 1
- // <Snippet7>
2
- using System ;
1
+ using System ;
3
2
4
3
public class Chain2Example
5
4
{
6
5
public static void Main ( )
7
6
{
7
+ // <Snippet7>
8
8
var pages = new Pages ( ) ;
9
9
Page current = pages . CurrentPage ;
10
10
if ( current != null )
@@ -16,11 +16,12 @@ public static void Main()
16
16
{
17
17
Console . WriteLine ( "There is no page information in the cache." ) ;
18
18
}
19
+
20
+ // The example displays the following output:
21
+ // There is no page information in the cache.
22
+ // </Snippet7>
19
23
}
20
24
}
21
- // The example displays the following output:
22
- // There is no page information in the cache.
23
- // </Snippet7>
24
25
25
26
public class Pages
26
27
{
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 2
2
3
3
<PropertyGroup >
4
4
<OutputType >Exe</OutputType >
5
+ <Nullable >disable</Nullable >
5
6
<TargetFramework >net9.0</TargetFramework >
6
7
</PropertyGroup >
7
8
Original file line number Diff line number Diff line change @@ -13,8 +13,9 @@ public static void Main(string[] args)
13
13
//names.Add("Major Major Major");
14
14
}
15
15
}
16
+
16
17
// 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
18
19
// has been assigned a value. A null reference exception could result
19
20
// at runtime.
20
21
//
@@ -23,5 +24,5 @@ public static void Main(string[] args)
23
24
// The example displays output like the following output:
24
25
// Unhandled Exception: System.NullReferenceException: Object reference
25
26
// not set to an instance of an object.
26
- // at Example .Main()
27
+ // at UseBeforeAssignExample .Main()
27
28
// </Snippet1>
Original file line number Diff line number Diff line change 1
1
// <Snippet3>
2
- using System ;
3
2
using System . Collections . Generic ;
4
3
5
4
public class NRE2Example
@@ -23,9 +22,10 @@ private static List<string> GetData()
23
22
return null ;
24
23
}
25
24
}
25
+
26
26
// The example displays output like the following:
27
27
// Unhandled Exception: System.NullReferenceException: Object reference
28
28
// 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()
31
31
// </Snippet3>
You can’t perform that action at this time.
0 commit comments