C # study notes (8): complex type and realization

1. Array (the Array)

Arrays are stored in a sequence of fixed-size set of elements of the same type. An array is used to store the data set, a set of generally considered to be an array of the same type of variable.

When you create an array, C # compiler will initialize the array types implicitly each array element to a default value. For example, all elements int array is initialized to 0.

public class ArrayTest
{
	class arrTest
	{
		static void Main(string[] args)
		{
			//赋值方式1(声明+初始化+赋值)
			double[] arrd1;
			arrd1 = new double[10];
			arrd1[1] = 34.001;

			//赋值方式2(初始化+赋值)
			double[] arrd2= new double[10];
			arrd2[1] = 34.001;

			//赋值方式3(声明赋值)
			double[] arrd3 = {11,12,13,14,18.001};

			//赋值方式4(初始化赋值)
			double[] arrd4 = new double[5] {11,12,13,14,18.001};

			//赋值方式4(初始化赋值-省略大小)
			double[] arrd5 = new double[] {11,12,13,14,18.001};

			Console.WriteLine("value is {0}",arrd1[1]);
			Console.WriteLine("value is {0}",arrd2[1]);
			Console.WriteLine("value is {0}",arrd3[1]);
			Console.WriteLine("value is {0}",arrd4[1]);
			Console.WriteLine("value is {0}",arrd5[1]);

			int[] arrI = new int[10];
			//初始化
			for(int i=0; i <10; i++)
			{
				arrI[i]=i+100;
			}
			//循环遍历
			foreach(int j in arrI)
			{
				int i = j - 100;
				Console.WriteLine("arrI[{0}] = {1}", i,j) ;
			}
			
			Console.ReadLine();
		}
	}
}

There are an array of multi-dimensional arrays, jagged arrays, pass an array parameter array, Array class

public class ArrayTest
{
	class arrTest
	{
		public void Arrs()
		{
			int[,] a = new int[3,2] {{1,1},{2,2},{3,3}};//3行2列的二维数组
			int[][] b = new int[2][] {new int[]{22,33,44},new int[]{88,99}};//交错数组


		}

		public double getAverage(int[] arr, int size)
		{
			int i;
			double avg;
			int sum=0;
			for(i=0;i<size;i++)
			{
				sum += arr[i];
			}
			avg = (double)sum / size;
			return avg;
		}
	}
	class MainTest
	{
		static void Main(string[] args)
		{
			arrTest arr = new arrTest();
			int[] num = new int[]{100,201,301,401};
			double avg;
			avg = arr.getAverage(num, num.Length);
			Console.WriteLine("average is {0}",avg);
			//Array方法
			Array.Sort(num);//排序
			Array.Reverse(num);//逆转
			Console.ReadKey();
		}
	}
}

2. The string (string)

Use  string  keyword to declare a string variable. string has its own properties and methods, you can query if necessary

3. The structure (struct)

In C #, the structure is a value type data structure, a single variable such that it may store data related to various types of data. struct  keyword is used to create structures

Class vs structure

Class and has the following basic structural differences:

  • Class is a reference type, the structure is a value type.
  • Structures do not support inheritance.
  • Structure can not declare a default constructor.
struct Books
{
	public string title;
	public int book_id;
	public string subject;
	public string author;

	public void getValues(string t, string a, string s, int id)
	   {
	      title = t;
	      author = a;
	      subject = s;
	      book_id =id; 
	   }
	   public void display()
	   {
	      Console.WriteLine("Title : {0}", title);
	      Console.WriteLine("Author : {0}", author);
	      Console.WriteLine("Subject : {0}", subject);
	      Console.WriteLine("Book_id :{0}", book_id);
	   }
}
public class structTest
{
	static void Main(string[] args)
	{
		Books Book1 = new Books(); /* 声明 Book1,类型为 Book */
		Books Book2 = new Books(); /* 声明 Book2,类型为 Book */

		Book1.getValues("C Programming","Nuha Ali", "C Programming Tutorial",6495407);
		Book2.getValues("Telecom Billing","Zara Ali", "Telecom Billing Tutorial", 6495700);

		Book1.display();
		Book2.display();

		Console.ReadKey();
	}

}

Understanding is not very deep

4. Enumeration (the Enum)

Enumeration is a set of named integer constants. Enumerated type is to use the  enum  keyword declared.

C # enumeration is a value type. In other words, the enumeration contains its own value and can not inherit or transfer inheritance.

enum Days
{
	Sun,Mon,Tue,Wed,Thu,Fri,Sat
}
public class MainTest
{
	static void Main(string[] args)
	{
		int x = (int)Days.Sun;
		int y = (int)Days.Fri;

		Console.WriteLine("Sun = {0}", x);
		Console.WriteLine("Fri  = {0}", y);

		Console.ReadKey();
	}
}

The type (class)

What constitutes a class of objects and perform any operation on this object. Objects are instances of classes. Methods and variables constitute a class to become a member of the class.

The definition of the class is based on keyword  class  began, followed by the name of the class. Body of the class, included in a pair of curly brackets

a. Constructor 

The class  constructor  is a special member function is executed when you create a new object class. Name The name of the class constructor identical, it does not have any return type. The default constructor has no parameters. But if you need a constructor can have arguments with arguments that this constructor is called parameterized constructor

b. destructor

Class  destructor  is a special member function of the class when the object is out of range.

Name destructor is in front of the name of the class with a wavy (~) as a prefix, it does not return a value, and it takes no parameters.

The destructor for the end of the program (such as closing the file, the release of memory, etc.) prior to the release of resources. Destructor not inherited or reload

namespace test
{
	class Add
	{
		public int x;
		public int y;
		//构造函数
		public Add(int a, int b)
		{
			Console.WriteLine("默认构造函数");
			x = a;
			y = b;
		}
		//析构函数
		~Add()
		{
			Console.WriteLine("对象已删除");
		}

		int getValue()
		{
			return x+y;
		}

		static void Main(string[] args)
		{
			Add a = new Add(3,4);
			int num = a.getValue();
			Console.WriteLine("add value  = {0}", num);
			Console.ReadKey();
		}
	}
}

c. static member

You can use  static  keyword members of the class is defined as static. When we declare a class member is static, meaning that no matter how many objects class is created, there is only one copy of the static member

Also can be a member function is declared as  static . Such functions can only access static variables. Static function before the object is created already exists

namespace staticTest
{
	class staticTest
	{
		public static int num;//静态变量
		public int a = 4;
		public void Count()
		{
			num++;
		}
		public int getNum()
		{
			return num;
		}
		public static int getNum_s()//静态函数,只能访问静态变量,类调用
		{
			return a;
		}
	}

	class MainTest
	{
		static void Main(string[] args)
		{
			staticTest s1 = new staticTest();
			staticTest s2 = new staticTest();

			s1.Count();
			Console.WriteLine("s1 -> num : {0}", s1.getNum());
			Console.WriteLine("s2 -> num : {0}", s2.getNum());
			Console.WriteLine("s2 -> num : {0}", staticTest.getNum_s());
			Console.ReadKey();
		}
	}
}

 

Guess you like

Origin blog.csdn.net/u013783095/article/details/94734316