Constant : ---- always the same amount
Constant declaration: Const type constant name = constant value
const int num = 10;
Variable ----- often change the value of the amount
Structure (struct):
In C #, the structure is a value type data structure, a single variable such that it can store data of various data types
Struct keyword is used to create the structure.
For example:
In the library there are many attributes for each book: title, author, subject, bookID
Definition of the structure:
When we have to use struct statement in the definition of the structure of time
struct statement for the program defines a new data type with more members
For chestnut:
//定义结构体
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
//调用结构体
public static void Main(string[] args)
{
Books Book1; /* 声明 Book1,类型为 Book */
Books Book2; /* 声明 Book2,类型为 Book */
/* book 1 详述 */
Book1.title = "C Programming";
Book1.author = "Nuha Ali";
Book1.subject = "C Programming Tutorial";
Book1.book_id = 6495407;
声明一个结构体:
public struct person
{
public string _name; //字段
public char _gender;
}
调用结构体:
Person laosu;
Laosu._name //输入点后就会自动显示出来;
Many people confuse between classes and structures will, in fact, there is a great difference between them:
VS class structure:
1. The reference type is the class, the structure is a value type
2. The structure does not support inheritance
3. The structure can not declare a default constructor
4. The structure of the field can not be declared initial value, class
such as:
struct test001 { private int aa = 1; }
The above code performs a " structure or not instance attributes field initializer error", and the class is no such limit, as follows:
class test002 { private int aa = 1; }
Single 举法:
Enumeration is a set of named integer constants, enumerated types are declared using the keyword enum
For chestnut:
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
static void Main()
{
int x = (int)Day.Sun;
int y = (int)Day.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}
结果:
Sun = 0
Fri = 5
NOTE: enumerations and structures can be used together:
Public enum Gender
{
男,
女
}
//声明了一个枚举 ;
Public struct Person
{
Public string _name ;
Public int _age;
Public Gender _gender ;
}
使用:
Class Program
{
Static void main(string() args)
{
Person zsperson;
Zsperson._name="张三";
Zsperson._age=90;
Zsperson._gender=Gender.男;
}
}
Array:
Is a memory array of the same type of elements of fixed size sequence set .
Array for storing a set of data, the array is generally considered a collection of variables of the same type
// array declaration:
The first way: Int [] number = new int [] {2,4,5} to initialize an array
The second way: int [] nus = new int [10] initialize an array
10 int into a type of memory space nus;
The third way: datatype [] arrayName;
- datatype for specifying the type of elements stored in the array.
- [] Rank (dimension) of the specified array. Rank specify the size of the array.
- arrayName specify the name of the array.
Almost any type can declare an array:
Int 【3】
int【】 nums={ 5,3,8}
[3] {1,3,5} // number of statements and the number of Int number = new int [] must be consistent
Int [] number=new int[] {2,4,5}
For chestnut:
//在一次语文测试后,老师让班长统计每一个学生的成绩并计算全班的平均成绩,
//然后把所有成绩显示出来
Console.WriteLine("请输入班级人数");
int number = 0;
int sum = 0;
if (int.TryParse(Console.ReadLine(), out number))
{
int[] scores = new int[number];//声明一个数组用来存成绩
for (int i = 0; i < number; i++)
{
Console.WriteLine("请输入第{0}个人的成绩", i + 1);
scores[i] = int.Parse(Console.ReadLine());//把用户输入的成绩转换为int类型并存入到数组中
sum += scores[i];//求和
}
Console.WriteLine("总成绩为{0},平均成绩为{1}",sum, sum / number);
}
else
{
Console.WriteLine("输入有误");
}
Console.ReadKey();
Knowledge extension:
Three methods converted into a digital string
- Convert.toint32(age);
- Int age = int.parse (numeric string type)
Although the method can maybe string type digital-digital converter, a digital type but not if the input string will be given
The third method:
strNUM string type.
Int num = 0
bool result = Int.TryParse(strNUM,out int age)
This method is the return value of type Bool, or bluntly, this line of code int.TryParse () use to receive a variable of type bool
The following explanation with an example:
int i = -1;
bool b = int.TryParse(null, out i);
执行完毕后,b等于false,i等于0,而不是等于-1,切记。
int i = -1;
bool b = int.TryParse("123", out i);
执行完毕后,b等于true,i等于123;
int.TryParse and int.Parse and more similar, but it does not produce abnormal returns true conversion is successful, the conversion failed to return false.
Seeking the maximum and minimum array:
//求数组最大值 ;
int[] numbers = new int[] { 1, 3, 4, -556, 5, 4, 34, 64, 34 };
int temp = int.MinValue ;
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] > temp)
{
temp = numbers[i];
}
}
Console.WriteLine("最大的值为{0}", temp);
Console.ReadKey();
//求数组的最大值和最小值
int[] numb = new int[] { 2, 43, 55, 23, -45, 54 };
int min = int.MaxValue;// 最小值
int max = int.MinValue;
for (int i = 0; i < numb.Length; i++)
{
if (numb[i] > max)
{
max = numb[i];
}
if (numb[i] < min)
{
min = numb[i];
}
}
Console.WriteLine("最大值为{0}",max);
Console.WriteLine("最小值为{0}",min);
Console.ReadKey();
Variable array: Parames Array
Under normal circumstances array:
static void Main(string[] args)
{
show("{0}{1}{2}{3}{4}{5}{6}{7}{8}", 1, "2", 4, 5, 6, 7, 8, 4, 6);
}
public static void show(string str, params int []strs)
{
}
This time the system will error, because this type of match can not be normal, but if we use a variable array, the effect Leverage!
The code can be changed to:
static void Main(string[] args)
{
show("{0}{1}{2}{3}{4}{5}{6}{7}{8}", 1, "2", 4, 5, 6, 7, 8, 4, 6);
}
public static void show(string str, params object []strs)
{
}
This can be run properly and without error of