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 ...
随机推荐
- PHP判断是中文还是英文
static function ischinese($s){ $allen = preg_match("/^[^/x80-/xff]+$/", $s); //判断是否是英文 $al ...
- break的使用例一
/* Name:break的使用例一 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月21日 02:28:24 Description:本程序代码无如何含 ...
- 一个用C++写的Json解析与处理库
什么是Json?这个库能做什么? JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is e ...
- CDialogSK - A Skinnable Dialog Class
Introduction This class is derived from the MFC CDialog. It supports the following features :- If ru ...
- tree(简单并差集)
tree Accepts: 156 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- HTML5API___manifest
离线缓存 manifest 在html标签里面增加个属性 mainfest 就可以告诉浏览器缓存文件在哪里. <html manifest='show.manifest' xmlns=" ...
- JavaScript之字符串引号的使用技巧
在JavaScript中可以随意使用引号,但是最好根据字符串包含的字符来选择. 1.如果字符串里面包含了单引号,那就把字符串放在双引号里面 var age = "this is 'pig'? ...
- Android:广播接收器(BroadCastReceiver)要点随笔。
@@@描述 广播接收器可以收到 Context.sendBroadcast或者Context.sendOrderedBroadcast发出的意图(intent). @@@Local ...
- MySQL 开放局域网
局域网连接mysql报错: ERROR 1130: Host '192.168.0.220' is not allowed to connect to this MySQL server 解决方法: ...
- les nationalités et les pays
masculin féminin pays français française la France chinois chinoise la Chine suisse suisse ...