References and Using

Do:

Take care when defining references

References must be one way (or circular dependency error)

Take advantage of the using directive

Avoid:

Excessive use of the using static directive

Object vs. Class

Object:

Represents one special thing (Example:Hammer or Saw)

Defines one thing created from that template

Created at runtime with the new keyword

Class:

Represens things of the same type (Example:Product)

Define the template specifying the data and processing associated with all things of that type

Created at develop time with code

Static class doesn't create object

Object initialization

3 Ways to initialize object:

1.Setting properties

Easy to debug

When populating from database values

When modifying properties

2.Parameterized constructor

When setting the basic set of properties

3.Object initializers

When readability is important

When initializing a subset or superset of properties

Instantiating Related Objects

Usage Scenarios

One method, Always, Sometimes

One method

Initialize in the method that needs it

public string SayHello()
{
var vendor = new Vendor();
var vendorGreeting = vendor.SayHello();
}

Always

Define a property

private Vendor productVendor;
public Vendor ProductVendor
{
get { return productVendor; }
set { productVendor = value; }
}
public Product()
{
this.ProductVendor = new Vendor();
}

Sometimes

Define a property

Initialize in the property setter

"Lazy Loading"

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

Null Checking

if (currentProduct != null && currentProduct.ProductVendor != null)
{
var companyName = currentProduct.ProductVendor.CompanyName;
}

C# 6 New Features

var companyName = currentProduct?.ProductVendor?.CompanyName;

"If null then null,it not then dot."

FAQ

1.What's the difference between an object and a class?

A class is a template that specifies the data and operations for an entity.

An object is an instance of that class created at runtime using the new keyword.

2.What is lazying loading and when would you use it?

Instantiating related objects when they are needed and not before.

This often involves creating the instance in the property getter for the related object

C# Best Practices - Accessing and Using Classes的更多相关文章

  1. iOS 10.0 更新点(开发者视角)

    html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0 ...

  2. Follow me to learn what is repository pattern

    Introduction Creating a generic repository pattern in an mvc application with entity framework is th ...

  3. C# Best Practices - Building Good Classes

    Building a Class The last four refer as members Signature Accessiblity modifier (Default:internal) c ...

  4. C# Best Practices - Define Proper Classes

    Application Architecture Define the components appropriately for the application and create project ...

  5. .NET Best Practices

    Before starting with best practices tobe followed, it is good to have clear understanding of how mem ...

  6. Exception (3) Java exception handling best practices

    List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...

  7. Lambda Expressions and Functional Interfaces: Tips and Best Practices

    转载自https://www.baeldung.com/java-8-lambda-expressions-tips 1. Overview   Now that Java 8 has reached ...

  8. Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses

    5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...

  9. Best Practices for Speeding Up Your Web Site

    The Exceptional Performance team has identified a number of best practices for making web pages fast ...

随机推荐

  1. 生成输出url

    继续使用前面的例子11-3URLTestDemo,修改Global.asax中的RegisterRoutes方法如下: public static void RegisterRoutes(RouteC ...

  2. java调用C++ DLL库方法

    最近一个项目要开发网页端人脸识别项目,人脸识别的算法已经写好,是C++版,但是网页端要求使用Java后台,这就涉及到Java调用DLL的问题.经过查找,实现了一个简单的例子. 1.第一步,先在Java ...

  3. JAVA并发,线程异常捕获

    由于线程的特性,当我们启动了线程是没有办法用try catch捕获异常的,如下例: package com.xt.thinks21_2; import java.util.concurrent.Exe ...

  4. JAVA泛型-自动包装机制不能应用于泛型数据的测试

    <thinging in java>中指出自动包装机制不能应用于泛型数据,自己写的测试代码如下: package com.xt.thinks15_11_1; import java.uti ...

  5. gcc 的include path和lib path调整

    `gcc -print-prog-name=cc1plus` -v `g++ -print-prog-name=cc1plus` -v   ------------------------------ ...

  6. oralce dg conf

    http://wenku.baidu.com/view/ea9fa16cdd36a32d73758168.html http://ylw6006.blog.51cto.com/470441/84181 ...

  7. 通达OA 小飞鱼开发培训第四讲 工作流介绍(图文)

    本次课程主要解说了OA工作流相关内容,有些涉及到工作流的程序开发假设对工作流不熟悉也是有非常大难度,因此在这里进行了内容补充.   1.工作流介绍

  8. PID教程

    PID教程 介绍 本教程将向您展示了比例每一个比例项 (P)的特点,积分项(I)和微分项 (D) 控制,以及怎样使用它们来获得所需的响应.在本教程中,我们会考虑下面单位反馈系统: Plant[被控对象 ...

  9. winform treeview 通过节点名称添加子节点

    /// <summary> /// 添加人员节点(利用TreeNodeCollection) /// </summary> /// <param name="t ...

  10. 2015.8.2 jdbc实现商品类的增删查改

    在惠普济宁基地进行了两周sql和java的学习,学到很多东西 刚才实现了用jdbc访问数据库对数据库进行操作,是用eclipse写的,过几天移植到NetBeans上,个人还是比较习惯看图形化界面 前几 ...