C # Advanced Programming (ii)-An explanation of core C #

Source: Internet
Author: User
Tags aliases case statement variable scope

Initialization and scope of variables

Initialization

C # has two methods to ensure that variables are initialized before they are used:

      • Variables are fields that, if not explicitly initialized, have a default value of 0 when these variables are created.

      • The local variable of the method must be explicitly initialized in the code before it can be used in the statement, and if his value is used before initialization, an error is made.

Scope

If the local variable scope conflicts, report CS0136 error.

field conflicts with local variables, hidden fields, access to hidden instance fields with the This keyword, static fields using Object.fieldname.

Constant

Characteristics of constants:

      • Constants must be initialized at declaration time, and changes will not be allowed after the value is specified.

      • The value of a constant must be used for calculations at compile time and cannot be extracted from a variable.

      • Constants are always static and are not allowed to be decorated with the static keyword.

The advantages of constants:

      • Easy to read

      • Easy to modify

      • Easy to avoid errors

Pre-defined data types

Value types and reference types

The difference between concept and storage method, refer to the first chapter.

Reference type

To create an object you must use the New keyword to assign a reference type to another reference type to get only its address, which changes as it changes. Such as:

         Test x, y;      X testx = new Test ();      Y testy = x; Testx.value =1;    The value of TESTX and testy after this statement is executed will be

CTS Type

The basic predefined types recognized by C # are not built into C #, but are embedded within the. NET Framework.

Decimal is not a built-in type in. NET formework, which can result in a performance penalty.

The bool type cannot represent false with a nonzero, and a non 0 represents true.

All built-in types inherit the object type.

The immutability of string: such as TESTX and testy in the above code if they are of type string, when the string pointed to by TESTX is changed, a new string is created in the managed heap and pointed to it, and the value pointed to by testy is not changed.

Flow control

Conditional statements

If, else if,else.

Switch statement

The value of the case statement must be a constant expression, and the variable is not allowed.

Difference from C + +: Activating the previous case does not automatically activate the latter case, and each case must be used to break the error otherwise; You can use a string as a test variable.

You can use the Goto label to jump through a line.

If case is empty, no break is required.

Cycle

The difference between for and while: A For a loop that is predictable in number of times, while a loop that is suitable for unpredictable times.

Foreach

To implement a foreach traversal, the IEnumerable interface must be implemented in the class.

You cannot change the value of each item in foreach.

Jump statement

Goto

Break

Jump out of the current loop

Continue

Skip this cycle.

Return

Exit Current method

Enumeration

Once the code is well-edited, the enumeration becomes the base type.

Name Space (namespace)

Unlike files or components, namespaces are a logical combination, not a physical combination.

The generic namespace is acceptable in CompanyName.ProjectName.StstemSection format.

Syntax for namespace aliases:

Using Slias = NamespaceName;

The namespace modifier is "::".

1//using System;  2 using Testsystem = System;  3   4 Namespace namespace alias  5 {  6 class program  7     {  8         static void Main (string[] args)  9         {Ten             Testsystem::console.writeline ("This is a C # progam");             TestSystem.Console.ReadKey ();     14}

Namespace Aliases Demo

Pre-compilation

#if #elif support A set of logical operators "! "," = = ","! + "," | | ".

C # Programming rules

Identifier rules

      • Can contain numeric characters, but they must start with a letter or an underscore.

      • You cannot use a C # keyword as an identifier.

User conventions

Rules should be the right decision, not a constraint.


      1. Naming conventions

Pascal: Name space, class, function name.

Camel: A field in a class (preferably with a "_" prefix can be more intuitive to know this is a field), the parameters of the method.


      1. Name Style

The name/naming style should be kept as consistent as possible.


      1. Name of the namespace

The namespace is. NET is the only way to differentiate between object names in a shared assembly.

Microsoft recommends using the following namespaces:<companyname>.<technologyname>


      1. Name and keywords

Slightly

Use of properties and methods

If the object looks like an object, it is represented by a property, namely:

      • Client health care can read its value, it is best not to use write-only properties.

      • Reading this value should not take him long.

      • There should be no negative effects.

      • You can set properties in any order.

      • Sequential read properties should also have the same effect.


Use of fields

The field should always be private.

2016-05-07 22:29:18

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.