Stream. null

Source: Internet
Author: User
Tags reflector
Null Field of the system. Io. Stream class

We know that the system. Io. Stream class in the. NET Framework base class library has a static field null, as shown below:

 
Public Static ReadonlyStream NULL;

As described in msdn:

 
Stream without a backup storage area. Use null to redirect the output to a stream that does not occupy any operating system resources. When the stream method provided for writing is called on null, this call only returns without any data writing. Null also implements a read method that returns zero instead of reading data.
Related in microsft. NET framework 4 Code

Use. Net reflector to view the code of the Microsoft. NET Framework 4 class library. The Code is as follows:

 1   Namespace System. Io
2 {
3 [Serializable, comvisible ( True )]
4 Public Abstract Class Stream: financialbyrefobject, idisposable
5 {
6 [Serializable]
7 Private Sealed Class Nullstream: Stream
8 {
9 Internal Nullstream (){}
10 Protected Override Void Dispose ( Bool Disposing ){}
11 Public Override Void Flush (){}
12 Public Override Int Read ([In, out] Byte [] Buffer, Int Offset, Int Count ){ Return 0 ;}
13 Public Override Int Readbyte (){ Return - 1 ;}
14 Public Override Long Seek ( Long Offset, seekorigin origin ){ Return 0l ;}
15 Public Override Void Setlength ( Long Length ){}
16 Public Override Void Write (Byte [] Buffer, Int Offset, Int Count ){}
17 Public Override Void Writebyte ( Byte Value ){}
18 Public Override Bool Canread { Get { Return True ;}}
19 Public Override Bool Canseek { Get { Return True ;}}
20 Public Override Bool Canwrite { Get { Return True ;}}
21 Public Override Long Length { Get { Return 0l ;}}
22 Public Override Long Position { Get { Return 0l ;} Set {}}
23
24 [Hostprotection (securityaction. linkdemand, externalthreading = True )]
25 Public Override Iasyncresult beginread ( Byte [] Buffer, Int Offset, Int Count, asynccallback callback, Object State)
26 {
27 If (! This . Canread) _ error. readnotsupported ();
28 Return Base . Blockingbeginread (buffer, offset, Count, callback, State );
29 }
30
31 [Hostprotection (securityaction. linkdemand, externalthreading = True )]
32 Public Override Iasyncresult beginwrite ( Byte [] Buffer, Int Offset, Int Count, asynccallback callback, Object State)
33 {
34 If (! This . Canwrite) _ error. writenotsupported ();
35 Return Base . Blockingbeginwrite (buffer, offset, Count, callback, State );
36 }
37
38 Public Override Int Endread (iasyncresult asyncresult)
39 {
40 If (Asyncresult = Null )Throw New Argumentnullexception ( " Asyncresult " );
41 Return Stream. blockingendread (asyncresult );
42 }
43
44 Public Override Void Endwrite (iasyncresult asyncresult)
45 {
46 If (Asyncresult = Null ) Throw New Argumentnullexception ( " Asyncresult " );
47 Stream. blockingendwrite (asyncresult );
48 }
49 }
50
51 Public Static Readonly Stream NULL;
52 Static Stream () {null = New Nullstream ();}
53 }
54 }

It can be seen that although the null field of the stream class is declared as the steam type, it is actually initially initialized as the nullstream type. The nullstream class is an internal private class of the stream class and a derived class of the stream class.

In fact, the above SourceProgramThe four override methods of lines 24th to 48 in the file for asynchronous read/write are not required and can be omitted, because the corresponding virtual methods provided by the base class can already work correctly.

In addition, the IF Statements for rows 27th and 34th are completely unnecessary. You can see the statements for rows 18th and 20th.

Mono 2.10.9 related Code

Now let's take a look at the related code in Mono 2.10.9:

 1   //  Mono-2.10.9/MCS/class/corlib/system. IO/stream. CS  
