{"id":232,"date":"2009-12-01T16:19:53","date_gmt":"2009-12-01T08:19:53","guid":{"rendered":"http:\/\/www.genepeng.com\/index.php\/232"},"modified":"2009-12-04T17:23:17","modified_gmt":"2009-12-04T09:23:17","slug":"c%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/www.genepeng.com\/index.php\/232","title":{"rendered":"c#\u5b66\u4e60\u7b14\u8bb0"},"content":{"rendered":"<ol>\n<li>C# Parameter Modifiers      <br \/>Parameter Modifier Meaning in Life       <br \/><strong>(None)<\/strong> If a parameter is not marked with a parameter modifier, it is assumed to be       <br \/>passed by value, meaning the called method receives a copy of the original       <br \/>data.       <br \/><strong>out<\/strong> Output parameters must be assigned by the method being called (and       <br \/>therefore are passed by reference). If the called method fails to assign       <br \/>output parameters, you are issued a compiler error.       <br \/><strong>ref<\/strong> The value is initially assigned by the caller and may be optionally reassigned       <br \/>by the called method (as the data is also passed by reference). No compiler       <br \/>error is generated if the called method fails to assign a ref parameter.       <br \/><strong>params<\/strong> This parameter modifier allows you to send in a variable number of       <br \/>arguments as a single logical parameter. A method can have only a single       <br \/>params modifier, and it must be the final parameter of the method. <\/li>\n<li>To define a nullable variable type, the question mark symbol (?) is suffixed to the underlying      <br \/>data type. Do note that this syntax is only legal when applied to value types. If you attempt to create       <br \/>a nullable reference type (including strings), you are issued a compile-time error. Like a nonnullable       <br \/>variable, local nullable variables must be assigned an initial value:       <br \/>static void LocalNullableVariables()       <br \/>{       <br \/>\/\/ Define some local nullable types.       <br \/>int? nullableInt = 10;       <br \/>double? nullableDouble = 3.14;       <br \/>bool? nullableBool = null;       <br \/>char? nullableChar = &#8216;a&#8217;;       <br \/>int?[] arrayOfNullableInts = new int?[10];       <br \/>\/\/ Error! Strings are reference types!       <br \/>\/\/ string? s = &quot;oops&quot;;       <br \/>} <\/li>\n<li>The ?? Operator      <br \/>The final aspect of nullable types to be aware of is that they can make use of the C# ?? operator.       <br \/>This operator allows you to assign a value to a nullable type if the retrieved value is in fact null. For       <br \/>this example, assume you wish to assign a local nullable integer to 100 if the value returned from       <br \/>GetIntFromDatabase() is null (of course, this method is programmed to always return null, but I       <br \/>am sure you get the general idea):       <br \/>static void Main(string[] args)       <br \/>{       <br \/>Console.WriteLine(&quot;***** Fun with Nullable Data *****\\n&quot;);       <br \/>DatabaseReader dr = new DatabaseReader();       <br \/>&#8230;       <br \/>\/\/ If the value from GetIntFromDatabase() is null,       <br \/>138 CHAPTER 4 n CORE C# PROGRAMMING CONSTRUCTS, PART II       <br \/>\/\/ assign local variable to 100.       <br \/>int? myData = dr.GetIntFromDatabase() ?? 100;       <br \/>Console.WriteLine(&quot;Value of myData: {0}&quot;, myData.Value);       <br \/>Console.ReadLine();       <br \/>} <\/li>\n<li>Defining Static Classes      <br \/>Since the release of .NET 2.0, the C# language expanded the scope of the static keyword by introducing       <br \/>static classes. When a class has been defined as static, it is not creatable using the new       <br \/>keyword, and it can contain only members or fields marked with the static keyword <\/li>\n<li>Understanding Read-Only Fields      <br \/>Closely related to constant data is the notion of read-only field data (which should not be confused       <br \/>with a read-only property). Like a constant, a read-only field cannot be changed after the initial       <br \/>assignment. However, unlike a constant, the value assigned to a read-only field can be determined       <br \/>at runtime, and therefore can legally be assigned within the scope of a constructor (but nowhere       <br \/>else). <\/li>\n<li>The sealed Keyword      <br \/>C# supplies another keyword, sealed, that prevents inheritance from occurring. When you mark a       <br \/>class as sealed, the compiler will not allow you to derive from this type. For example, assume you       <br \/>have decided that it makes no sense to further extend the MiniVan class:       <br \/>\/\/ This class cannot be extended!       <br \/>sealed class MiniVan : Car       <br \/>{       <br \/>} <\/li>\n<li>Sealing Virtual Members\n<p>sometimes you may not wish to seal an entire class, but simply want to prevent        <br \/>derived types from overriding particular virtual methods. For example, assume we do not want         <br \/>part-time salespeople to obtain customized bonuses. To prevent the PTSalesPerson class from overriding         <br \/>the virtual GiveBonus() method, we could effectively seal this method in the SalesPerson         <br \/>class as follows:         <br \/>\/\/ SalesPerson has sealed the GiveBonus() method!         <br \/>class SalesPerson : Employee         <br \/>{         <br \/>&#8230;         <br \/>public override sealed void GiveBonus(float amount)         <br \/>{         <br \/>&#8230;         <br \/>}         <br \/>}         <br \/>Here, SalesPerson has indeed overridden the virtual GiveBonus() method defined in the         <br \/>Employee class; however, it has explicitly marked it as sealed. Thus, if we attempted to override this         <br \/>method in the PTSalesPerson class:         <br \/>202 CHAPTER 6 n UNDERSTANDING INHERITANCE AND POLYMORPHISM         <br \/>sealed class PTSalesPerson : SalesPerson         <br \/>{         <br \/>public PTSalesPerson(string fullName, int age, int empID,         <br \/>float currPay, string ssn, int numbOfSales)         <br \/>:base (fullName, age, empID, currPay, ssn, numbOfSales)         <br \/>{         <br \/>}         <br \/>\/\/ No bonus for you! Error!         <br \/>public override void GiveBonus(float amount)         <br \/>{         <br \/>\/\/ Rats. Can&#8217;t change this method any further.         <br \/>}         <br \/>}         <br \/>we receive compile-time errors.<\/p>\n<\/li>\n<li>Obtaining Interface References: The as Keyword      <br \/>The second way you can determine whether a given type supports an interface is to make use of the       <br \/>as keyword, which was first introduced in Chapter 6. If the object can be treated as the specified       <br \/>interface, you are returned a reference to the interface in question. If not, you receive a null reference. <\/li>\n<li>Obtaining Interface References: The is Keyword <\/li>\n<li>The unsafe Keyword     <br \/>When you wish to work with pointers in C#, you must specifically declare a block of \u201cunsafe code\u201d      <br \/>using the unsafe keyword (any code that is not marked with the unsafe keyword is considered \u201csafe\u201d      <br \/>automatically). For example, the following Program class declares a scope of unsafe code within the      <br \/>safe Main() method:      <br \/>class Program      <br \/>{      <br \/>static void Main(string[] args)      <br \/>{      <br \/>unsafe      <br \/>{      <br \/>\/\/ Work with pointer types here!      <br \/>}      <br \/>\/\/ Can&#8217;t work with pointers here!      <br \/>}      <br \/>}<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>C# Parameter Modifiers Parameter Modifier Meaning in Life (None) If a parameter is not marked with a parameter modifier, it is assumed to be passed by value, meaning the called method receives a copy of the original data. out Output parameters must be assigned by the method being called (and therefore are passed by reference). &#8230; <a title=\"c#\u5b66\u4e60\u7b14\u8bb0\" class=\"read-more\" href=\"https:\/\/www.genepeng.com\/index.php\/232\" aria-label=\"More on c#\u5b66\u4e60\u7b14\u8bb0\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[56],"tags":[],"_links":{"self":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/232"}],"collection":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":3,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/232\/revisions\/236"}],"wp:attachment":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}