ACCESS MODIFIERS

c# has some access modifiers as below:

public:class or member can be accessed by no restrictions

private:members can only be accessed within the class itself

internal:class or member can only be accessed within the assembly,not others.

protected:members can only be accessed by derived class(or within the class of course), not others non-derived class.

protected internal: members can only be accessed by derived class, OR, in the same assembly.

NOTE:

  1. not all access modifiers can be used on class,that's why there's underline on "class or member" and "members". it's nonsense to use private,protected,protected internal on a class. This may make mistake easily.
  2. no access modifier allowed on namespace,but public is implicitly.
  3. internal is default to a class
  4. no access modifier allowed on a interface but public is implicitly.
  5. private is default to members.

Modifiers and Static

Can not use override,virtual or abstract on a static member, cause these are for instance of inheriting

Size of basic types

type size(bits) Range
 char  16(2 bytes of unicode)  
 byte(unsigned-byte)  8  0 ~ 255
 sbyte(signed-byte)  8  -128 ~ 127
 short  16  –32,768 ~ 32,767
 ushort  16  0 ~ 65,535
 int  32  –2,147,483,648 ~ 2,147,483,647
 uint  32  0 ~ 4294967295
 long  64  –9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
 ulong  64  0 ~ 18,446,744,073,709,551,615
 decimal  128  

dynamic

for most time, dynamic is just the same as object,(xx is dynamic) equals to (xx is object) so:

int a=1;
(a is dynamic) is true

but (null is object) is false, so:

string a=null;
(a is dynamic) is false,of course

dynamic i=1;
(i is object) is true

params

No additional prameters are permitted after "params" prameters, and only one params keyword is allowed in a method argument declaration.

A "params" parameter has to be an array.

bitwise

&: computes the logical bitwise AND
|: computes the logical bitwise OR
^:if and only if one operand is true then true,or maybe i can say, if the operands is not the same then true otherwise false.
7^2=>111^010=>101=>5

class and struct

  1. class is a reference type that stored in heap,and struct is value type that stored in stack. Struct often used to present light weight object which only contains data.
  2. struct can have constructor which have parameter(s), default constructor(paramterless) is not allowed.
  3. struct can have const,field,property,method,index,operator,event,but if you need these members,then a class may be better.
  4. struct can implement interfaces,but can not inherit from a class,so members of struct can not be protected.
  5. you can declare a struct object without "new" keyword: 
    MyStruct myStruct;
    myStruct.Pro1="value";
  6. only class can have deconstructor


boxing & unboxing

boxing means value type data in stack "be boxed" to manage heap
unboxing mean reference type data in manage heap "be unboxed" to stack.

but why we call that "boxing" and "unboxing"? i think just because managed heap is a place where will be cleaned(by GC),so we will package up garbages,or  "box" garbages up and put them onto the heap.But sometime we don't want it to be collected by GC,so unbox it from heap and put them onto stack. Just kidding^^

Equals and ==

When we're talking about equal, actually we're talking about two kind of equal: logical equal(having the same value) or reference equal(having the same reference)

For reference objects, Equals method and the "==" operator have the same behavior, that is, comparing two objects to see if they have the same reference.But sometime we want a little bit change so we override the Equals method to let it compare the value rather than reference,String type is a classic example, when we use Equals method to compare two string,actually we are checking if they are the same sequence of chars.

Things is a little bit complicate for value type.The Equals method of value type is used to check the value equality, but value type don't have the "==" operator, unless you overload it.

Dictionary and Hashtable

  1. Dictionary and Hashtable are based on hash,Dictionary is actually a hashtable. Dictionary supports generic type while Hashtable don't, so Dictionary is type-safe,and has a better performance, because we don't need to convert the value which is object type to what it actually is,so no risk of boxing/unboxing.
  2. Both use the GetHashCode method to check the uniqueness of keys,but if there is already a key which have the same hashcode,then the Equals method will be called to check if they are ference equals.

Interface

  1. interface can extend another interface
  2. interface CAN have indexer! check it on MSDN
  3. interface CAN have event! check it on MSDN
  4. "Interfaces can contain events, indexers, methods, and properties." ——MSDN
  5. You can implement multi interfaces which have method(s) with same name!! In this situation,you should implement them explicitly:
    public interface ITest {
    void Test();
    }
    public interface ITest2 {
    void Test();
    }
    public class Dual : ITest, ITest2
    {
    void ITest.Test() {
    Console.WriteLine("ITest.Test");
    }
    void ITest2.Test() {
    Console.WriteLine("ITest2.Test");
    }
    }

    And the methods can not be public.In order to access the method, you have to first convert the class to the interface type:

    var dual = new Dual();
    // Call the ITest.Test() function by first assigning to an explicitly typed variable
    ITest test = dual;
    test.Test();
    // Call the ITest2.Test() function by using a type cast.
    ((ITest2)dual).Test();

What is serialization?

Serialization is a converting from data structure or object state in memory to byte stream so that it can be store,transfer,or turn into other format like xml,json under helping of formaters.

Foreground thread and background thread

  1. A managed thread is either foreground thread or background thread.Foreground thread(s) keep application running,while background don't.
  2. Process can not be stopped until all foreground threads are stopped.
  3. Process won't be stopped by stopping Background thread.
  4. Thread is foreground by default,you can set the IsBackground property to be ture if needed.

