c#入门经典入门经典-调试函数的运用
见代码:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Diagnostics;
5
//这里用到两个函数Debug.WriteLine()和Trace.WriteLine()
6
//Debug.WriteLine()和Trace.WriteLine类似于Console.WriteLine
7
//区别是把信息输出到output窗口
8
//Debug.WriteLine()仅在调试时输入,生成可发布版本时自动消失
9
//在调试的时候,如果设置为debug在可以看到output中看到完整的信息
10
//调试时如果设置为release模式,即发布模式,则看到output窗口中的完整信息
11
//
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
namespace Ch07Ex01
14
{
15
class Program
16
{
17
static void Main(string[] args)
18
{
19
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
20
//存储下标数组,为什么是数组?因为有可能有多个相同最大值,其下标是不同的,
21
//因此就用一个数组存储.
22
int[] maxValIndices;
23
24
int maxVal = Maxima(testArray, out maxValIndices);
25
Console.WriteLine("Maximum value {0} found at element indices:", maxVal);
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
foreach (int index in maxValIndices)
28
{
29
//输出maxValIndices对象中的元素,其中存储的数组下标,本例中从0开始到最大值9分别存在9和11位
30
Console.WriteLine(index);
31
}
32
Console.ReadKey();
33
}
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
/// <summary>
36
/// 找出给定数组中最大值
37
/// </summary>
38
/// <param name="integers">integers存储传来的数组</param>
39
/// <param name="indices">indices数组存储数组下标,定于为out,为引用调用,有本函数初始化数组</param>
40
/// <returns>传入数组中的最大值</returns>
41
static int Maxima(int[] integers, out int[] indices)
42
{
43
//初始化,处理第一个元素
44
Debug.WriteLine("Maximum value search started.");
45
indices = new int[1];
46
int maxVal = integers[0];
47
indices[0] = 0;
48
int count = 1;
49
//Debug.Assert(count > 1, "my test", "contact me");
50
//在此我自己的添加的代码,可以忽略。
51
Debug.WriteLine("Maximum value initiallized to " + maxVal + ", at element index 0.");
52
53
for (int i = 1; i < integers.Length; i++)
54
{
55
Debug.WriteLine("Now looking at element at index " + i + ".");
56
//当数组中只有一个最大值的情况
57
if (integers[i] > maxVal)
58
{
59
maxVal = integers[i];
60
count = 1;
61
indices = new int[1];
62
indices[0] = i;
63
Debug.WriteLine("New maxmum found. New value is " + maxVal + ", at element index " + i + ".");
64
}
65
else
66
{
67
//最大值有两个时的情况
68
if (maxVal == integers[i])
69
{
70
count++;
71
/*数组发生变化,不能用new int[1],此时要存储多个下标,因此数组长度将发生变化,
72
* new int[1]不在适用,因此要扩大数组大小,但是数组长度是不能动态增长的,处理办法如下 :
73
* 1.把原来下标数组中的值存储到一个备份数组中
74
* 2.重新构造下标数组的维数,维数大小代表重复最大值的个数,以这个值为依据进行构造
75
* 其中用到了数组拷贝函数CopyTo() 用法为:老数组.CopyTo(新数组,拷贝到新数组的哪个下标开始的位置)。
76
*/
77
78
//数组备份
79
int[] oldIndices = indices;
80
//根据新的count值构造数组的长度
81
indices = new int[count];
82
//数组拷贝
83
oldIndices.CopyTo(indices, 0);
84
//新的下标加入数组
85
indices[count - 1] = i;
86
Debug.WriteLine("Duplication maximum found at element index " + i + ".");
87
}
88
}
89
}
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
Trace.WriteLine("Maximum value " + maxVal + " found, with " + count + " occurrence.");
92
Debug.WriteLine("Maximum value search completed.");
93
//返回最大值,由于下标数组是out的,所以不用返回
94
return maxVal;
95
}
96
}
97
}
98![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)