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. codeforces 149E . Martian Strings kmp

    题目链接 给一个字符串s, n个字符串str. 令tmp为s中不重叠的两个连续子串合起来的结果, 顺序不能改变.问tmp能形成n个字符串中的几个. 初始将一个数组dp赋值为-1. 对str做kmp, ...

  2. 为什么Java项目前会出现一个红色感叹号!

    先看看问题,如下图所示: 造成这个问题的原因是,我把一个 jar 包删除了,然后又配了个新的进去,然后就一直有这个错误,刚开始很郁闷,怎么已经配置过儿,还出现这个问题?关键是代码里面没有报错的.郁闷的 ...

  3. 一些特殊css

    属性 描述            outline  (轮廓)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用. outline:#00FF00 dotted thick; 可以按顺序 ...

  4. android小知识之中如何获取当前时间

    百度整理过来的 [java] view plaincopyprint? import    java.text.SimpleDateFormat; SimpleDateFormat    format ...

  5. Linux中的IO复用接口简介(文件监视?)

    I/O复用是Linux中的I/O模型之一.所谓I/O复用,指的是进程预先告诉内核,使得内核一旦发现进程指定的一个或多个I/O条件就绪,就通知进程进行处理,从而不会在单个I/O上导致阻塞. 在Linux ...

  6. cocos2dx 坐标和锚点

    cocos2dx中使用opengl坐标系,左下角为坐标原点,在大部分情况下,都是使用这种坐标系的. 当我们创建了一个渲染对象到窗口后,那么这个对象本身也是也是有自己的坐标系的,这种坐标系是节点自己的坐 ...

  7. 现场故障案例:AIX安装Oracle10G runInstaller弹出错误一例

    AIX安装Oracle10G runInstallert弹出错误一例 环境: 系统:AIX5300-08 数据库:Oracle 10g(64bit) AIX客户机卸载oracle软件后,又一次安装or ...

  8. GridView行编辑、更新、取消、删除事件使用方法

    注意:当启用编辑button时,点击编辑button后会使一整行都切换成文本框.为了是一行中的一部分是文本框,须要把以整行的全部列都转换成模板,然后删掉编辑模板中的代码.这样就能使你想编辑的列转换成文 ...

  9. UIView 中bounds和frame的差别

    搞iOS开发的童鞋基本都会用过UIView,那他的bounds和frame两个属性也不会陌生,那这两个有什么实质性的区别呢? 先看到下面的代码你肯定就明白了一些: -(CGRect)frame{    ...

  10. HDU1200:To and Fro

    Problem Description Mo and Larry have devised a way of encrypting messages. They first decide secret ...