2 Namespace System. Io
3 {
4 [Serializable]
5 [Comvisible (True )]
6 Public Abstract Class Stream: financialbyrefobject, idisposable
7 {
8 Public Static Readonly Stream null = New Nullstream ();
9 }
10
11 Class Nullstream: Stream
12 {
13 Public Override Bool Canread { Get { Return True ;}}
14 Public Override Bool Canseek {Get { Return True ;}}
15 Public Override Bool Canwrite { Get { Return True ;}}
16 Public Override Long Length { Get { Return 0 ;}}
17 Public Override Long Position { Get { Return 0 ;} Set {}}
18 Public Override Void Flush (){}
19 Public Override Int Read ( Byte [] Buffer, Int Offset, Int Count ){ Return 0 ;}
20 Public Override Int Readbyte (){ Return -1 ;}
21 Public Override Long Seek ( Long Offset, seekorigin origin ){ Return 0 ;}
22 Public Override Void Setlength ( Long Value ){}
23 Public Override Void Write ( Byte [] Buffer, Int Offset, Int Count ){}
24 Public Override Void Writebyte ( Byte Value ){}
25 }
26 }

It can be seen that in mono, The nullstream class is no longer an internal class of the stream class, but an internal class in the system. Io namespace. Of course, nullstream is still a derived class of the stream class. The internal implementation is basically similar to Microsoft's implementation, except for the four Virtual Methods no longer used for asynchronous read/write in override.

Whether it is Microsoft implementation or mono version, the nullstream class is invisible to the outside world and can only be accessed through the stream. null field, and only this unique instance.

Test procedure

Let's write a test program:

 1   Using System;
2 Using System. IO;
3
4 Static Class Tester
5 {
6 Static Void Main ()
7 {
8 Console. writeline ( " OS version: " + Environment. osversion );
9 Console. writeline ( " CLR version: " + Environment. version );
10 Console. writeline ( " Stream. null: " + Stream. null );
11 }
12 }

Compile and run in the opensuse 12.1 operating system:

 
Ben @ vbox :~ /Work>DMCS -- versionMono C # compiler version 2.10.9.0ben @ vbox :~ /Work>DMCS tester. CSBen @ vbox :~ /Work>Mono tester.exeOS Version: Unix 3.1.9.1clr version: 4.0.30319.1stream.null:System. Io. nullstream

It can be seen that in mono, The nullstream class is located in the system. Io namespace.

Compile and run Windows Vista:

E: \ work>CSC tester. CSMicrosoft (r) Visual C #2010 compiler 4.0.30319.1 copyright (c) Microsoft Corporation. All rights reserved. E: \ work>TesterOS Version: Microsoft Windows NT 6.0.6002 Service Pack 2clr version: 4.0.30319.239stream.null:System. Io. Stream + nullstream

The "+" indicates that the nullstream class is an internal class of the system. Io. Stream class.

Compile and run in winows 7:

 
C: \ work>C: \ windows \ Microsoft. NET \ framework \ v4.0.30319 \ CSC tester. CSMicrosoft (r) Visual C #2010 compiler version 4.0.30319.1copyright (c) Microsoft Corporation. All Rights Reserved. c: \ work>TesterOS Version: Microsoft Windows NT 6.1.7601 Service Pack 1clr version: 4.0.30319.239stream.null:System. Io. Stream + nullstream

Compile and run in Windows 8 consumer preview:

C: \ work>C: \ windows \ Microsoft. NET \ framework \ v4.0.30319 \ CSC tester. CSMicrosoft (r) Visual C # compiler version 4.0.30319.172.16for Microsoft (r). NET Framework 4.5Copyright (c) Microsoft Corporation. All rights reserved. C: \ work>TesterOS Version: Microsoft Windows NT 6.2.82501_clr version: 4.0.30319.172.16stream.null:System. Io. Stream + nullstream

Windows 8 has pre-installed. NET Framework 4.5 (100% compatible with. NET Framework 4), but is not pre-installed with. NET Framework 2.0, 3.0, and 3.5.

On the contrary, Windows 7 has pre-installed. NET Framework 2.0, 3.0, and 3.5, But not. NET Framework 4.

In fact, Windows 2000, Windows XP, and Windows Server 2003 are both Windows NT 5.x kernels, while Windows Vista, Windows 7, and Windows 8 are Windows NT 6.x kernels:

    • Windows 2000: Windows NT 5.0
    • Windows XP: Windows NT 5.1
    • Windows 2003: Windows NT 5.2
    • Windows Vista: Windows NT 6.0
    • Windows 7: Windows NT 6.1
    • Windows 8: Windows NT 6.2
References
    1. Msdn: stream. null field (system. Io)
    2. . Net Reflector
    3. Mono sources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.