C # Learn the third day

Source: Internet
Author: User
Tags array definition modifiers

1. Constants:
Methods for declaring constants:
Const constant (note to capitalize)//The left of the assignment number must be a variable, and the constant cannot be re-assigned
/* Do not want to be changed the amount is generally set as constant */
2. Enumeration
1). Syntax:
[Pubic] enum enum name
{

Value of 1,
Value of 2,
Value of 3,
.......

}
Public: access modifier, optional
Enum: Enumeration method
2). Enumeration meaning
In order to solve the difference between the definitions of the project, more people play together, need to unify the name of the thing to play
For example: gender
char gender = "male";
String s1= "men";
String s2= "female";
/* enum declared below the namespace, outside of the class, representing this namespace, all classes can be used */
/* Nature is variable, so-called multiple variables */
-Examples of applications such as
public enum Gender
{
Man
Woman
}
static void Main (string[] args)
{
int n=10;
Gender Gender=gender. Male;

}
3). Type conversion:
(1)--Conversion of enum type and int type (two types compatible)
int n= (int) "male";//return value is 0 (enum type default starting from 0) haha, like double
(2)--with string conversion
int n1=1;
Gender gender=gender.n1;//return value is "female"
/* All types can call the ToString method to convert to a string, but the string cannot be converted directly to an enumeration type */
(3)--Call the parse () method
Gender n= (Gender) Enum.parse (typeof (Gender), s),//convert S to Gender type, and N accept
/* If the enumeration does not work like s, it throws an exception (according to the old method try--catch--statement) */
3. Structure
Can help us declare several different types of variables at once
public struct person
{
public string _name;/* can only be added in the structure, but now it becomes a field, not a variable, which is stored
Multiple values, a variable can hold only one value
General requirements specification underlined to be the same as the variable */
public int _age;
public Char _gender;
}
Similar to arrays in Python,----but completely different
Person ara;//In fact, only one variable ARA is declared here, making its type part of the structure
Ara._name= "Ara";
ara._age=20;
Ara._gender= "Man";
4. Arrays
can help us to store multiple variables of the same type at once (data)//similar to arrays in Python
Grammar:
1) array definition of numbers
Int[] Nums=new int[10];
*/* Note that in C #, you first define the array length (that is, open up the memory space),
Well----first set up the whole, to memory has certain reason, conforms to the C family's consistent style
Each variable in the array is an element in the group, and the initial value is 0
To access each element, you need the ordinal action for the element (ordinal from zero, haha, like Python)
*/
By looping, note that you cannot assign a value more
for (int i=0;i<nums.length;i++);
{
Nums[i]=i;

}
Of course, you can also take a value by loop
for (int i=0;i<nums.length;i++);
{
Console.WriteLine (Nums[i]);
}
2) string (initial value is null, that is, there is no space) and Boolean type (initial value is false) array definition
3) How arrays are declared
Int[] nums={1,2,3}
Int[] Nums=new int[3]{1,2,3}
Exercise: Remove the largest integer and the smallest integer from an array of integers
Declares an array of type iny and arbitrarily assigns an initial value
int nums={1,2,3,4,5,6,7,8,8,9}//defines an array
The int max=nums[0];//declares that the variable stores the maximum value (and can also be written as int max=int. MinValue (assigns the smallest value represented by an integer type to max) There's a function like this.
int mon=num[0];//declares a variable to store the minimum value
int sum=ave=0;
for (int i=0;i<nums.length;i++);
{
if (Nums[i]>max)
{
Max=nums[i];
}
if (nums[i]>min)
{
Min=nums[i];
}
Sum+=nums[i];
Ave=sum/nums.length;
}
Console.WriteLine ("The maximum value in the array is {0}, the minimum value is {1}, the sum is {2}, the average is {3}", Max,min,sum,ave);
Consolo.readkey ();
Exercise: Split the name with "|"
String[] names={"Lao Yang", "Old Su"};
String Str=null;
for (int i=0;i<names.length-1;i++)
{
str+=names[i]+ "|"
}
Console.WriteLine (Str+names[names.length-1]);
Console.readkey ();
5. Bubble sort
Arranges elements in an array in order from small to large or from large to small
Int[] nums={122,,22,33,23,2,34,54,1223}
Compares the first element to each subsequent element
Sort by special method Array.Sort (Nums) (only in ascending order)
Array.reverse (Nums);
for (int i=0;i<nums.length;i++)
{
for (int j=0;j<nums.length-1-i;j++)
{
if (nums[j]>nums[j+1])
{
int temp =NUMS[J];
NUMS[J]=NUMS[J+1];
NUMS[J+1]=NUMS[J];
}
}
}
for (int i=0;i<nums.length;i++)
{
Console.WriteLine (Nums[i])
}
Console.readkey ();
6. Methods
public static return value type method name ([parameter list])
{
Method body;
}
Public: access modifiers, public, common
Static: Statically
Return value type: If not, write void (that is, you do not need a variable to receive, but can wrap)
Method Name: psical, all words capitalized, the remaining lowercase
Parameter list: The required conditions, if not, the parentheses must be written
Return: Ends This method immediately, returning the value to return
* If you want to be executed, you must execute it in the main () function
* Called when [type variable = class name.] Method Name ([parameter list])
* Call an unknown function, and a parenthesis will display the conditions that should be entered
In some cases, the class name can be omitted, under the same class
Example
public static int Getmax (int n1,int n2)
{
Return n1>n2?n1:n2;

}
int A=program. Getmax (1,3);

