concept reference in category c#

appears as: reference, references, reference, The reference, references, The references
C# in Depth, Fourth Edition

This is an excerpt from Manning's book C# in Depth, Fourth Edition.

The plans for C# 8 include nullable reference types, which will allow even more communication. Up to this point, nothing in the language lets you express whether a reference (either as a return value, a parameter, or just a local variable) might be null. This leads to error-prone code if you’re not careful and boilerplate validation code if you are careful, neither of which is ideal. C# 8 will expect that anything not explicitly nullable is intended not to be nullable. For example, consider a method declaration like this:

The ref keyword and object references are different concepts. Similarities certainly exist, but you need to distinguish between them. Passing an object reference by value isn’t the same thing as passing a variable by reference, for example. In this section, I’ve emphasized the difference by using object reference instead of just reference.

Listing 15.5. Checking a reference with a local variable
Address? address = customer.Address;      #1
if (address != null)                      #2
{                                         #2
    Console.WriteLine(address.Country);   #2
}                                         #2
else
{
    Console.WriteLine("(Address unknown)");
}
C# in Depth

This is an excerpt from Manning's book C# in Depth.

  • References are like URLs—they’re small pieces of data that let you access the real information.
  • Myth #3: Objects are passed by reference in C# by default

    This is probably the most widely propagated myth. Again, the people who make this claim often (though not always) know how C# actually behaves, but they don’t know what “pass by reference” really means. Unfortunately, this is confusing for people who do know what it means.

    The formal definition of pass by reference is relatively complicated, involving l-values and similar computer-science terminology, but the important thing is that if you pass a variable by reference, the method you’re calling can change the value of the caller’s variable by changing its parameter value. Now, remember that the value of a reference type variable is the reference, not the object itself. You can change the contents of the object that a parameter refers to without the parameter itself being passed by reference. For instance, the following method changes the contents of the StringBuilder object in question, but the caller’s expression will still refer to the same object as before:

    Dependency Injection in .NET

    This is an excerpt from Manning's book Dependency Injection in .NET.

  • The dependency contains nondeterministic behavior. This is particularly important in unit tests, because all tests should be deterministic. Typical sources of nondeterminism are random numbers and algorithms that depend on the current date or time. Note that common sources of nondeterminism, such as System.Random, System.Security.Cryptography.RandomNumberGenerator, or System.DateTime.Now are defined in mscorlib, so you can’t avoid having a reference to the assembly in which they’re defined. Nevertheless, you should treat them as VOLATILE DEPENDENCIES, because they tend to destroy TESTABILITY.
  • This tightly couples the ProductService class to the Data Access library. There’s no reasonable way we can intercept this piece of code and replace it with something else. The reference to the Data Access library is hard-coded into the ProductService class.

    Armed with Jens’ advice, Mary creates a new C# library project and adds a class called ProductService, shown in the following listing. To make the ProductService class compile, she must add a reference to her Data Access library, because the CommerceObjectContext class is defined there.

    Figure 3.8. Only the COMPOSITION ROOT contained in the application root should have a reference to a DI CONTAINER. All other modules in the application should rely entirely on DI patterns and have no reference to the container.
    ASP.NET AJAX in Action

    This is an excerpt from Manning's book ASP.NET AJAX in Action.

    The getLocation method takes a reference to a DOM element and returns an object with two attributes, x and y, which store the left and top coordinates of the element relative to the upper-left corner of the parent frame:

    Figure 8.3 shows that the Sys._Application class, the single instance of which is the Application object, is a container. One of the goals of the Application object is to host and keep track of the client components instantiated in the page. As you’ll discover in the following sections, hosting client components in a container has various advantages. For example, you can retrieve references to client components through the container, instead of storing them in global JavaScript variables. Another benefit of hosting components in the Application object is that they’re automatically disposed by the container when the page is unloaded by the browser. This means you don’t have to manually call the dispose method on each component instance. Client components become children of the Application object during their creation process, which is illustrated in section 8.2.1.

    Figure 8.8. You can use the $find method to retrieve a reference to an instance of a client behavior by knowing the ID of the associated DOM element and the value of the behavior’s name property.

    Creating a TrivialComponent instance with the new operator is just the first step. The next (optional) thing to do is configure the instance by setting client properties. For example, the id property lets you retrieve a reference to the new instance using the $find method.

    Listing 13.6. Validating references in the initialization code
    Samples.SomeControl.prototype = { initialize : function() { Samples.SomeControl.callBaseMethod(this, 'initialize'); var e = Function._validateParams( [this._childElement], [ {name:"childElement", mayBeNull:false, optional:false, isDomElement:true}, ]); if(e) throw e; // Initialization code continues here. } }