📄 visual c# .net 入门.txt
字号:
// Import namespaces
using System;
using System.Collections;
using System.IO;
// Declare namespace
namespace MsdnAA
{
// Declare application class
class QuickSortApp
{
// Application initialization
static void Main (string[] szArgs)
{
... ... ...
// Pass to QuickSort function
QuickSort (szContents, 0, szContents.Count - 1);
... ... ...
}
// QuickSort implementation
static void QuickSort (ArrayList szArray, int nLower, int nUpper)
{
// Check for non-base case
if (nLower < nUpper)
{
// Split and sort partitions
int nSplit = Partition (szArray, nLower, nUpper);
QuickSort (szArray, nLower, nSplit - 1);
QuickSort (szArray, nSplit + 1, nUpper);
}
}
// QuickSort partition implementation
static int Partition (ArrayList szArray, int nLower, int nUpper)
{
// Pivot with first element
int nLeft = nLower + 1;
string szPivot = (string) szArray[nLower];
int nRight = nUpper;
// Partition array elements
string szSwap;
while (nLeft <= nRight)
{
// Find item out of place
while (nLeft <= nRight)
{
if (((string) szArray[nLeft]).CompareTo (szPivot) > 0)
break;
nLeft = nLeft + 1;
}
while (nLeft <= nRight)
{
if (((string) szArray[nRight]).CompareTo (szPivot) <= 0)
break;
nRight = nRight - 1;
}
// Swap values if necessary
if (nLeft < nRight)
{
szSwap = (string) szArray[nLeft];
szArray[nLeft] = szArray[nRight];
szArray[nRight] = szSwap;
nLeft = nLeft + 1;
nRight = nRight - 1;
}
}
// Move pivot element
szSwap = (string) szArray[nLower];
szArray[nLower] = szArray[nRight];
szArray[nRight] = szSwap;
return nRight;
}
}
}
QuickSort() 函数
这个函数需要三个参数:对数组的引用、下界和上界。它调用 Partition() 函数将数组分成两部分,其中一部分包含 Pivot 值之前的所有字符串,另一部分包含 Pivot 值之后的所有字符串。然后,它调用自身来对每个部分进行排序。
上面修改中的注释应该说明了每个代码块的作用。唯一的新概念就是 CompareTo() 方法的使用,该方法是 String 类的成员,并且应该是自说明的。
运行 QuickSort 应用程序
这一步完成 QuickSort C# 示例应用程序。现在,可以构建项目并运行应用程序。需要提供一个示例文本文件,以供其进行排序。将该文件放在与 EXE 文件相同的目录中。
程序输出
下面是已完成的 QuickSort C# .NET 示例应用程序的输出。可以查看示例输入文件 'example.txt' 和输出文件 'output.txt'。
步骤 8. 使用调试器
调试器是诊断程序问题的一个必不可少的工具。我们觉得有必要在本入门指南中对其进行介绍。这最后一步将向您展示如何走查程序和使用诸如 QuickWatch 这样的功能。
设置断点
当程序在调试器中运行时,断点会暂停程序的执行,从而使开发人员能够控制调试器。要设置断点,请右键单击您想要程序暂停的行,然后单击 InsertBreakpoint,如下所示。
注:带有断点的行以红色突出显示。通过再次右键单击该行并选择 Remove Breakpoint 可以删除断点。
单步调试程序
既然设置了断点(最好是在前面所示的行中),就让我们在调试器中运行程序。在 Debug 菜单中,选择 Start 而不是同前面一样选择 Start Without Debugging。这样就在调试器中启动了程序,并因而激活了断点。
一旦程序遇到断点,调试器便会接收程序的控制。这时会有一个箭头指向当前执行的行。
要单步调试一行代码,可以选择 Debug | Step Over 并观察光标是否移到下一行。Debug | Step Into 命令允许您单步执行将要调用的函数。进行两次 Step Over 之后的屏幕如下所示。
如果想要程序在遇到下一个断点、遇到异常或退出之前继续执行,请从菜单中选择 Debug | Continue。
检查变量值
当您可以控制调试器时,可将鼠标指针移到变量上以获得它的基本值。
您也可以右键单击变量,然后从上下文菜单中选择 QuickWatch。QuickWatch 将为您提供关于某些变量(如 ArrayList 对象)的更多详细信息。
其他调试器工具
Visual Studio 调试器具有许多其他工具(例如 Call Stack 查看器)的功能,可以使用此调试器来查看到此为止调用的函数。还可以获得内存转储和关于进程中线程的信息。我们鼓励您使用这些功能强大的调试工具。
小结
本入门指南旨在帮助您用 Visual Studio 构建一个简单的 C# 项目。它无法进行全面的介绍。我们鼓励您查询关于 C# 和 .NET 的其他资源,以便更多地学习这些技术。在完成本教程之后,您至少有了一个可用的项目,在您研究 Visual C# 时,可以从修改此这些代码开始。
为了方便起见,我们提供了完整的源程序和项目文件。您可以通过本文档顶部的目录来访问它们。
其他资源
我们强烈推荐下面这些关于 C# 和 .NET 平台的书籍。它们是开发人员尝试学习这些新技术的有益资源。
? Archer, Tom.Inside C#.Redmond:Microsoft Press, 2001.
? Deitel, Harvey.C#:How to Program.Upper Saddle River, NJ:Prentice Hall, 2001.
? Gunnerson, Eric.A Programmer's Introduction to C#.New York:Apress, 2000.
? Platt, David.Introducing Microsoft .NET.Redmond:Microsoft Press, 2001.
补遗:QuickSort C# .NET 的源代码
下面是 QuickSort C# .NET 示例应用程序的完整源代码。您可以复制、使用和分发这些代码(无版权费)。注意,这些源代码以"原样"提供并且不作任何保证。
//
// QuickSort C# .NET Sample Application
// Copyright 2001-2002 Microsoft Corporation. All rights reserved.
//
// MSDN ACADEMIC ALLIANCE [http://www.msdn.microsoft.com/academic]
// This sample is part of a vast collection of resources we developed for
// faculty members in K-12 and higher education. Visit the MSDN AA web site for more!
// The source code is provided "as is" without warranty.
//
// Import namespaces
using System;
using System.Collections;
using System.IO;
// Declare namespace
namespace MsdnAA
{
// Declare application class
class QuickSortApp
{
// Application initialization
static void Main (string[] szArgs)
{
// Print startup banner
Console.WriteLine ("\nQuickSort C#.NET Sample Application");
Console.WriteLine ("Copyright (c)2001-2002 Microsoft Corporation. All rights reserved.\n");
Console.WriteLine ("MSDN ACADEMIC ALLIANCE [http://www.msdnaa.net/]\n");
// Describe program function
Console.WriteLine ("This example demonstrates the QuickSort algorithm by reading an input file,");
Console.WriteLine ("sorting its contents, and writing them to a new file.\n");
// Prompt user for filenames
Console.Write ("Source: ");
string szSrcFile = Console.ReadLine ();
Console.Write ("Output: ");
string szDestFile = Console.ReadLine ();
// Read contents of source file
string szSrcLine;
ArrayList szContents = new ArrayList ();
FileStream fsInput = new FileStream (szSrcFile, FileMode.Open, FileAccess.Read);
StreamReader srInput = new StreamReader (fsInput);
while ((szSrcLine = srInput.ReadLine ()) != null)
{
// Append to array
szContents.Add (szSrcLine);
}
srInput.Close ();
fsInput.Close ();
// Pass to QuickSort function
QuickSort (szContents, 0, szContents.Count - 1);
// Write sorted lines
FileStream fsOutput = new FileStream (szDestFile, FileMode.Create, FileAccess.Write);
StreamWriter srOutput = new StreamWriter (fsOutput);
for (int nIndex = 0; nIndex < szContents.Count; nIndex++)
{
// Write line to output file
srOutput.WriteLine (szContents[nIndex]);
}
srOutput.Close ();
fsOutput.Close ();
// Report program success
Console.WriteLine ("\nThe sorted lines have been written to the output file.\n\n");
}
// QuickSort implementation
private static void QuickSort (ArrayList szArray, int nLower, int nUpper)
{
// Check for non-base case
if (nLower < nUpper)
{
// Split and sort partitions
int nSplit = Partition (szArray, nLower, nUpper);
QuickSort (szArray, nLower, nSplit - 1);
QuickSort (szArray, nSplit + 1, nUpper);
}
}
// QuickSort partition implementation
private static int Partition (ArrayList szArray, int nLower, int nUpper)
{
// Pivot with first element
int nLeft = nLower + 1;
string szPivot = (string) szArray[nLower];
int nRight = nUpper;
// Partition array elements
string szSwap;
while (nLeft <= nRight)
{
// Find item out of place
while (nLeft <= nRight && ((string) szArray[nLeft]).CompareTo (szPivot) <= 0)
nLeft = nLeft + 1;
while (nLeft <= nRight && ((string) szArray[nRight]).CompareTo (szPivot) > 0)
nRight = nRight - 1;
// Swap values if necessary
if (nLeft < nRight)
{
szSwap = (string) szArray[nLeft];
szArray[nLeft] = szArray[nRight];
szArray[nRight] = szSwap;
nLeft = nLeft + 1;
nRight = nRight - 1;
}
}
// Move pivot element
szSwap = (string) szArray[nLower];
szArray[nLower] = szArray[nRight];
szArray[nRight] = szSwap;
return nRight;
}
}
}
补遗:关于 QuickSort C# .NET
为了演示 QuickSort Visual C# .NET 示例应用程序实际是如何运行的,我们提供了编译好的可执行文件。您可以通过编译这些项目文件来创建自己的可执行文件。单击 Quicksort_Visual_CSharp_.NET.exe,下载源代码项目文件和可执行文件包。
使用应用程序
启动 Command Prompt(从"开始"菜单运行"cmd.exe")。使用 CD 命令将目录更改为可执行文件所在的目录。然后运行"quicksort.exe"。
程序将提示您提供输入和输出文件的名称。任何包含多行的文本文件均可使用。如果需要,可以使用记事本来创建一个此类文件。然后,该程序将对输入文件的内容进行排序,并且将其写入输出文件。
示例程序输出
下面是来自此 QuickSort C# .NET 应用程序的一个实例的输出。此示例演示了 QuickSort 算法,方法是读取输入文件、对文件的内容进行排序,然后将其写入新的文件。用户输入的文本以下划线标记。
您可以查看下面的示例输入文件 'example.txt' 和输出文件 'output.txt'。
QuickSort C# .NET Sample Application
Copyright (c)2001-2002 Microsoft Corporation. All rights reserved.
MSDN ACADEMIC ALLIANCE [http://www.msdn.microsoft.com/academic]
This example demonstrates the QuickSort algorithm by reading an input file,
sorting its contents, and writing them to a new file.
Source: example.txt
Output: output.txt
The sorted lines have been written to the output file.
查看示例输入文件"example.txt":
Visual C#
Windows Embedded
javascript
Speech API
ASP.NET
VBScript
Windows Media
Visual Basic
.NET Framework
BizTalk Server
XML Parser
Internet Explorer
Visual C#
SQL Server
Windows XP
DirectX API
查看示例输出文件"output.txt":
.NET Framework
ASP.NET
BizTalk Server
DirectX API
Internet Explorer
javascript
Speech API
SQL Server
VBScript
Visual Basic
Visual C#
Visual C#
Windows Embedded
Windows Media
Windows XP
XML Parser
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -