C # basic programming and development knowledge,

Source: Internet
Author: User

C # basic programming and development knowledge,

In order to help you remember some basic C # And take notes based on examples, it is convenient for beginners to take notes after learning and to increase the basic memory.

// Enumeration type

// Can be defined under namespace or under class
Using System;
Namespace test1 {
??? // Defines the enumeration type
???? Enum GameState: byte // modify the storage type of this enumeration type. The default value is int {
??????? // You can also modify it, pause = 100, and add one for the rest.
??????? Pause ,?? // The default value is integer 0.
??????? Failed ,? // The default value is integer 1.
??????? Success, // The default value is Integer 2.
??????? Start ??? // The default value is Integer 3.
??? }
??? Class Program {
??????? Static void Main (){
??????????? /// Declare a variable using the defined enumerated type
??????????? // GameState state = GameState. Success;
??????????? //// Comparison of enumeration types
??????????? // If (state = GameState. Start)
??????????? //{
??????????? //??? Console. WriteLine ("the current game is in the starting state! ");
??????????? //}
??????????? // Console. WriteLine (state );
??????????? GameState state = GameState. Start;
??????????? Int number = (int) state;
??????????? Console. WriteLine (number );
??????????? Console. ReadLine ();???
??????? }}}
// Struct
// Can be defined under namespace or class
// Use struct to make the program clearer
// Several types are converted into one type
// You must learn to classify things
Using System;
Namespace test1 {
??? // Defines a struct.
??? Struct Position {
??????? Public float x;
??????? Public float y;
??????? Public float z;
??? }
??? Enum Direction {
??????? West,
??????? North,
??????? East,
??????? South
??? }
??? Struct Path {
??????? Public float distance;
??????? Public Direction dir;
??? }
??? Class Program {
??????? Static void Main (){
??????????? // When using struct to declare a variable, it is equivalent to using all the variables in the struct to declare
??????????? Position enemy1Position;
??????????? Enemy1Position. x = 34;
// You can access the specified variable in the struct by adding an attribute name.
??????????? Path pa;
??????????? Pa. distance = 1000;
??????????? Pa. dir = Direction. East;
??????????? Console. ReadKey ();
???? }}}
// Array
// Unlike other languages, [] is placed after the array type
// An error is reported when an index does not exist.
Using System;
Namespace test1 {
??? Class Program {
??????? Static void Main (){
??????????? Int [] scores;
??????????? Scores = new int [4];
??????????? Scores = new int [] {23,12, 45,13 };
??????????? For (int I = 0; I ??????????? {
??????????????? Console. WriteLine (scores [I]);
??????????? }
??????????? // Foreach retrieves the elements in the array in sequence, assigns them to temp, assigns them to temp, and then executes the loop body
??????????? Foreach (int temp in scores)
??????????? {
??????????????? Console. WriteLine (temp );
??????????? }
??????????? Console. ReadKey ();
???? }}}
// String processing
// Name. Length returns the Length of a string or array.
// Name. ToLower (): converts the string to lowercase and returns the result, without affecting the original string.
// Name. ToUpper (): converts the string to uppercase and returns the result, without affecting the original string.
// Name. Trim (): removes spaces before and after the string, which has no effect on the original string.
// Name. TrimStart (): removes spaces in front of the string and does not affect the original string.
// Name. TrimEnd (): removes spaces after the string, which has no effect on the original string.
// Name. Split ('Char '): splits the original string according to the specified character to obtain a Split string array.
Using System;
Namespace test1 {
??? Class Program {
??????? Static void Main (){
??????????? String str1 = "WWW.baidu.com ?? ";
??????????? Int num = str1.Length;
??????????? Foreach (char temp in str1)
??????????? {
??????????????? Console. Write (temp );
??????????? }
??????????? // String res = str1.ToLower ();
??????????? // String res = str1.ToUpper ();
??????????? // String res = str1.Trim ();
??????????? // String res = str1.TrimStart ();
??????????? // String res = str1.TrimEnd ();
??????????? String [] res = str1.Split ('.');
??????????? Console. WriteLine (str1 );
??????????? Foreach (string temp in res)
??????????? {
??????????????? Console. WriteLine (temp );
??????????? }
??????????? Console. ReadKey ();
???? }}}
// Define a random number ranging from 0 to 50
Random number = new Random ();
Int number1 = number. Next (0, 51 );
// Fast sorting, using a fast algorithm
Array. Sort (name); name = Array
// Array Function with the number of unknown parameters
Using System;
Namespace test1 {
??? Class Program {
??????? // An array of parameters is defined here, and the number of parameters is unknown.
??????? Static int Plus (params int [] array ){
??????????? Int sum = 0;
??????????? For (inti = 0; I ??????????????? Sum + = array [I];
??????????? }
??????????? Return sum;
??????? }
??????? Static void Main (){
??????????? Int sum = Plus (1, 2, 3 );
??????????? Console. WriteLine (sum );
??????????? Console. ReadKey ();
???? }}}

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.