C # Learning Notes First---C # basics (type and underlying syntax)

Source: Internet
Author: User
Tags logical operators

First, the data types are divided into value types and reference data types, and pointer types (not currently involved): value types are stored directly on the stack of memory, reference types store a reference on the stack, and store specific values on the heap.

Value types are divided into built-in types and user-definable struct types, enum types, which inherit from system. The ValueType class, while the system. ValueType inherits from the System.Object class, which is the base class for all classes.

value types such as int and bool are commonly used aliases, formally named system. Int32, System. Boolean.

You can also use VAR to name a variable, so you can temporarily not determine his data type.

A struct is similar to a sort type in Java, and is used to customize a class that can contain different attributes;

Enum enum type is equivalent to setting a sequence number, the variable can only take values in the value sequence, each value automatically defaults to an int value starting from 0, but can be manually forced to start from another or to force other data types.

Give a chestnut: days:short {Monday = 5, Tuesday, Wenesday, Thursday, Friday, Saturday, Sunday}

The definition of struct and enum type should be written in the Main method body!!!! End no semicolon!!!!

A reference type is first of three built-in types: String, object, dynamic. Where dynamic enforces type checking at run time rather than during the compile phase.

The two assignment methods of string type can get the same value, but the cost at the bottom is different (two methods: String a= "ABCDEFG"; string b= "ABCD"; b+= "EFG"), the first is recommended, the second is more expensive for system requirements, Values that need to be modified frequently can use the StringBuilder type to obtain variable-length memory space.

Focus: StringBuilder method AppendFormat Use method

StringBuilder s = new StringBuilder ();
S.appendformat ("hello{1}{0}", "LD", "wor");

You can assign a value of S to HelloWorld.

Next is the class type, which can contain member variables and internal methods, the default access modifier is private, the external is inaccessible, and can be manually modified to public. The Get Set method of C # is not a separate method but instead uses the keyword get set, which directly invokes the Get Set keyword to use the corresponding method, and the static method binds to the class name, which is not accessible with the instantiated object, but directly to the class name.

The interface interface can only declare methods, there is no specific implementation method, the subclass must implement the method in the interface.

Abstract class abstraction classes cannot be instantiated, but methods can be defined internally, subclasses can invoke methods within the abstract class by default, or they can override the method with the same name within themselves to implement overloads of the method. An interface can not contain member variables, and an abstract class can have its own member variable, and the quilt class inherits.

Classes are familiar after the class is transformed, the type conversions in C # are divided into display transformations and implicit conversions, C # is a strongly typed language, and the type is checked at compile time.

Implicit conversions are applied to classes with a smaller range to a larger range, no loss of value, and a display conversion for a class with a larger range, but easily resulting in loss of accuracy.

There are two important keyword as,is in the type conversion.

IS is used to determine whether it can be converted to a class, the output is ture or false, respectively, it is possible to convert to the target type, if it is ture, you can use as for the conversion, but as can only be used for reference type and non-null type, if the conversion for value type will be an error, Because converting from a large class to a small class assigns a null value to the small class, and the value type cannot be empty.

Conversion of value types can use the method under the Convert class or the Parse method or the TryParse method under the corresponding class, and a default value is assigned when the TryParse method is used to convert a value that cannot be converted. You can also use the inherited interface iconventible or typeconventer to customize your own type conversion method.

The conversion of value types and reference types is often referred to as boxing and unboxing, and boxing is implicit conversions, no special syntax is required, and unpacking can be implemented by displaying transformations.

A common problem with unpacking is that a non-nullable type is assigned a null value, which can be an error, and the workaround is to add the variable name when the variable is named. To make this value nullable, the same meaning of the naming method is also system.nullable<int> i=10, this I can be assigned to NULL, but the reading will be error, the solution is to use HasValue to take the value, empty will return false, Or, using the method Getvalueordefalt, the null value returns the default value. Modify the default value to use double question marks: int k=i?? 20; When I is empty, the value of K is 20, if I have a value then the normal assignment.

Next is the array, which is a reference type and all the arrays inherit from the base class system. Array, which is an abstract class that cannot be instantiated.

One-dimensional array int[] numbers=new int[5];

Two-dimensional array int[,] numbers=new int[5,4];

Array of arrays int[][] numbes=new int[5][4];

An array of arrays is also called a jagged array, and each row can have a different length.

All of these are fixed-length arrays, and the lengths of all arrays are determined at the time of definition, and if you want to use a variable-length array that can use the ArrayList array list, the array list can store different types and easily cause confusion, and it is recommended that you use list,list to define the intrinsic value types of the generic type. The notation is as follows list<int> intlist=new list<int> (); This list can only use values of type int.

Above is the use of the location to access the values in the list, while using the Hashtable hash table can define each value of the key,hashtable no mandatory type, if mandatory, you can use dictionary to enforce the provisions, the following wording dictionary <string,string> d=new dictionary<string,string> (); enforces that both key and value are string types. Hashtable returns a null value when accessing a nonexistent key, and an error in dictionary. However, the dictionary thread is not secure and it is recommended to use Concurrentdictionary in multi-threading. There are also SortedList, which automatically sorts the array according to the key value, and the stack array is an advanced one, similar to that of a heap; the queue is an FIFO array.

Then the underlying syntax in C #--Logical statements

There is no difference between logical operators and other languages, and it is particularly possible to omit the curly braces if the program has only one line after the if judgment, but it is not recommended for programming purposes.

One of the more interesting loops is the use of a foreach loop, which is a handy way to use this loop, which must be a collection type to implement the IEnumerable interface.

List<int> iss=new list<int> () {A-i};

foreach (int i in ISS)

{

Console.WriteLine (i);

}

Console.ReadLine ();

This loops out each value in ISS.

C # Learning Notes First---C # basics (type and underlying syntax)

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.