Understanding Values and References in .NET

Creation PDF417 in .NET Understanding Values and References

8
Printing PDF 417 In VS .NET
Using Barcode maker for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Barcode Drawer In .NET Framework
Using Barcode maker for ASP.NET Control to generate, create bar code image in ASP.NET applications.
www.OnBarcode.com
static void Main() { int arg = 42; DoWork(arg); Console.WriteLine(arg); // writes 42, not 43 }
PDF 417 Creation In C#.NET
Using Barcode generation for .NET framework Control to generate, create PDF 417 image in Visual Studio .NET applications.
www.OnBarcode.com
PDF417 Encoder In .NET Framework
Using Barcode creation for .NET framework Control to generate, create PDF-417 2d barcode image in .NET applications.
www.OnBarcode.com
Understanding Values and References
Generate PDF-417 2d Barcode In Visual Basic .NET
Using Barcode generation for .NET framework Control to generate, create PDF417 image in VS .NET applications.
www.OnBarcode.com
Paint Bar Code In VS .NET
Using Barcode printer for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
In the preceding exercise, you saw that if the parameter to a method is a reference type, any changes made by using that parameter change the data referenced by the argument passed in. The key point is that, although the data that was referenced changed, the parameter itself did not it still references the same object. In other words, although it is possible to modify the object that the argument refers to through the parameter, it s not possible to modify the argument itself (for example, to set it to refer to a completely different object). Most of the time, this guarantee is very useful and can help to reduce the number of bugs in a program. Occasionally, however, you might want to write a method that actually needs to modify an argument. C# provides the ref and out keywords so that you can do this.
Make GS1 - 13 In VS .NET
Using Barcode generation for ASP.NET Control to generate, create UPC - 13 image in ASP.NET applications.
www.OnBarcode.com
EAN128 Generation In .NET
Using Barcode printer for ASP.NET Control to generate, create GTIN - 128 image in ASP.NET applications.
www.OnBarcode.com
Creating ref Parameters
Code 39 Maker In .NET
Using Barcode generator for ASP.NET Control to generate, create Code 3 of 9 image in ASP.NET applications.
www.OnBarcode.com
UPC Code Printer In VS .NET
Using Barcode creation for ASP.NET Control to generate, create UPC-A Supplement 2 image in ASP.NET applications.
www.OnBarcode.com
If you pre x a parameter with the ref keyword, the parameter becomes an alias for (or a reference to) the actual argument rather than a copy of the argument. When using a ref parameter, anything you do to the parameter you also do to the original argument because the parameter and the argument both reference the same object. When you pass an argument to a ref parameter, you must also pre x the argument with the ref keyword. This syntax provides a useful visual indication that the argument might change. Here s the preceding example again, this time modi ed to use the ref keyword:
Draw Data Matrix 2d Barcode In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.
www.OnBarcode.com
International Standard Book Number Printer In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create ISBN - 13 image in ASP.NET applications.
www.OnBarcode.com
static void DoWork(ref int param) // using ref { param++; } static void Main() { int arg = 42; DoWork(ref arg); Console.WriteLine(arg); }
Bar Code Maker In Java
Using Barcode maker for Android Control to generate, create barcode image in Android applications.
www.OnBarcode.com
Drawing PDF-417 2d Barcode In None
Using Barcode drawer for Excel Control to generate, create PDF 417 image in Office Excel applications.
www.OnBarcode.com
// using ref // writes 43
Barcode Generation In Visual C#
Using Barcode generator for Visual Studio .NET Control to generate, create barcode image in .NET framework applications.
www.OnBarcode.com
PDF-417 2d Barcode Recognizer In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
This time, you pass to the DoWork method a reference to the original argument rather than a copy of the original argument, so any changes the method makes by using this reference also change the original argument. That s why the value 43 is displayed on the console. The rule that you must assign a value to a variable before you can use the variable still applies to ref arguments. For example, in the following example, arg is not initialized, so this
Bar Code Recognizer In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Create Barcode In Java
Using Barcode creation for Android Control to generate, create bar code image in Android applications.
www.OnBarcode.com
Part II
UCC - 12 Maker In None
Using Barcode encoder for Font Control to generate, create GS1 128 image in Font applications.
www.OnBarcode.com
Painting UCC-128 In None
Using Barcode encoder for Word Control to generate, create EAN 128 image in Office Word applications.
www.OnBarcode.com
Understanding the C# Language
code will not compile. This failure is because param++ inside DoWork is really arg++, and arg++ is allowed only if arg has a de ned value:
static void DoWork(ref int param) { param++; } static void Main() { int arg; // not initialized DoWork(ref arg); Console.WriteLine(arg); }
Creating out Parameters
The compiler checks whether a ref parameter has been assigned a value before calling the method. However, there may be times when you want the method to initialize the parameter. With the out keyword, you can do this. The out keyword is very similar to the ref keyword. You can pre x a parameter with the out keyword so that the parameter becomes an alias for the argument. As when using ref, anything you do to the parameter, you also do to the original argument. When you pass an argument to an out parameter, you must also pre x the argument with the out keyword. The keyword out is short for output. When you pass an out parameter to a method, the method must assign a value to it. The following example does not compile because DoWork does not assign a value to param:
static void DoWork(out int param) { // Do nothing }
However, the following example does compile because DoWork assigns a value to param.
static void DoWork(out int param) { param = 42; }
Because an out parameter must be assigned a value by the method, you re allowed to call the method without initializing its argument. For example, the following code calls DoWork to initialize the variable arg, which is then displayed on the console:
static void DoWork(out int param) { param = 42; }
Copyright © OnBarcode.com . All rights reserved.