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. halcon与C#混合编程

    halcon源程序: dev_open_window(0, 0, 512, 512, 'black', WindowHandle)read_image (Image, 'C:/Users/BadGuy ...

  2. iOS 相关职位要求整理版

    在拉勾上找了20家,BOSS直聘找了10家感兴趣的在招聘 iOS 程序员的公司,把职位要求整理了一下. 初创公司一般要求1年以上开发经验,成长型或者成熟型公司一般要求最低2年以上开发经验.这里针对的是 ...

  3. 一步一步学习SignalR进行实时通信_2_Persistent Connections

    原文:一步一步学习SignalR进行实时通信_2_Persistent Connections 一步一步学习SignalR进行实时通信\_2_Persistent Connections Signal ...

  4. 关于crontab笔记

    如下所示,一般crontab文件里面的定时任务格式如下所示: 59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2 ...

  5. ecshop删除商品函数

    /** * 从回收站删除多个商品 * @param mix $goods_id 商品id列表:可以逗号格开,也可以是数组 * @return void */ function delete_goods ...

  6. 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法

    Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  7. NGUI使用教程(1) 安装NGUI插件

    前言 鉴于当前游戏开发的大势,Unity3d的发展势头超乎我的预期,作为一个Flash开发人员,也是为Flash在游戏开发尤其是手游开发中的地位感到担忧....所以 近期一段时间都在自己学习unity ...

  8. 23种设计模式的C++实现

    之前看Head First设计模式的时候照着书上的代码实现了一个C++版本(书上是Java版本的),代码上传在https://github.com/clpsz/Book-HFDP-Code. 当时因为 ...

  9. SQL数据库关键字和列名冲突处理

    在设计SQL数据库的时候可能由于考虑不全,使列名和数据库内关键字冲突,可能导致Query不能被正确识别,对列名要加[]处理.

  10. list根据所存对象属性排序

    比如有个list,里面存的是一个个对象,对象有个list属性,其值可以是字符串和数字. private void getSortList(List<AclResource> newList ...