1. Constants:
Methods for declaring constants:
Const constant (note to capitalize)//The left of the assignment number must be a variable, and the constant cannot be re-assigned
/* Do not want to be changed the amount is generally set as constant */
2. Enumeration
1). Syntax:
[Pubic] enum enum name
{

Value of 1,
Value of 2,
Value of 3,
.......

}
Public: access modifier, optional
Enum: Enumeration method
2). Enumeration meaning
In order to solve the difference between the definitions of the project, more people play together, need to unify the name of the thing to play
For example: gender
char gender = "male";
String s1= "men";
String s2= "female";
/* enum declared below the namespace, outside of the class, representing this namespace, all classes can be used */
/* Nature is variable, so-called multiple variables */
-Examples of applications such as
public enum Gender
{
Man
Woman
}
static void Main (string[] args)
{
int n=10;
Gender Gender=gender. Male;

}
3). Type conversion:
(1)--Conversion of enum type and int type (two types compatible)
int n= (int) "male";//return value is 0 (enum type default starting from 0) haha, like double
(2)--with string conversion
int n1=1;
Gender gender=gender.n1;//return value is "female"
/* All types can call the ToString method to convert to a string, but the string cannot be converted directly to an enumeration type */
(3)--Call the parse () method
Gender n= (Gender) Enum.parse (typeof (Gender), s),//convert S to Gender type, and N accept
/* If the enumeration does not work like s, it throws an exception (according to the old method try--catch--statement) */
3. Structure
Can help us declare several different types of variables at once
public struct person
{
public string _name;/* can only be added in the structure, but now it becomes a field, not a variable, which is stored
Multiple values, a variable can hold only one value
General requirements specification underlined to be the same as the variable */
public int _age;
public Char _gender;
}
Similar to arrays in Python,----but completely different
Person ara;//In fact, only one variable ARA is declared here, making its type part of the structure
Ara._name= "Ara";
ara._age=20;
Ara._gender= "Man";
4. Arrays
can help us to store multiple variables of the same type at once (data)//similar to arrays in Python
Grammar:
1) array definition of numbers
Int[] Nums=new int[10];
*/* Note that in C #, you first define the array length (that is, open up the memory space),
Well----first set up the whole, to memory has certain reason, conforms to the C family's consistent style
Each variable in the array is an element in the group, and the initial value is 0
To access each element, you need the ordinal action for the element (ordinal from zero, haha, like Python)
*/
By looping, note that you cannot assign a value more
for (int i=0;i<nums.length;i++);
{
Nums[i]=i;

}
Of course, you can also take a value by loop
for (int i=0;i<nums.length;i++);
{
Console.WriteLine (Nums[i]);
}
2) string (initial value is null, that is, there is no space) and Boolean type (initial value is false) array definition
3) How arrays are declared
Int[] nums={1,2,3}
Int[] Nums=new int[3]{1,2,3}
Exercise: Remove the largest integer and the smallest integer from an array of integers
Declares an array of type iny and arbitrarily assigns an initial value
int nums={1,2,3,4,5,6,7,8,8,9}//defines an array
The int max=nums[0];//declares that the variable stores the maximum value (and can also be written as int max=int. MinValue (assigns the smallest value represented by an integer type to max) There's a function like this.
int mon=num[0];//declares a variable to store the minimum value
int sum=ave=0;
for (int i=0;i<nums.length;i++);
{
if (Nums[i]>max)
{
Max=nums[i];
}
if (nums[i]>min)
{
Min=nums[i];
}
Sum+=nums[i];
Ave=sum/nums.length;
}
Console.WriteLine ("The maximum value in the array is {0}, the minimum value is {1}, the sum is {2}, the average is {3}", Max,min,sum,ave);
Consolo.readkey ();
Exercise: Split the name with "|"
String[] names={"Lao Yang", "Old Su"};
String Str=null;
for (int i=0;i<names.length-1;i++)
{
str+=names[i]+ "|"
}
Console.WriteLine (Str+names[names.length-1]);
Console.readkey ();
5. Bubble sort
Arranges elements in an array in order from small to large or from large to small
Int[] nums={122,,22,33,23,2,34,54,1223}
Compares the first element to each subsequent element
Sort by special method Array.Sort (Nums) (only in ascending order)
Array.reverse (Nums);
for (int i=0;i<nums.length;i++)
{
for (int j=0;j<nums.length-1-i;j++)
{
if (nums[j]>nums[j+1])
{
int temp =NUMS[J];
NUMS[J]=NUMS[J+1];
NUMS[J+1]=NUMS[J];
}
}
}
for (int i=0;i<nums.length;i++)
{
Console.WriteLine (Nums[i])
}
Console.readkey ();
6. Methods
public static return value type method name ([parameter list])
{
Method body;
}
Public: access modifiers, public, common
Static: Statically
Return value type: If not, write void (that is, you do not need a variable to receive, but can wrap)
Method Name: psical, all words capitalized, the remaining lowercase
Parameter list: The required conditions, if not, the parentheses must be written
Return: Ends This method immediately, returning the value to return
* If you want to be executed, you must execute it in the main () function
* Called when [type variable = class name.] Method Name ([parameter list])
* Call an unknown function, and a parenthesis will display the conditions that should be entered
In some cases, the class name can be omitted, under the same class
Example
public static int Getmax (int n1,int n2)
{
Return n1>n2?n1:n2;

}
int A=program. Getmax (1,3);

