原来我是学过C#的,不过不是用《C#入门经典》。半年前看的了,看到了高级属性了,因为没有应用,所以现在是什么都想不起来了。先来温习一下。顺便让自己记住,学过了的东西一定要用,做好笔记,要不,又来重头来过,不值啊。实践是学习的唯一真理。
下面先温习一下吧。
用VS2005 EXPRESS EDITION建立一个控制台程序和一个WINDOWS FORMS应用程序。
前者很简单,只要在程序主体部分加上Console.WriteLine("hello world"); ,便可实现一个简单的控制台程序。
后者看起来和VB无异。在插入一个按钮之后,在FORM1.CS中键入下面代码。
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("this first windows app in the book!");
}
原来在C#中试用枚举,总是不成功,可能是原来那本书错了吧。现在用一下枚举:(P89)
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
enum manio : byte
{
schao = 1,
bird = 2,
taoshu = 3
}
class Program
{
static void Main(string[] args)
{
byte directionbyte;
string directionstring;
manio mydirection = manio.schao;
Console.WriteLine("mydirection={0}", mydirection);
directionbyte = Convert.ToByte( mydirection); //在这里,显式转换使用(byte)mydirection也一样
directionstring = Convert.ToString(mydirection); //在这里,不能用(string)mydirection,因为需要进行的处理不仅是把枚举转化为STRING
//也可以用mydirection.ToString();
Console.WriteLine("byte equivalent = {0}", directionbyte);
Console.WriteLine("string equ ={0}", directionstring);
}
}
}
skill:当VS自动完成功能下拉菜单出现里,按回车可输出高亮显示的,按空格可输出高亮显示的并在其后空格。当按空格后又接着输入分号表示结束语句,则VS又会清除自动产生的空格,将分号紧接在语句后面。很方便!
skill:当光标停在函数中时,会出现如图的函数说明,按上下箭头键可以翻页。
(p98)
using System;
using System.Collections.Generic;
using System.Text;
//函数的调用、类型转换、输出位置、
namespace ConsoleApplication1
{
class Program
{
static void show()
{
Console.WriteLine("hello world");
return;
}
static void Main(string[] args)
{
show(); //调用函数
string mystring;
int myint;
myint = 5;
//mystring = (string)myint; //在这里用(sgring)进行转换是不可以的!
mystring = myint.ToString(); //也可以用mystring = Convert.ToString(myint); 效果一样
Console.WriteLine("mystring={1},myint={0}",myint , mystring); //注意0 1与后面的变量的对应关系
}
}
}
输出:
下面这个程序没得语法错误,但是不能找出十个数中的最大
using System;
using System.Collections.Generic;
using System.Text;
//数组、函数、参数调用、输入
namespace ConsoleApplication1
{
class Program
{
static int MaxValue(int[] intManioArray)
{
int maxValue = intManioArray[0];
for (int i = 1; i < intManioArray.Length; i++)
{
if (intManioArray[i] > maxValue) maxValue = intManioArray[i];
}
return maxValue;
}
static void Main(string[] args)
{
/* 错误的数组使用,在访问之前必须先初始化(p85)
int[] arrayInMain;
for (int i = 0; i <= 10; i++)
{
arrayInMain[i] = Console.Read();
}
Console.WriteLine("the max is {0}", MaxValue(arrayInMain));
*/
int[] arrayinMain = new int[10];
for (int i = 0; i <= 9; i++)
{
arrayinMain[i] = Console.Read();
}
Console.WriteLine("the max is {0}", MaxValue(arrayinMain));
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
//命令行参数的使用
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0} command line arguments were specified:", args.Length);
foreach (string a in args)
Console.WriteLine(a);
}
}
}