C# Common Keyword II

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 = 9) {}   // CS0241
}

public class B
{
   public void Test() { Test(9); }
   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 指令的范围限制为包含它的文件。

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

C# Common Keyword II

时间: 2024-10-23 23:07:38

C# Common Keyword II的相关文章

spoj 1812 LCS2 - Longest Common Substring II (后缀自动机)

spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 <= n <= 10 |A[i]| <= 1e5 思路: 和spoj 1811 LCS差不多的做法 把其中一个A建后缀自动机 考虑一个状态s, 如果A之外的其他串对它的匹配长度分别是a[1], a[2], ..., a[n - 1], 那么min(a[1], a[2], ..., a[n - 1]

spoj1812 LCS2 - Longest Common Substring II

地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the set of lowercase letters. Substring, also called factor, is a cons

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

Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the set of lowercase letters. Substring, also called factor, is a consecutive sequenc

SPOJ Longest Common Substring II

Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: LCS264-bit integer IO format: %lld      Java class name: Main A string is finite sequence of characters over a non-empty finite se

SPOJ LCS2 1812. Longest Common Substring II

SPOJ Problem Set (classical) 1812. Longest Common Substring II Problem code: LCS2 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the set of lowercase letters. Substring, also called factor, is a consecu

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

spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 <= n <= 10 |A[i]| <= 1e5 思路: 和spoj 1811 LCS几乎相同的做法 把当中一个A建后缀自己主动机 考虑一个状态s, 假设A之外的其它串对它的匹配长度各自是a[1], a[2], ..., a[n - 1], 那么min(a[1], a[2], ..., a[n -

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

[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记录\(f[i]\)表示走到了\(i\)节点 能够匹配上的最长公共子串的长度 当然,每个串的\(f[i]\)可以更新\(f[i.parent]\) 所以需要拓扑排序 对于每个串求出每个节点的最长匹配 然后对他们取\(min\),表示某个节点大家都能匹配的最长长度 最后对于所有点的值都取个\(max\)

【SPOJ】Longest Common Substring II

[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完后去最小值才是每个点的最终贡献 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm

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

http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要忘记了一点:更新parent树的祖先. 为什么呢?首先如果子树被匹配过了,那么长度一定大于任意祖先匹配的长度(甚至有些祖先匹配长度为0!为什么呢,因为我们在匹配的过程中,只是找到一个子串,可能还遗漏了祖先没有匹配到,这样导致了祖先的记录值为0,那么在对对应状态去min的时候会取到0,这样就wa了.而