Chapter 2: C # BASICS (c # advanced programming version 6th) (unfinished)

Source: Internet
Author: User
Tags finally block variable scope
1. You can use the csc.exe compiler to compile the source file.
  • The most convenient way is to start running-> Program-> VS Tools-> VS command prompt
    Run in the directory:
    Csc fileName. cs
  • Or, after the corresponding environment variables are configured, run the preceding command in the cmd window.
  • To compile the file into a dll:
    Csc/t: library fileName. cs
  • If you want to introduce dll during compilation:
    Csc fileName. cs/r: dllName. dll
2. C # variable names are case sensitive 3. C # handle "uninitialized variables" as errors
  • Variables are fields in a class or structure. If Initialization is not displayed, the default value is always initialized when these variables are created.
  • A variable is a local variable of the method. It must be initialized before it can be used. Otherwise, an error occurs.
4. Declare a variable to create only a reference for the object. But this reference does not point to any object (different from c ++) 5. Scope conflict of Variables
  • Scope conflict between local variables

    • The variable declared in the loop statement. The scope is only in the loop body.
    • Variables with the same name cannot be declared twice in the same scope (that is, variables are not allowed to be hidden as c ++)
  • Field and local variable scope conflict
    Using System; namespace Wrox. proCSharp. basics {class ScopeTest2 {static int j = 20; // field Console. writeLine (j); public static void Main () {int j = 30; // local variable Console. writeLine (j); return ;}}}
    • Output result: 30
6. Constants
  • Constants must be initialized during declaration.
  • The initialized value cannot be extracted from a variable (if you want to do so, use a read-only field)
  • Constants are always static and cannot be modified using static
  • Example
    Const int a = 100;
7. Value Type and reference type
  • Value Type: stored on the stack
  • Reference Type: stored on the managed stack (mutual value assignment only modifies the reference and does not generate another value)
8. String
  • Although String is a referenced variable, it is different from the general reference type, such as code:
    using System;class StringExample{   public static int Main()   {      string s1 = "a string";      string s2 = s1;      Console.WriteLine("s1 is " + s1);      Console.WriteLine("s2 is " + s2);      s1 = "another string";      Console.WriteLine("s1 is now " + s1);      Console.WriteLine("s2 is now " + s2);      return 0;   }}
    • Output result:
      S1 is a string
      S2 is a string
      S1 is now another string
      S2 is a string
    • That is, the changes to S1.
    • The process is as follows: when s1 is created and initialized, A New string object is allocated to the stack. When s2 is initialized, the s2 reference also points to this object. However, when the s1 value is changed, instead of being replaced, a new object will be allocated to the new value on the stack, and the s2 variable still points to the original object.
9. Switch statement
  • Example

     switch (integerA)            {                case 1:                    // do something                    break;                case 2:                    // do something                    break;                default:                    // do something                    break;            }

  • The value of case must be a constant expression.
  • If the case clause does not contain the break or goto jump statement, the compilation is incorrect.
  • Exception: if a case clause is null, you can jump from this case to the next case.
  • The discharge sequence of the case clause is irrelevant, but there cannot be case statements with the same value
  • Different from c ++, strings can be used as test variables.
10. The foreach loop cannot change the values of items in the set. To change the values, use the for loop 11. goto jump statement.
  • Example: jump to a user-specified tag

    goto label1;            // other code        label1:            Console.WriteLine(" jump to here...");
  • Restrictions
    • You cannot jump to a code block like a for loop.
    • Cannot jump out of the class range
    • Cannot exit to try... Finally block after catch Block
  • Benefit: jump between the case clauses of the switch statement
12. Enumeration type
  • Example

    using System;namespace Wrox.ProCSharp.Basics{public enum TimeOfDay{   Morning = 0,   Afternoon = 1,   Evening = 2}   class EnumExample{   public static int Main()   {      WriteGreeting(TimeOfDay.Morning);      return 0;   }   static void WriteGreeting(TimeOfDay timeOfDay)   {      switch(timeOfDay)      {         case TimeOfDay.Morning:            Console.WriteLine("Good morning!");            break;         case TimeOfDay.Afternoon:            Console.WriteLine("Good afternoon!");            break;         case TimeOfDay.Evening:            Console.WriteLine("Good evening!");            break;         default:            Console.WriteLine("Hello!");            break;      }   }}}

  • Extract enumerated values from strings
    TimeOfDay time = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "afternoon", true);            Console.WriteLine((int)time);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.