首先,介紹一下最基礎的東東,只有基礎打牢了,才能做出漂亮的程式,就像學英語,首先要寫ABC一樣,這只是一個過程,當然談戀愛也要過程,也要基礎,扯遠了,我將會用最簡單明了的句語來表達.
首先,介紹語法,說了,像ABC,那一定要掌握了,不然的話,先英語都不會講了,
兩種類型: 值类型,引用类型,指针类型
值类型:变量保存为实际值,内存分配在堆栈上,默认值为零,例如:int char struct等都是值类型
引用类型:变量保存对象的地址,内存分配在堆上,默认值为null,例如:类、接口、数组, 指针等。
通过装箱和取消装箱,可以将值类型转换为引用类型,然后再转换回值类型。除了装箱值类型外,无法将引用类型转换为值类型。
oh,还好赶在MM下线前整完了哈。下個主題(哈哈):数据类型处理(數據強制轉換,装箱和取消装箱)。
About Main () function.
不管用C,C++,JAVA,還是用其它語言,一般來講都會用到main這個字眼,
盛現在有空,就介紹一個Main() 在C# 中的重要作用及一些用法,
Main 方法是程序的入口点,程序控制在该方法中开始和结束。
该方法在类或结构的内部声明。它必须为静态方法,而不应为公共方法。(在上面的示例中,它接受默认访问级别 private。)
它可以具有 void 或 int 返回类型。
声明 Main 方法时既可以使用参数,也可以不使用参数。
参数可以作为从零开始索引的命令行参数来读取。
与 C 和 C++ 不同,程序的名称不会被当作第一个命令行参数。
1.一个程序中不能有两个以上的Main函数,有且只有一个
四種形式:
static void
Main
()
{
//this add code…
}
static int
Main
()
{
//this add code…
}
static void
Main
(string[] args)
{
//this add code…
}
static int
Main
(string[] args)
{
//this add code…
}
2.在命令行传输参数时,存放在string数组args中。使用Length属性来测试输入参数的个数。
3.使用foreach语句来检索所有的参数
foreach (string s in args)
{
System.Console.WriteLine(s);
}
4.例子main與命令行參數:程序在运行时采用一个参数,将该参数转换为整数,并计算该数的阶乘。如果没有提供参数,则程序发出一条消息来解释此程序的正确用法。(這個例子MSDN上也有.)
代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace MainFunctions
{
class Program
{
public class Functions
{ // arguments: 3
public static long Factorial(int n)
{
if (n < 0) { return -1; } //error result - undefined
if (n > 256) { return -2; } //error result - input is too big
if (n == 0) { return 1; }
// Calculate the factorial iteratively rather than recursively:
long tempResult = 1;
for (int i = 1; i <= n; i++)
{
tempResult *= i;
}
return tempResult;
}
}
class MainClass
{
static int Main (string[] args)
{
int m = args.Length - 3;
System.Console.WriteLine("args length:{0}",args.Length);
//循环访问数组的兩種方法
for (int i = 0; i < args.Length; i++)
{
System.Console.WriteLine("for :Arg[{0}] = [{1}]", i, args[i]);
}
foreach (string s in args)
{
//int m = args.Length - 3; every run args.Length = 3;
System.Console.WriteLine("foreach:Arg[{0}] = [{1}]", m++, s);
}
// Test if input arguments were supplied:
if (args.Length == 0)
{
System.Console.WriteLine("Please enter a numeric argument.");
System.Console.WriteLine("Usage: Factorial <num>");
return 1;
}
try
{
// Convert the input arguments to numbers:
int num = int.Parse(args[0]);
System.Console.WriteLine("The Factorial of {0} is {1}.", num, Functions.Factorial(num));
return 0;
}
catch (System.FormatException)
{
System.Console.WriteLine("..Please enter a numeric argument.");
System.Console.WriteLine("Usage: Factorial <num>");
return 1;
}
}
}
}
}
寫好代碼之後,
把相應的字符放入 項目->屬性->調試->命令行參數即可..選譯開始執行(不調試).
按照此步驟應該可以得到想要的結果,有任何問題請留言或者mail me..