Mutable object and Immutable object

Mutable means "changeable" and Immutable means "unchangeable"

Immutable object means that once you create the object, you can never change its state.For example, string is immutable, when you declare a variable of string : string a="foo", then an object of string "foo" is created, when you do this:a="bar", you are actually creating a new instance of string ,the origin instance "foo" still exists and never ever be changed.

1. "obj is dynamic" is always true, nomatter what type is obj,unless obj is null。

2."params" parameter must be the last one,even to "out" parameter

3.Why can not a struct have default constructor??

Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.

so basically your code is equal to:

    DateTime? dt = DateTime.Now;
object box = (object)dt;
Console.Write(box.GetType().ToString());

also, looking at "Boxing Nullable Types" on MSDN we read:

If the object is non-null -- if HasValue is true -- then boxing occurs, but only the underlying type that the nullable object is based on is boxed. Boxing a non-null nullable value type boxes the value type itself, not the System.Nullable(Of T) that wraps the value type.

this clearly explain the "strange" behavior of Nullable<T>.GetType()

Easy mistakes in c#的更多相关文章

  1. 【转载学习前辈的经验】-- Mistakes I made (as a developer) 我(作为一名开发者)所犯过的错误

    我 2006 年开始工作,至今已经 10 年.10 年是个里程碑,我开始回顾自己曾经犯过的错误,以及我希望从同行那里得到什么类型的忠告.一切都在快速改变,10 年了,我不能确定这些秘诀是否还有用. 不 ...

  2. 10 Biggest Business Mistakes That Every Entrepreneur Should Avoid

    原文链接:http://www.huffingtonpost.com/syed-balkhi/10-biggest-business-mista_b_7626978.html When I start ...

  3. [转]50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs

    http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/ 50 Shades of Go: Traps, Gotc ...

  4. Top 10 Mistakes Java Developers Make--reference

    This list summarizes the top 10 mistakes that Java developers frequently make. #1. Convert Array to ...

  5. Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)--reference

    (Sorry for that click-bait heading. Couldn’t resist ;-) ) We’re on a mission. To teach you SQL. But ...

  6. 转载:10 Easy Steps to a Complete Understanding of SQL

    10 Easy Steps to a Complete Understanding of SQL 原文地址:http://tech.pro/tutorial/1555/10-easy-steps-to ...

  7. 10 Easy Steps to a Complete Understanding of SQL

    原文出处:http://tech.pro/tutorial/1555/10-easy-steps-to-a-complete-understanding-of-sql(已经失效,现在收集如下) Too ...

  8. 8 Mistakes to Avoid while Using RxSwift. Part 1

    Part 1: not disposing a subscription Judging by the number of talks, articles and discussions relate ...

  9. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

随机推荐

  1. URL的getFile()和getPath()方法的区别(转)

    转自博客:http://blog.csdn.net/l375852247/article/details/7999063 import java.net.MalformedURLException; ...

  2. 微信小程序从子页面退回父页面时的数据传递 wx.navigateBack()

    我们知道,在微信小程序中,从一个页面转到另一个页面,一般情况下可以通过navigate或redirect时候的url来携带参数,然后在目标页面的onLoad函数参数中获取这些url参数.例如: // ...

  3. bootstrap更新数据层

    mq推送数据,表格实时更新,发现销毁表格不太合适,整体表格闪动,于是选择更新数据层. 先初始化表格,然后在推送数据的时候先循环遍历数据 例如: initDevTable(data.operatingL ...

  4. Sonar及其eclipse插件的安装 详细 http://www.importnew.com/10017.html

    参考:http://www.importnew.com/10017.html

  5. Linux运维基础入门(四):Linux中的网络知识04

    一,虚拟机的安装 略 二,Linux系统下的网络配置(Linux虚拟机的网络设定为桥接模式) 桥接模式:虚拟机同主机一样,在网络中相当于一个真实存在的装有Linux系统的电脑.(我们先用这个模式) N ...

  6. 《集体智慧编程》第7章代码 Python3执行出错

    电子工业出版社,2015年第3版 P153,增加了buildtree函数后执行出错,报错为: ----------------------------------------------------- ...

  7. js高级——构造函数,实例对象和原型对象——prototype、__proto__和constructor构造器

    一.前言 了解JavaScript面向对象,需要先了解三个名词: 构造函数,实例对象和原型对象. 注意:JavaScript中没有类(class)的概念,取而代之的是构造函数,两者类似却又有很大的差别 ...

  8. MySQL学习3---事务

    MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据. 在 MySQL 中只有使用了 Innodb 数据库引擎的数据库或表才支持事务. 事务处理可以用来维护数据库的完整性,保证成批的 ...

  9. Django基础学习七之如何配置django+mysql

    很久没有更新博客了,也有段时间没有持续性的学习了,感觉堕落了,今天继续开始学习吧 今天主要来学习一下在django下配置mysql的数据库和使用admin用户管理数据库 1.在project中的set ...

  10. 最大正方形 · Maximal Square

    [抄题]: 在一个二维01矩阵中找到全为1的最大正方形 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 返回 4 [暴力解法]: 时间分析: 空间分析:i j 中保留一 ...