1. Constants:
Methods for declaring constants:
Const constant (note to capitalize)//The left of the assignment number must be a variable, and the constant cannot be re-assigned
/* Do not want to be changed the amount is generally set as constant */
2. Enumeration
1). Syntax:
[Pubic] enum enum name
{

Value of 1,
Value of 2,
Value of 3,
.......

}
Public: access modifier, optional
Enum: Enumeration method
2). Enumeration meaning
In order to solve the difference between the definitions of the project, more people play together, need to unify the name of the thing to play
For example: gender
char gender = "male";
String s1= "men";
String s2= "female";
/* enum declared below the namespace, outside of the class, representing this namespace, all classes can be used */
/* Nature is variable, so-called multiple variables */
-Examples of applications such as
public enum Gender
{
Man
Woman
}
static void Main (string[] args)
{
int n=10;
Gender Gender=gender. Male;

}
3). Type conversion:
(1)--Conversion of enum type and int type (two types compatible)
int n= (int) "male";//return value is 0 (enum type default starting from 0) haha, like double
(2)--with string conversion
int n1=1;
Gender gender=gender.n1;//return value is "female"
/* All types can call the ToString method to convert to a string, but the string cannot be converted directly to an enumeration type */
(3)--Call the parse () method
Gender n= (Gender) Enum.parse (typeof (Gender), s),//convert S to Gender type, and N accept
/* If the enumeration does not work like s, it throws an exception (according to the old method try--catch--statement) */
3. Structure
Can help us declare several different types of variables at once
public struct person
{
public string _name;/* can only be added in the structure, but now it becomes a field, not a variable, which is stored
Multiple values, a variable can hold only one value
General requirements specification underlined to be the same as the variable */
public int _age;
public Char _gender;
}
Similar to arrays in Python,----but completely different
Person ara;//In fact, only one variable ARA is declared here, making its type part of the structure
Ara._name= "Ara";
ara._age=20;
Ara._gender= "Man";
4. Arrays
can help us to store multiple variables of the same type at once (data)//similar to arrays in Python
Grammar:
1) array definition of numbers
Int[] Nums=new int[10];
*/* Note that in C #, you first define the array length (that is, open up the memory space),
Well----first set up the whole, to memory has certain reason, conforms to the C family's consistent style
Each variable in the array is an element in the group, and the initial value is 0
To access each element, you need the ordinal action for the element (ordinal from zero, haha, like Python)
*/
By looping, note that you cannot assign a value more
for (int i=0;i<nums.length;i++);
{
Nums[i]=i;

}
Of course, you can also take a value by loop
for (int i=0;i<nums.length;i++);
{
Console.WriteLine (Nums[i]);
}
2) string (initial value is null, that is, there is no space) and Boolean type (initial value is false) array definition
3) How arrays are declared
Int[] nums={1,2,3}
Int[] Nums=new int[3]{1,2,3}
Exercise: Remove the largest integer and the smallest integer from an array of integers
Declares an array of type iny and arbitrarily assigns an initial value
int nums={1,2,3,4,5,6,7,8,8,9}//defines an array
The int max=nums[0];//declares that the variable stores the maximum value (and can also be written as int max=int. MinValue (assigns the smallest value represented by an integer type to max) There's a function like this.
int mon=num[0];//declares a variable to store the minimum value
int sum=ave=0;
for (int i=0;i<nums.length;i++);
{
if (Nums[i]>max)
{
Max=nums[i];
}
if (nums[i]>min)
{
Min=nums[i];
}
Sum+=nums[i];
Ave=sum/nums.length;
}
Console.WriteLine ("The maximum value in the array is {0}, the minimum value is {1}, the sum is {2}, the average is {3}", Max,min,sum,ave);
Consolo.readkey ();
Exercise: Split the name with "|"
String[] names={"Lao Yang", "Old Su"};
String Str=null;
for (int i=0;i<names.length-1;i++)
{
str+=names[i]+ "|"
}
Console.WriteLine (Str+names[names.length-1]);
Console.readkey ();
5. Bubble sort
Arranges elements in an array in order from small to large or from large to small
Int[] nums={122,,22,33,23,2,34,54,1223}
Compares the first element to each subsequent element
Sort by special method Array.Sort (Nums) (only in ascending order)
Array.reverse (Nums);
for (int i=0;i<nums.length;i++)
{
for (int j=0;j<nums.length-1-i;j++)
{
if (nums[j]>nums[j+1])
{
int temp =NUMS[J];
NUMS[J]=NUMS[J+1];
NUMS[J+1]=NUMS[J];
}
}
}
for (int i=0;i<nums.length;i++)
{
Console.WriteLine (Nums[i])
}
Console.readkey ();
6. Methods
public static return value type method name ([parameter list])
{
Method body;
}
Public: access modifiers, public, common
Static: Statically
Return value type: If not, write void (that is, you do not need a variable to receive, but can wrap)
Method Name: psical, all words capitalized, the remaining lowercase
Parameter list: The required conditions, if not, the parentheses must be written
Return: Ends This method immediately, returning the value to return
* If you want to be executed, you must execute it in the main () function
* Called when [type variable = class name.] Method Name ([parameter list])
* Call an unknown function, and a parenthesis will display the conditions that should be entered
In some cases, the class name can be omitted, under the same class
Example
public static int Getmax (int n1,int n2)
{
Return n1>n2?n1:n2;

}
int A=program. Getmax (1,3);

