C# Common Keyword II

1、as 运算符用于在兼容的引用类型之间执行某些类型的转换。

   class csrefKeywordsOperators
{
class Base
{
public override string ToString()
{
return "Base";
}
}
class Derived : Base
{ } class Program
{
static void Main()
{ Derived d = new Derived(); Base b = d as Base;
if (b != null)
{
Console.WriteLine(b.ToString());
} }
}
}

  as 运算符类似于强制转换操作。但是,如果无法进行转换,则 as 返回 null 而非引发异常。请看下面的表达式:

expression as type
// 它等效于以下表达式,但只计算一次 expression。
expression is type ? (type)expression : (type)null

参考:http://msdn.microsoft.com/zh-cn/library/cscsdfbt(v=vs.90).aspx

2、Metadata is binary information describing your program that is stored either in a CLR(common language runtime ) portable executable (PE) file or in memory.When you compile your code into a PE file, metadata is inserted into one portion of the file, while your code is converted to Microsoft intermediate language (MSIL) and inserted into another portion of the file.When code is executed, the runtime loads metadata into memory and references it to discover information about your code's classes, members, inheritance, and so on.

  Metadata描述存储于CRL-PE或存储于于内存中的program。当编译时,metadata被插入到文件中。当执行时,runtime加载metadata到内存,根据metadata来发现你的程序的信息。

  • Description of the assembly.

    • Identity (name, version, culture, public key).

    • The types that are exported.

    • Other assemblies that this assembly depends on.

    • Security permissions needed to run.

  • Description of types.

    • Name, visibility, base class, and interfaces implemented.

    • Members (methods, fields, properties, events, nested types).

  Metadata is the key to a simpler programming model, eliminating the need for Interface Definition Language (IDL) files, header files, or any external method of component reference. Metadata allows .NET languages to describe themselves automatically in a language-neutral manner, unseen by both the developer and the user.

参考:http://msdn.microsoft.com/en-us/library/xcd8txaw(v=vs.90).aspx

3、public static class A,静态类。静态类的主要功能如下:

  因此创建静态类与创建仅包含静态成员和私有构造函数的类大致一样。私有构造函数阻止类被实例化。使用静态类的优点在于,编译器能够执行检查以确保不致偶然地添加实例成员。编译器将保证不会创建此类的实例。静态类不能包含构造函数,但仍可声明静态构造函数以分配初始值或设置某个静态状态。

class SimpleClass
{
// Static constructor
static SimpleClass()
{
//...
}
}

  

  • 在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化。无法直接调用静态构造函数。

  • 无法直接调用静态构造函数,也无法何时执行静态构造函数。

参考:

1)http://msdn.microsoft.com/zh-cn/library/79b3xss3(VS.80).aspx

2)http://msdn.microsoft.com/zh-cn/library/k9x6w0hc(v=vs.80).aspx

4、is关键字用于判断对象是否为从 MyObject 派生的一个类型:

class Class1 {}
class Class2 {}
class Class3 : Class2 { } class IsTest
{
static void Test(object o)
{
Class1 a;
Class2 b; if (o is Class1)
{
Console.WriteLine("o is Class1");
a = (Class1)o;
// Do something with "a."
}
else if (o is Class2)
{
Console.WriteLine("o is Class2");
b = (Class2)o;
// Do something with "b."
} else
{
Console.WriteLine("o is neither Class1 nor Class2.");
}
}
static void Main()
{
Class1 c1 = new Class1();
Class2 c2 = new Class2();
Class3 c3 = new Class3();
Test(c1);
Test(c2);
Test(c3);
Test("a string");
}
}
/*
Output:
o is Class1
o is Class2
o is Class2
o is neither Class1 nor Class2.
*/

参考:http://msdn.microsoft.com/zh-cn/library/scekt9xw(v=vs.90).aspx

5、方法参数不能有默认值。如果要获得同样的效果,请使用方法重载。

// CS0241.cs
public class A
{
public void Test(int i = ) {} // CS0241
} public class B
{
public void Test() { Test(); }
public void Test(int i) {}
} public class C
{
public static void Main()
{
B x = new B();
x.Test();
}
}

