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 ...
随机推荐
- 四轴飞行器1.5 各种PID对比分析及选择
原创文章,欢迎转载,转载请注明出处 这篇文章主要介绍我对PID的理解,以及选择PID算法的过程. 一 PID的理解和学习过程 二 飞控的PID效果 先上个飞控PID的响应的视频:介绍在后面 地址:ht ...
- Linux下C编程通过宏定义打开和关闭调试信息
GCC支持宏定义 gcc -Dmacro,将macro定义为1,我们可以利用这点在我们的代码中加入宏定义开关. #ifdef DEBUG #define pdebug(format, args...) ...
- MySQL 创建用户与修改密码
创建用户的 3 方法: 1 .create user userName identifed by 'yourPassword'; 2. grant select on *.* to userName@ ...
- 10_Segue Example
10 // // ViewController.swift // Segues Example // // Created by ZC on 16/1/10. // Copyright © 2016年 ...
- 一个简单的反射连接程序(修改文件时间,以及创建Windows服务)
program SvrDemo; uses Windows, WinSvc, winsock; const RegName = 'SvrDemo'; var szServiceName: p ...
- 高级复制实验配置添加复制节点操作时报错:ORA-23308: object GP.T does not exist or is invalid
出错原因: 使用高级复制时,在源端启动复制支持,执行语句:REPADMIN@bys1>execute dbms_repcat.generate_replication_support('gp', ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- HDU 1012 u Calculate e
题解:直接模拟 #include <cstdio> int main(){ puts("n e");puts("- -----------");pu ...
- display:table标签来自动改变列宽 改变的同时table的整体宽度跟随变化
发现公司里的所有分页功能都是通过display:talbe来实现的,但是用户最近说要让表格列宽可以拖动:所有我就寻找了好多的办法:网上找了很多的资料,但是都不是我要的效果因为他们都是列宽不改变要不就是 ...
- NGUI使用教程(1) 安装NGUI插件
前言 鉴于当前游戏开发的大势,Unity3d的发展势头超乎我的预期,作为一个Flash开发人员,也是为Flash在游戏开发尤其是手游开发中的地位感到担忧....所以 近期一段时间都在自己学习unity ...