C # String operations, nullable types, document annotations, nested types

Source: Internet
Author: User

String

String is an array of Unicode strings, and is immutable

This operation does not affect the original string, it will add a new copy.

About the split operation

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Reflection;usingSystem.Runtime.CompilerServices;usingSystem.Diagnostics;namespaceconsoleapp2{classProgram {Static voidMain (string[] args) {            stringS1 ="Hi there! this, is:a string."; Char[] delimiter = {' ','!',',','.',':' }; string[] Words =S1.            Split (delimiter, stringsplitoptions.removeemptyentries); foreach(stringSinchwords)            {Console.WriteLine (s); }        }    }}

StringBuilder class

The StringBuilder class can help you dynamically and efficiently produce strings, and avoid creating many replicas

The StringBuilder class is a member of the BCL, located in the Syste.text namespace, and is a mutable array of Unicode

After the StringBuilder object is created, the class allocates a buffer that is longer than the current string, and only the buffer can accommodate changes to the string without allocating memory

If it is larger than the current cache, a larger buffer is allocated, copying the string to the same buffer zone as the original.

Parse a string into a data value

All predefined simple types have a static method called Parse, which takes a string value representing the type and converts it to the actual value of the type

Each built-in type with the parse method has a TryParse method

The TryParse method accepts two parameters and returns a bool value

The first one is the string you want to convert

The second is an out parameter that points to the reference of the target variable

Returns True if TryParse succeeds, otherwise false

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Reflection;usingSystem.Runtime.CompilerServices;usingSystem.Diagnostics;namespaceconsoleapp2{classProgram {Static voidMain (string[] args) {            stringparseresultsummary; stringStringfirst =" -"; intIntfirst; BOOLSuccess =int. TryParse (Stringfirst, outIntfirst); Parseresultsummary= success?"Was successfully pared":"was not successfully parsed"; Console.WriteLine ("String {0} {1}", stringfirst,parseresultsummary); stringStringsecond ="vt750"; intIntsecond; Success=int. TryParse (Stringsecond, outIntsecond); Parseresultsummary= success?"Was successfully parsed":"was not successfully parsed"; Console.WriteLine ("String {0} {1}", Stringsecond, parseresultsummary); }    }}

Nullable types

A nullable type is always based on another declared type called the underlying type .

    • Nullable types can be created from any value type, including pre-defined simple types
    • Nullable types cannot be created from reference types or other nullable types
    • Declared nullable types can no longer be displayed in code, only variables of nullable types can be declared

To create a variable of a nullable type, simply add a question mark after the name of the underlying type in the variable declaration

Using a nullable type must ensure that the variable is not NULL, and attempting to read a null variable produces an exception

    • Use the same way
    • To detect whether a nullable type has a value, you can compare it to null or check his HasValue property

Using the empty Join operator

Consists of two consecutive question marks

Use a nullable user-defined type
struct mystruct{        publicint  x;          Public int y;          Public MyStruct (int xval,int  yval)        {            = xval;             = yval;        }}

Static void Main (string[] args) {            mystructnew mystruct (5);            Console.WriteLine (msnull.value.x);}
Nullable<t>

Main method

The first two forms do not return a value to the execution environment after the program terminates, and the latter two return int,0 usually indicate success.

Parameters can have 0 or more, even if there are no arguments, the args parameter is not NULL, but an array with no elements

Document comments

Nested types
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Reflection;usingSystem.Runtime.CompilerServices;usingSystem.Diagnostics;namespaceconsoleapp2{classMyClass {classMyCounter { Public intCount {Get;Private Set; }  Public StaticMyCounteroperator++(MyCounter current) {current. Count++; returnCurrent ; }        }        PrivateMyCounter counter;//fields of a nested class type         PublicMyClass () {counter =NewMyCounter (); }//constructor Function         Public intINCR () {return(counter++). Count; }//constructor Function         Public intGetValue () {returnCounter. Count; }//Get Count value    }    classProgram {Static voidMain () {MyClass MC=NewMyClass (); Mc. INCR (); Mc. INCR (); Mc.            INCR (); Mc. INCR (); Mc. INCR (); Mc.            INCR (); Console.WriteLine (MC.        GetValue ()); }    }}

    • Members of a nested type always have full access to members of an enclosing type
    • Members of the enclosing type
      • The nested type itself can always be accessed
      • You can access only nested type members that have access rights declared

The visibility of nested types also affects the inheritance of base class members, and if the enclosing type is a derived class, nested types can uniformly use the same name to hide the members. You can use the new modifier to explicitly-type hiding on a declaration of a nested type.

The this reference in a nested type refers to an object of a nested type. if an object of the nested type requires access to the enclosing type, it must hold a reference to the enclosing type. The following code indicates. Pass the This reference provided by the enclosing object as a parameter to the constructor of the nested type

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Reflection;usingSystem.Runtime.CompilerServices;usingSystem.Diagnostics;namespaceconsoleapp2{classSomeClass//Closed Class    {        intField1 = the, Field2 = -;//fields for enclosing classesmynested MN =NULL;//a reference to a nested class         Public voidprintmymembers () {mn. Printoutermembers ();//calling a method in a nested class        }         PublicSomeClass ()//constructor Function{mn=NewMynested ( This);//Create a nested class instance this passes a reference to the enclosing type        }        classmynested//Nested class declarations{SomeClass sc=NULL;//enclosing a reference to a class             Publicmynested (SomeClass SC)//constructors for nested classes{SC= SC;//storing references to nested classes            }             Public voidPrintoutermembers () {Console.WriteLine ("Field1: {0}"Sc. FIELD1);//Closed FieldConsole.WriteLine ("Field2: {0}"Sc. FIELD2);//Closed Field            }        }    }    classProgram {Static voidMain () {SomeClass MYSC=NewSomeClass ();        Mysc.printmymembers (); }    }}

C # String operations, nullable types, document annotations, nested types

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.