参考:http://msdn.microsoft.com/zh-cn/library/294000kk(v=vs.90).aspx

6、using有以下2个用途:

  

  除此外,还有一个using语句的用法。using 指令的范围限制为包含它的文件。

7、default关键字。

  给定参数化类型 T 的一个变量 t,只有当 T 为引用类型时,语句 t = null 才有效;只有当 T 为数值类型而不是结构时,语句 t = 0 才能正常使用。解决方案是使用default 关键字,此关键字对于引用类型会返回 null,对于数值类型会返回零。对于结构,此关键字将返回初始化为零或 null 的每个结构成员,具体取决于这些结构是值类型还是引用类型。

public class GenericList<T>
{
private class Node
{
//... public Node Next;
public T Data;
} private Node head; //... public T GetNext()
{
T temp = default(T); Node current = head;
if (current != null)
{
temp = current.Data;
current = current.Next;
}
return temp;
}
}

参考:http://msdn.microsoft.com/zh-cn/library/sf0df423(v=vs.90).aspx

C# Common Keyword II的更多相关文章

  1. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  2. C# Common Keyword

    [C# Common Keyword] 1.abstract Use the abstract modifier in a class declaration to indicate that a c ...

  3. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  4. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  5. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

  6. spoj1812 LCS2 - Longest Common Substring II

    地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags  A string is fi ...

  7. SPOJ1812 Longest Common Substring II

    题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...

  8. SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS

    LCS2 - Longest Common Substring II no tags  A string is finite sequence of characters over a non-emp ...

  9. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

随机推荐

  1. Matlab 之 FFT的理解和应用

    网上看了一些大牛的关于FFT的见解,加上自己的一点儿理解,针对以下这几个问题来加深对FFT的理解. 不知道大家有没有类似以下几点的困惑: 问题的提出 对于1秒钟输出的连续信号,使用采样率Fs不同,就会 ...

  2. Linq 分组(group by)后列变行

    表一: 表二: 已知表一的List,想得到表二的结果: var query = from c in t.AsEnumerable() group c by new { pingming = c.Fie ...

  3. hibernate规避SQL注入实例

    项目被检测出SQL注入,注入url如:http://127.0.0.1:8080/Test/wlf/getServiceInfo.html?province=%25E6%25B5%2599%25E6% ...

  4. IDEA開發 java web 初步

    作爲一個小白,我也不知道爲啥同學們喜歡用IDEA開發,而不選擇eclipse,但是在項目學習中eclipse卻真的多次出現問題,無奈之下,本人也安裝了一個IDEA作爲學習使用.參考了博客開始使用這個工 ...

  5. .NET的URL重写

    [概述] URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程.重写URL是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力:而且在你改变了 ...

  6. 【转】利用JMeter进行压力测试

    压力测试以软件响应速度为测试目标,尤其是在较短时间内大量并发用户的同时访问时,软件的性能和抗压能力. JMeter是一款开源的压力测试工具,目前最新Release版本是2.3.4,它不仅可以测试Web ...

  7. java代码啊==indexOf()方法返回字符第一次出现的位置

    package com.s.x; public class Wang { public static void main(String[] args) { if ("woaini" ...

  8. NGUI的UIPanel、UIButton、AtlasMaker、Widget、Anchor、Tween、RectTransform

    全文请看:http://note.youdao.com/noteshare?id=f7b476be35ec554e311bc13ef60b62ef

  9. ubuntu16.04让内核编译一次过的方法

    问题: 进入内核后,发现make menuconfig 出错,而且在在网上找到的一些安装包,安装结束后,发现make menuconfig后的图形界面虽然出来了,但是图形界面里的内容没有出来! 解决方 ...

  10. node中的socket.io制作命名空间

    如果开发者想在一个特定的应用程序中完全控制消息与事件的发送,只需要使用一个默认的"/"命名空间就足够了.但是如果开发者需要将应用程序作为第三方服务提供给其他应用程序,则需要为一个用 ...