1. Constants:
Methods for declaring constants:
Const constant (note to capitalize)//The left of the assignment number must be a variable, and the constant cannot be re-assigned
/* Do not want to be changed the amount is generally set as constant */
2. Enumeration
1). Syntax:
[Pubic] enum enum name
{

Value of 1,
Value of 2,
Value of 3,
.......

}
Public: access modifier, optional
Enum: Enumeration method
2). Enumeration meaning
In order to solve the difference between the definitions of the project, more people play together, need to unify the name of the thing to play
For example: gender
char gender = "male";
String s1= "men";
String s2= "female";
/* enum declared below the namespace, outside of the class, representing this namespace, all classes can be used */
/* Nature is variable, so-called multiple variables */
-Examples of applications such as
public enum Gender
{
Man
Woman
}
static void Main (string[] args)
{
int n=10;
Gender Gender=gender. Male;

}
3). Type conversion:
(1)--Conversion of enum type and int type (two types compatible)
int n= (int) "male";//return value is 0 (enum type default starting from 0) haha, like double
(2)--with string conversion
int n1=1;
Gender gender=gender.n1;//return value is "female"
/* All types can call the ToString method to convert to a string, but the string cannot be converted directly to an enumeration type */
(3)--Call the parse () method
Gender n= (Gender) Enum.parse (typeof (Gender), s),//convert S to Gender type, and N accept
/* If the enumeration does not work like s, it throws an exception (according to the old method try--catch--statement) */
3. Structure
Can help us declare several different types of variables at once
public struct person
{
public string _name;/* can only be added in the structure, but now it becomes a field, not a variable, which is stored
Multiple values, a variable can hold only one value
General requirements specification underlined to be the same as the variable */
public int _age;
public Char _gender;
}
Similar to arrays in Python,----but completely different
Person ara;//In fact, only one variable ARA is declared here, making its type part of the structure
Ara._name= "Ara";
ara._age=20;
Ara._gender= "Man";
4. Arrays
can help us to store multiple variables of the same type at once (data)//similar to arrays in Python
Grammar:
1) array definition of numbers
Int[] Nums=new int[10];
*/* Note that in C #, you first define the array length (that is, open up the memory space),
Well----first set up the whole, to memory has certain reason, conforms to the C family's consistent style
Each variable in the array is an element in the group, and the initial value is 0
To access each element, you need the ordinal action for the element (ordinal from zero, haha, like Python)
*/
By looping, note that you cannot assign a value more
for (int i=0;i<nums.length;i++);
{
Nums[i]=i;

}
Of course, you can also take a value by loop
for (int i=0;i<nums.length;i++);
{
Console.WriteLine (Nums[i]);
}
2) string (initial value is null, that is, there is no space) and Boolean type (initial value is false) array definition
3) How arrays are declared
Int[] nums={1,2,3}
Int[] Nums=new int[3]{1,2,3}
Exercise: Remove the largest integer and the smallest integer from an array of integers
Declares an array of type iny and arbitrarily assigns an initial value
int nums={1,2,3,4,5,6,7,8,8,9}//defines an array
The int max=nums[0];//declares that the variable stores the maximum value (and can also be written as int max=int. MinValue (assigns the smallest value represented by an integer type to max) There's a function like this.
int mon=num[0];//declares a variable to store the minimum value
int sum=ave=0;
for (int i=0;i<nums.length;i++);
{
if (Nums[i]>max)
{
Max=nums[i];
}
if (nums[i]>min)
{
Min=nums[i];
}
Sum+=nums[i];
Ave=sum/nums.length;
}
Console.WriteLine ("The maximum value in the array is {0}, the minimum value is {1}, the sum is {2}, the average is {3}", Max,min,sum,ave);
Consolo.readkey ();
Exercise: Split the name with "|"
String[] names={"Lao Yang", "Old Su"};
String Str=null;
for (int i=0;i<names.length-1;i++)
{
str+=names[i]+ "|"
}
Console.WriteLine (Str+names[names.length-1]);
Console.readkey ();
5. Bubble sort
Arranges elements in an array in order from small to large or from large to small
Int[] nums={122,,22,33,23,2,34,54,1223}
Compares the first element to each subsequent element
Sort by special method Array.Sort (Nums) (only in ascending order)
Array.reverse (Nums);
for (int i=0;i<nums.length;i++)
{
for (int j=0;j<nums.length-1-i;j++)
{
if (nums[j]>nums[j+1])
{
int temp =NUMS[J];
NUMS[J]=NUMS[J+1];
NUMS[J+1]=NUMS[J];
}
}
}
for (int i=0;i<nums.length;i++)
{
Console.WriteLine (Nums[i])
}
Console.readkey ();
6. Methods
public static return value type method name ([parameter list])
{
Method body;
}
Public: access modifiers, public, common
Static: Statically
Return value type: If not, write void (that is, you do not need a variable to receive, but can wrap)
Method Name: psical, all words capitalized, the remaining lowercase
Parameter list: The required conditions, if not, the parentheses must be written
Return: Ends This method immediately, returning the value to return
* If you want to be executed, you must execute it in the main () function
* Called when [type variable = class name.] Method Name ([parameter list])
* Call an unknown function, and a parenthesis will display the conditions that should be entered
In some cases, the class name can be omitted, under the same class
Example
public static int Getmax (int n1,int n2)
{
Return n1>n2?n1:n2;

}
int A=program. Getmax (1,3);

C # Learn the third day

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.