C# Best Practices - Accessing and Using Classes
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的更多相关文章
- iOS 10.0 更新点(开发者视角)
html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0 ...
- Follow me to learn what is repository pattern
Introduction Creating a generic repository pattern in an mvc application with entity framework is th ...
- C# Best Practices - Building Good Classes
Building a Class The last four refer as members Signature Accessiblity modifier (Default:internal) c ...
- C# Best Practices - Define Proper Classes
Application Architecture Define the components appropriately for the application and create project ...
- .NET Best Practices
Before starting with best practices tobe followed, it is good to have clear understanding of how mem ...
- Exception (3) Java exception handling best practices
List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...
- 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 ...
- 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 ...
- Best Practices for Speeding Up Your Web Site
The Exceptional Performance team has identified a number of best practices for making web pages fast ...
随机推荐
- 用while判断输入的数字是否回文数
/* Name:用while判断输入的数字是否回文数 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月18日 04:29:07 Description:用 ...
- 简单介绍一下ODI的几个基本概念
简单介绍一下ODI的几个基本概念 ODI的几个基本概念是本文我们主要要介绍的内容,接下来我们就开始介绍这一过程,一起来看看吧! 什么是资料库 ODI资料库可安装在任何支持ANSIISO89的数据库 ...
- HTTP使用BASIC认证的原理及实现方法(还有NTLM方法,比较复杂)
一. BASIC认证概述 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份证的方法,当一个客户端向HTTP服务 器进行数据请求时,如果客 ...
- python总结
环境:django,numpy,matplotlib, 解释语言:开发效率高,通用性强,内置方便的数据容器,易于扩展和嵌入. 语言:lua--嵌入式/网络/APP,erlang--嵌入式,python ...
- BZOJ 1037 [ZJOI2008]生日聚会Party(单调DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1037 [题目大意] 现在有n个男生,m个女生排成一行,要求不存在一个区间男女之差大于k ...
- HDU 3613 Best Reward(扩展KMP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果 ...
- Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs)
Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs) Cola:一个分布式爬虫框架 发布时间:2013-06-17 14:58:27, 关注:+2034, 赞美: ...
- [ javascript ] 司徒正美的fadeOut-fadeIn效果!
首先感谢司徒正美的文章! 在司徒大神的博客看到一个简单的渐入渐出的效果.全然採用js实现. 例如以下: <!doctype html> <html dir="ltr&quo ...
- XML方式实现Spring声明式事务管理
1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...
- ADO接口
转自百度文库 ADO中最重要的对象有三个:Connection.Recordset和Command,分别表示连接对象.记录集对象和命令对象. 三个对象对应的智能指针分别是:_ConnectionPtr ...