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 ...
随机推荐
- label 和 legend标签的用法
label 和 legend标签的用法 label标准用法: 一般浏览器都支持 一般而言,label标签位于表单元素的前面或者后面,为控件提供说明文字 <label for="user ...
- 《windows程序设计》学习_3.4:实现雷区翻转
#include<windows.h> #include "resource.h" LRESULT CALLBACK WndProc (HWND, UINT, WPAR ...
- Orz 终于有了自己的博客地址
新博客地址:http://www.wnjxyk.cn/
- req.body取不到值的问题;
随着express升级,bodyParser从express中被分离了出来,因此,在使用express新版本的时候,需要npm install body-parser 来安装bodyParser. 在 ...
- nefu 462 fib组合
nefu 462 fib组合 (斐波那契数列的通项公式以及推倒过程) 分类: 数学2014-05-21 10:27 190人阅读 评论(0) 收藏 举报 题目链接:http://acm.nefu.ed ...
- 解决Agent admitted failure to sign using the kye with ssh
之前如果建立 ssh 连接,只要將公匙复制到~/.ssh/authorized_keys就可以直接登录而不需要建立密碼. 如果在使用时候出现如下信息: Agent admitted failure t ...
- Debug目录下没有.exe文件
记一下小笔记: VC6.0设置.exe文件的输出路径: Project->Settings->Link Category选择"General" 在Output file ...
- Android应用开发基础篇(2)-----Notification(状态栏通知)
一.概述 Notification这个部件的功能是在状态栏里显示消息提醒,比如有未读的短信或者是未接的电话,那么状态栏里都会有显示,更或者是从某个应用(比如QQ,酷我音乐等等)里按Home键 ...
- iOS app 集成友盟推送问题
之前做app推送主要是集成友盟SDK,在程序获取deviceToken时,老是提示如下错误: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用 ...
- $timeout, $interval
$timeout, $interval layout: posttitle: Angular@1.4.3 中文 API 服务篇 $timeout & $intervaldesc: '$ti ...