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. Android倒计时Button

    最近做用户绑定,需要用到倒计时的一个Button,就花点时间封装了一个,非常简单,效果图如下: 1.TimeButton 自定义倒计时Button package com.example.timebu ...

  2. 转: angular编码风格指南

    After reading Google's AngularJS guidelines, I felt they were a little too incomplete and also guide ...

  3. VS2012生成绿色版程序的方法

    方法就是在工程属性里设置: 配置属性-〉常规-〉项目默认值-〉MFC的使用-〉在静态库中使用MFC,见下图 之后重新编译即可.

  4. AspectJ给类的属性打桩,进行替换。

    这个东西必须写个博客记一下了,一方面是防止以后忘记,一方面也反思一下自己的固执. 在我们的代码中,通常会有一些配置文件的路径写死在代码里面.比如 public class ConfigPath { p ...

  5. SSD磁盘,CPU居高不下,高并发的情况下,是不是mysql解析器耗费的cpu资源高?

    你看看我做的实验,这个user表是300多W纪录,普通磁盘下,消耗时间最多的是Copy to tmp table 0.81秒,当然在ssd下,这个可以减少很多很多的,第二高就是sending data ...

  6. Javascript对象的声明

    JavaScript 对象 对象由花括号分隔.在括号内部,对象的属性以名称和值对的形式 (name : value) 来定义.属性由逗号分隔: var person={firstname:" ...

  7. URAL 1297 Palindrome 后缀数组

    D - Palindrome Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  8. Java学习之对象实例化

    一个对象实例化过程:Person p = new Person();1,JVM会读取指定的路径下的Person.class文件,并加载进内存,并会先加载Person的父类(如果有直接的父类的情况下). ...

  9. java freemark生成word文档

    1.下载freemarker-2.3.19.jar 2.把要填充的内容用  ${title},${no}代替 3.用word 打开,保存为2003xml 4.打开生成xml文件,看下有没有把表达式  ...

  10. php基础知识(有代码有注释)

    回顾 数组: 数据的组合 数组定义: 三种方式(常用array()) 数组访问: 下标 数组分类: 关联数组, 索引数组和混合数组 数组循环遍历: foreach遍历(foreach原理) 二维数组: ...