C # first basic series

Source: Internet
Author: User

I recorded my notes during my learning process. I just shared my notes so that many people who are just learning. net programming can quickly learn C #.

  1. Managed code, unmanaged code

(1) Any code generated by. net that requires the CLR to run is managed code

(2) Execution Code not maintained by CLR is unmanaged code

  1. Difference between break and Continue

(1) Example:

Static void Main (string [] args)

{

For (int I = 0; I <10; I ++)

{

If (I = 2)

{

// Break;

Continue;

}

}

}

Note: You can use the debugging function to check the structure.

  1. How to exchange Variables

Int num1 = 5;

Int num2 = 10;

(1) first writing method (it is recommended to use this best method)

Int temp = num1;

Num1 = num2;

Num2 = temp;

(2) method 2

Num1 = num1 + num2;

Num2 = num1-num2;

Num1 = num1-num2;

(3) method 3

Num1 = num1 | num2;

Num2 = num1 ^ num2;

Num1 = num1 ^ num2;

(4) Method 4 (only stack-based compilation can be performed)

Num1 = num2 + (num2 = num1) * 0;

(5) Method 5 (when using this method, make sure that neither of the two variables is 0)

Num1 = num1 * num2;

Num2 = num1/num2;

Num1 = num1/num2;

Console. WriteLine ("num1 = {0}, num2 = {1}", num1, num2 );

  1. Method (the method is to reuse the module)

(1) methods without parameters and return values

Syntax: [public] Name of the void method ()

{

// Method body

}

Public static void Func ()

{

Console. WriteLine ("I want to do the website ");

}

(2) If the Main method calls this method, you must add static (static) methods before the method)

(3) methods with Parameters

Syntax: public static void method name (variable name of the method type parameter)

{

// Method body

}

Public static void FuncReplace (int money)

{

If (money >=1000)

{

Console. WriteLine ("available websites ");

}

Else if (money >= 500)

{

Console. WriteLine ("simple website ");

}

Else

{

Console. WriteLine ("Capital Shortage ");

}

}

(4) methods with parameters with return values

Syntax: [public] Method Name of the static return value type (parameter type parameter)

{

//

}

Public static int GetNum (int num)

{

Return num * 2;

}

Execution Method

Int res = GetNum (3 );

Console. WriteLine (res );

(5) Implement the Max and Min Methods

Class Program

{

Static void Main (string [] args)

{

Int num1 = 12;

Int num2 = 34;

Int max = Max (num1, num2 );

Int min = Min (num1, num2 );

Console. WriteLine ("the larger number is: {0}", max );

Console. WriteLine ("the smaller number is {0}", min );

Console. ReadKey ();

}

Public static int Max (int n1, int n2)

{

/*

If (n1> n2)

{

Return n1;

}

Else

{

Return n2;

}

*/

Int num = 0;

If (n1> n2)

{

Num = n1;

}

Else

{

Num = n2;

}

Return num;

}

Public static int Min (int n1, int n2)

{

Return n1> n2? N2: n1; // ternary expression

}

}

(6) Calculate the sum of the odd values between Min and max.

Class Program

{

Static void Main (string [] args)

{

Int res = SumOdd (3, 7 );

Console. WriteLine (res );

Console. ReadKey ();

}

Public static int SumOdd (int min, int max)

{

Int res = 0;

For (int I = min; I <= max; I ++)

{

If (I % 2! = 0)

{

Res + = I;

}

}

Return res;

}

}

(7) calculate the maximum number

Class Program

{

Static void Main (string [] args)

{

Int [] nums = {12, 34, 54, 65, 76,454 };

Int res = GetNUum (nums );

Console. WriteLine (res );

Console. ReadKey ();

}

Public static int GetNUum (int [] nums)

{

Int max = 0;

For (int I = 0; I <nums. Length; I ++)

{

If (nums [I]> max)

{

Max = nums [I];

}

}

Return max;

// Return nums. Max ();

}

}

(8) write three methods to complete

1. Input any integer without parameters. If an error is prompted and the input continues, an integer is returned.

Int InputNum_1 ()

2. Complete the integer input from 0 to this number range with a parameter

Int InPutNum_2 (int max)

3. Complete the integer input between the two numbers with two parameters

Int InPutNum_3 (int min, int max)

