Understanding Values and References in VS .NET

Drawer PDF-417 2d barcode in VS .NET Understanding Values and References

8
Make PDF417 In VS .NET
Using Barcode creator for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Barcode Creation In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create bar code image in ASP.NET applications.
www.OnBarcode.com
Understanding Values and References
Print PDF-417 2d Barcode In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
Painting PDF-417 2d Barcode In VS .NET
Using Barcode generator for .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
In C#, you can assign the null value to any reference variable. The null value simply means that the variable does not refer to an object in memory. You can use it like this:
Generate PDF417 In Visual Basic .NET
Using Barcode generator for .NET Control to generate, create PDF 417 image in .NET framework applications.
www.OnBarcode.com
Painting PDF-417 2d Barcode In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Circle c = new Circle(42); Circle copy = null; ... if (copy == null) copy = c; // Initialized
QR Code Encoder In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create QR-Code image in ASP.NET applications.
www.OnBarcode.com
Painting Barcode In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
// copy and c refer to the same object
Making Universal Product Code Version A In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create UPCA image in ASP.NET applications.
www.OnBarcode.com
Code 128 Code Set B Generation In .NET
Using Barcode creator for ASP.NET Control to generate, create Code 128 Code Set A image in ASP.NET applications.
www.OnBarcode.com
Using Nullable Types
Encode Bar Code In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
UPC-E Generator In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create UCC - 12 image in ASP.NET applications.
www.OnBarcode.com
The null value is useful for initializing reference types, but null is itself a reference, and you cannot assign it to a value type. The following statement is therefore illegal in C#:
Generate Code 128A In None
Using Barcode printer for Font Control to generate, create ANSI/AIM Code 128 image in Font applications.
www.OnBarcode.com
Make UPC Symbol In Objective-C
Using Barcode encoder for iPad Control to generate, create Universal Product Code version A image in iPad applications.
www.OnBarcode.com
int i = null; // illegal
Create UPC A In Java
Using Barcode maker for BIRT Control to generate, create UPC-A Supplement 2 image in BIRT reports applications.
www.OnBarcode.com
Barcode Printer In .NET Framework
Using Barcode printer for Visual Studio .NET Control to generate, create bar code image in .NET framework applications.
www.OnBarcode.com
However, C# de nes a modi er that you can use to declare that a variable is a nullable value type. A nullable value type behaves in a similar manner to the original value type, but you can assign the null value to it. You use the question mark ( )to indicate that a value type is nullable, like this:
Creating EAN13 In Objective-C
Using Barcode drawer for iPad Control to generate, create EAN 13 image in iPad applications.
www.OnBarcode.com
Bar Code Encoder In None
Using Barcode maker for Software Control to generate, create barcode image in Software applications.
www.OnBarcode.com
int i = null; // legal
Barcode Scanner In .NET Framework
Using Barcode Control SDK for ASP.NET Control to generate, create, read, scan barcode image in ASP.NET applications.
www.OnBarcode.com
Print EAN-13 Supplement 5 In None
Using Barcode generation for Font Control to generate, create EAN13 image in Font applications.
www.OnBarcode.com
You can ascertain whether a nullable variable contains null by testing it in the same way as a reference type:
if (i == null) ...
You can assign an expression of the appropriate value type directly to a nullable variable. The following examples are all legal:
int i = null; int j = 99; i = 100; // Copy a value type constant to a nullable type i = j; // Copy a value type variable to a nullable type
You should note that the converse is not true. You cannot assign a nullable value to an ordinary value type variable, so given the de nitions of variables i and j from the preceding example, the following statement is not allowed:
j = i; // Illegal
This also means that you cannot use a nullable variable as a parameter to a method that expects an ordinary value type. If you recall, the Pass.Value method from the preceding exercise expects an ordinary int parameter, so the following method call will not compile:
int i = 99; Pass.Value(i); // Compiler error
Part II
Understanding the C# Language
Understanding the Properties of Nullable Types
Nullable types expose a pair of properties that you can use and that you have already met in 6, Managing Errors and Exceptions. The HasValue property indicates whether a nullable type contains a value or is null, and you can retrieve the value of a non-null nullable type by reading the Value property, like this:
int i = null; ... if (!i.HasValue) i = 99; else Console.WriteLine(i.Value);
Recall from 4, Using Decision Statements, that the NOT operator (!) negates a Boolean value. This code fragment tests the nullable variable i, and if it does not have a value (it is null), it assigns it the value 99; otherwise, it displays the value of the variable. In this example, using the HasValue property does not provide any bene t over testing for a null value directly. Additionally, reading the Value property is a long-winded way of reading the contents of the variable. However, these apparent shortcomings are caused by the fact that int is a very simple nullable type. You can create more complex value types and use them to declare nullable variables where the advantages of using the HasValue and Value properties become more apparent. You will see some examples in 9, Creating Value Types with Enumerations and Structures. Note The Value property of a nullable type is read-only. You can use this property to read the
value of a variable but not to modify it. To update a nullable variable, use an ordinary assignment statement.
Using ref and out Parameters
Ordinarily, when you pass an argument to a method, the corresponding parameter is initialized with a copy of the argument. This is true regardless of whether the parameter is a value type (such as an int), a nullable type (such as int ), or a reference type (such as a WrappedInt). This arrangement means it s impossible for any change to the parameter to affect the value of the argument passed in. For example, in the following code, the value output to the console is 42 and not 43. The DoWork method increments a copy of the argument (arg) and not the original argument:
static void DoWork(int param) { param++; }
Copyright © OnBarcode.com . All rights reserved.