Accessing and Using Classes

15 / 100 SEO Score

The classes don’t do anything until we access them and use them. To access classes we need to considers using references and using directives and to uses classes, we need to consider using object initialization and null checking.

In the article, I will start with setting references and the purpose of using directives. Including the new static feature comes with C# 6 and examine how to use static and non-static class. Then compare the new three-technique on Object Initialization. Also, cover how to instantiate related objects and include a technique called lazy loading. Lastly, look at null checking which includes new C#6 new features.

What is the difference between an object and a class?

  • A Class is a template that specifies the data and operators.
  • An Object is an instance of that class created at runtime using keyword.

What is Lazy Loading and when would you use it?

  • Instantiating related objects when they are needed and not before.
  • This often involves creating the instant in the property getter for the related object

References and Using

Reference identifies the component containing the class. You can use a Using directive to use the class without the fully qualified namespacing.

Best practice References and Using

Here are the thing you need to take care of when defining references.

  • References must be one way – If you have Product class , want to use the components of the common library class which is in the different components. You will need to reference the common library class in the product class. Then if you to reference back product class in common library class, visual studio will not allow do it. You get an error secular dependency
  • Avoid excessive use of the static directive.

Using a Class

Object

  • Represent one specific thing.
  • Defines one thing created from that template
  • Created at runtime with the new keyword

Class

  • Represent things of the same type.
  • Defines the template specifying the data and processing associated with all things of that type
  • Created at development time with code

Object Initialization

The following are the three ways to initialize an object including Setting properties, Parameterized constructor and object initializers

Setting properties

var currentProduct = new Product();
            currentProduct.ProductName = "Mango";
            currentProduct.ProductId = 1;
            currentProduct.Description = "1 kg of mango pack";

Setting properties – when populating from database values

Parameterized constructor

var currentProduct = new Product(1, "Mango", "11 kg of mango pack");

Parameterized constructor – When setting the basic set of properties.

Object initializers

     var currentProduct = new Product{
                ProductId= 1,
                ProductName= "Mango",
                Description= "1 kg of mango pack"
            };

Object initializers – When readability is important and initializing a subset or superset of properties.

Instantiating Related Objects

  • It can be needed by one Method, Always or sometimes
 public string SayHello()
        {
            var vendor = new Vendor();
            var VendorGreeting 
 = vendor.SayHello();
....
}

One Method – Initialize in the method that needs it.

  public Vendor ProductVendor
        {

            get {
      return productVendor;
            }
            set { productVendor = value; }
        }

 public Product()
        {
            Console.WriteLine("Product instance created");
            //this.ProductVendor = new Vendor();
        }

Always – Define a Property and Initialize in the constructor.

public Vendor ProductVendor
        {

            get {
                if (productVendor == null)
                {
                    productVendor = new Vendor();
                }
                return productVendor;
            }
            set { productVendor = value; }
        }

Sometimes – Define a property and Initialized in the property getter “Lazy Loading”

Null Checking

Before the new null checking classic from C# 6, It was time-consuming to check null references. See the below example. They called “Null-Conditional Operator or Elvis operator”

if (currentProduct !=  null  && currentProduct.ProductVendor = != null)
{ 
   var Company  =  current.Providor.CompanyName;
}

Below is the new way to check null

var companyName =  currentProduct?.ProductVendor?.CompanyName;
  • ?. the null-conditional operator called the Elvis operator
  • If the variable on the left side is null, the expression is null.
  • If the variable on the left side is not null, then we continue with the dot.
  • if null the null; if not then dot – Mads Torgersen, C# Language