Class Program {static void Main (string [] args) {Console. write ("enter a number:"); int num = GetInputNum (9, 17); Console. writeLine (num * 2); Console. readKey ();} /// <summary> /// enter any number in the int range and return /// </summary> /// <returns> </returns> public static int GetInputNum () {return GetInputNum (int. minValue, int. maxValue) ;}/// <summary> /// complete the int type data input and return, the input number must be between 0 and the given number. /// </summary> /// <param name = "max"> the upper limit of the given number </param> // /<returns> </returns> public static int GetInputNum (int max) {return GetInputNum (0, max) ;}/// <summary> /// enter the int number, between the specified range, /// </summary> /// <param name = "min"> deprecate the specified range </param> /// <param name = "max "> launch within a specified range </param> // <returns> </returns> public static int GetInputNum (int min, int max) {string str = Console. readLine (); int num; while (true) {try {num = Convert. toInt32 (str); if (num> min & num <max) {break;} Console. write ("the input number is no longer between {0} and {1}. Please enter again", min, max); str = Console. readLine ();} catch {Console. write ("incorrect input, please enter again"); str = Console. readLine () ;}} return num ;}}

  

  1. Add code snippet by yourself

(1) how to create your own code segment

1) tool-> code segment Manager-> switch to C #-> copy path in my computer-> then you can modify the file into a file and put it on the desktop following the code segment inherent in my computer.

(2) how to add custom code segments

2) Open the original interface-> Import-> put it under your own file-> complete

  1. Method Overloading

(1) Overloading means that different methods of the same type or similar functions are unified into the same method name for programmers to call.

(2) method Overloading is essentially a different method, except that the method name is the same, so the parameters provided during method calling are automatically judged by the compiler.

(3) Conditions for Heavy Loads

1) Remove all content before the method name

2) Remove the method body.

3) Remove the parameter name of the method parameter and leave the type name

4) comparison, which can overlap cannot constitute heavy load

  1. Enumeration refers to the data obtained one by one.

(1) Syntax:

[Public] enum enumeration name

{

// Enumerate members

}

(2) how to define Enumeration

(3) enumeration is a type (*)

(4) How to declare enumerated Variables

(5) how to assign values to and use enumeration Variables

Public enum Direction

{

East,

South,

West,

North

}

Class Program

{

Static void Main (string [] args)

{

Direction dir = Direction. North;

Console. WriteLine (Direction) 3 );

Console. ReadKey ();

}

}

  1. Structure

(1) Syntax:

[Public] struct structure name

{

// Structure member

}

The structure member can be "field", "method", and "constructor"

(2) methods for defining structures

(3) add public if the structure field requires external access

(4) Definition of display not allowed non-parameter Constructor

(5) The definition constructor must assign values to each field.

(6) After a constructor with parameters is provided, the constructor without parameters still exists.

(7) When declaring Structure Variables, you can skip the constructor.

1) if no constructor is used, you can only call fields that have been assigned a value.

2) The structure is constructed to assign the initial value to the field.

(8) The structure member cannot have an initial value, unless it is modified by static or consit.

(9) The structure can inherit the self-structure

(10) generally, the structure of object-oriented features is not required.

(11) The structure is used when a small number of fields are encapsulated.

  1. Winform Application

(1) how to create

1) create a project

2) drag controls (modify attributes)

3) add events

Method triggered by executing an action

(2) Winform

1) Change the interface (usually through)

2) Business Logic

(3) Position of the form (controlling the position of the form)

1) set the StartPosition attribute of the form.

(4) move the mouse

Write the following code under the MouseMove event: Text = "X =" + e. X + ", Y =" + e. Y;

(5) form scrolling

Public partial class Form1: Form {public Form1 () {InitializeComponent (); StartPosition = FormStartPosition. manual; Location = new Point (0,100);} int step = 5; int dis = 1; private void timer1_Tick (object sender, EventArgs e) {// loop implementation // if (Location. *> 1366) // {// Location = new Point (0-Size. width, Location. y); //} // Location = new Point (Location. X + 5, Location. y); // returns in the past // if (Location. X> 1366-Size. width | Location. X <0) // {// step =-step; //} // Location = new Point (Location. X + step, Location. y); // fast on both sides, slow in the middle // determine the distance between my form and the margin int left = Location. x; int right = 1366-Location. x-Size. width; if (Location. x> 1366-Size. width | Location. X <0) {step =-step;} if (step> 0) {// right float dis = right/10 = 0? 1: right/10;} else {// left floating dis = left/10 = 0? 1: left/10;} Location = new Point (Location. X + step * dis, Location. Y );}}

  

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.