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 ...
随机推荐
- BOM 浏览器对象模型学习
window对象属性 innerWidth/innerHeight 浏览器窗口的内部宽度与高度 outerWidth/outerHeight 浏览器的外部宽度与高度 length window.fra ...
- JDK的目录结构及结构图
-bin目录: JDK开发工具的可执行文件 -lib目录: 开发工具使用的归档包文件 -jre: Java 运行时环境的根目录,包含Java虚拟机,运行时的类包和Java应用启动器, ...
- php mvc 框架演示
<pre name="code" class="cpp"><pre name="code" class="pyt ...
- Uva 1612 Guess
Thinking about it: 题目要求最后一名(也就是第N位)的分数要尽量的大,那么就一定要求第N-1名的分数也要尽量大.假如N-1可以取400和500,那么N-1应该取500,如果取400, ...
- openssl命令行Base64编解码
openssl对base64编解码的规范支持较差,用它编解码的结果别的语言如php处理很不方便,注意的几点整理如下 1,如果php加密结果做base64编码长度小于64,则需要添加一个换行符opens ...
- CodeForces 22C System Administrator
把v和2结点交换, 1和v连,其它点和v之间能够互相连. #include <iostream> #include <cstdlib> #include <cstring ...
- Foundation 框架 NSFileManager,NSData 简单的文件操作
一.简单展示NSFileManager的使用 #import <Foundation/Foundation.h> int main(int argc, const char * argv[ ...
- [Swust OJ 582]--放学了,抢机子了(SPFA)
题目链接:http://acm.swust.edu.cn/problem/0582/ Time limit(ms): 5000 Memory limit(kb): 65535 Descriptio ...
- c++ primer plus 习题答案(6)
p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- MyEclipse 8.5整合Git,并在Github上发布项目(转)
下载Eclipse的git插件——EGit.下载网址http://download.eclipse.org/egit/updates-1.3/org.eclipse.egit-updatesite-1 ...