C#是一门面向对象的语言,拥有所有面向对象的特性,现在让我们进入C#1.0.
精髓:
只有在Main()函数中才能定义变量;
Console.read();一个字符;
Console.readLine();一行读取;
只有引用类型才有默认值;
C#是强类型,不支持自由类型转换;
每个case后必须有break;
枚举是一个集合.
让代码说话:
class
Hello
{
static void Main( )
{
// Use the system console object
System.Console.WriteLine( " Hello 从C# 1.0 杀到 C#3.0 " );
}
}
{
static void Main( )
{
// Use the system console object
System.Console.WriteLine( " Hello 从C# 1.0 杀到 C#3.0 " );
}
}
if/else
class
MainEntryPoint
{
static void Main( string [] args)
{
Console.WriteLine( " Type in a String " );
string input;
input = Console.ReadLine();
if (input == "" )
{
Console.WriteLine( " You typed in an empty string " );
}
else if (input.Length < 5 )
{
Console.WriteLine( " The string had less than 5 characters " );
}
else if (input.Length < 10 )
{
Console.WriteLine( " The string had at least 5 but less than 10 characters " );
}
Console.WriteLine( " The string war " + input);
}
}
{
static void Main( string [] args)
{
Console.WriteLine( " Type in a String " );
string input;
input = Console.ReadLine();
if (input == "" )
{
Console.WriteLine( " You typed in an empty string " );
}
else if (input.Length < 5 )
{
Console.WriteLine( " The string had less than 5 characters " );
}
else if (input.Length < 10 )
{
Console.WriteLine( " The string had at least 5 but less than 10 characters " );
}
Console.WriteLine( " The string war " + input);
}
}
enum枚举:
public
enum
TimeOfDay
{
Morning = 0 ,
Afternoon = 1 ,
Evening = 2
}
class EnumExample
{
public static int Main()
{
// 调试说明更改WriteGreeting(TimeOfDay.Evening)
WriteGreeting(TimeOfDay.Morning);
return 0 ;
}
static void WriteGreeting(TimeOfDay timeOfDay)
{
switch (timeOfDay)
{
case TimeOfDay.Morning:
Console.WriteLine( " Good morning! " );
break ;
case TimeOfDay.Afternoon:
Console.WriteLine( " Good afternoon! " );
break ;
case TimeOfDay.Evening:
Console.WriteLine( " Good evening! " );
break ;
default :
Console.WriteLine( " Hello " );
break ;
}
}
}
{
Morning = 0 ,
Afternoon = 1 ,
Evening = 2
}
class EnumExample
{
public static int Main()
{
// 调试说明更改WriteGreeting(TimeOfDay.Evening)
WriteGreeting(TimeOfDay.Morning);
return 0 ;
}
static void WriteGreeting(TimeOfDay timeOfDay)
{
switch (timeOfDay)
{
case TimeOfDay.Morning:
Console.WriteLine( " Good morning! " );
break ;
case TimeOfDay.Afternoon:
Console.WriteLine( " Good afternoon! " );
break ;
case TimeOfDay.Evening:
Console.WriteLine( " Good evening! " );
break ;
default :
Console.WriteLine( " Hello " );
break ;
}
}
}
以上代码中初学时候会想最后的default以下的语句还会不会执行!
函数的调用:
public
class
MathLib
{
public int Add( int x, int y)
{
return x + y;
}
}
class Client
{
public static void Main()
{
MathLib mathObj = new MathLib();
Console.WriteLine(mathObj.Add( 7 , 8 ));
}
}
// =====================================================
class MainEntryPoint
{
static void Main( string [] args)
{
for ( int i = 0 ; i < 100 ; i += 10 )
{
for ( int j = i; j < i + 10 ; j ++ )
{
Console.Write( " + " , + j);
}
}
}
}
{
public int Add( int x, int y)
{
return x + y;
}
}
class Client
{
public static void Main()
{
MathLib mathObj = new MathLib();
Console.WriteLine(mathObj.Add( 7 , 8 ));
}
}
// =====================================================
class MainEntryPoint
{
static void Main( string [] args)
{
for ( int i = 0 ; i < 100 ; i += 10 )
{
for ( int j = i; j < i + 10 ; j ++ )
{
Console.Write( " + " , + j);
}
}
}
}
接下来是for和变量
public
class
ScopeText
{
public static int Main()
{
for ( int i = 0 ; i < 10 ; i ++ )
{
Console.WriteLine(i);
}
for ( int i = 9 ; i >= 0 ; i ++ )
{
Console.WriteLine(i);
}
return 0 ;
}
}
// ========================================================
class ScopeTest2
{
static int j = 20 ;
// j未被使用
public static void Main()
{
int j = 30 ;
Console.WriteLine(j);
return ;
}
}
{
public static int Main()
{
for ( int i = 0 ; i < 10 ; i ++ )
{
Console.WriteLine(i);
}
for ( int i = 9 ; i >= 0 ; i ++ )
{
Console.WriteLine(i);
}
return 0 ;
}
}
// ========================================================
class ScopeTest2
{
static int j = 20 ;
// j未被使用
public static void Main()
{
int j = 30 ;
Console.WriteLine(j);
return ;
}
}
接着是String
class
StringExample
{
public static int Main()
{
string s1 = " a string " ;
string s2 = s1;
Console.WriteLine( " s1 is " + s1);
Console.WriteLine( " s2 is " + s2);
s1 = " another string " ;
Console.WriteLine( " s1 is now " + s1);
Console.WriteLine( " s2 is now " + s2);
return 0 ;
}
}
{
public static int Main()
{
string s1 = " a string " ;
string s2 = s1;
Console.WriteLine( " s1 is " + s1);
Console.WriteLine( " s2 is " + s2);
s1 = " another string " ;
Console.WriteLine( " s1 is now " + s1);
Console.WriteLine( " s2 is now " + s2);
return 0 ;
}
}
我初学时候都是认真将上面代码写有下的,还记得那个时候还有一样很重要的知识就是下面这个表类型的默认值:
类型 默认值
Numeric 0
bool false
char '\0'
enum 0
引用 null
这样就可以对类型的默认值有比较清楚的认识啦.
现在我想很少有人用C#1.0,但还是给出实践代码(这是我以前写的但现在看又有新的体会):
实现排序:
class
ArrayDemo
{
static void Main( string [] args)
{
int [] elements = new int [ 5 ];
int index;
int temp;
Console.WriteLine( " 输入5个数字 " );
for (index = 0 ; index < elements.Length; index ++ )
{
elements[index] = int .Parse(Console.ReadLine());
}
Console.WriteLine( " \n已经排序数组 " );
for (index = 0 ; index < elements.Length; index ++ )
{
for ( int j = index + 1 ; j < elements.Length; j ++ )
{
if (elements[index] > elements[j])
{
temp = elements[index];
elements[index] = elements[j];
elements[j] = temp;
}
}
Console.WriteLine(elements[index]);
}
}
}
{
static void Main( string [] args)
{
int [] elements = new int [ 5 ];
int index;
int temp;
Console.WriteLine( " 输入5个数字 " );
for (index = 0 ; index < elements.Length; index ++ )
{
elements[index] = int .Parse(Console.ReadLine());
}
Console.WriteLine( " \n已经排序数组 " );
for (index = 0 ; index < elements.Length; index ++ )
{
for ( int j = index + 1 ; j < elements.Length; j ++ )
{
if (elements[index] > elements[j])
{
temp = elements[index];
elements[index] = elements[j];
elements[j] = temp;
}
}
Console.WriteLine(elements[index]);
}
}
}
编写一个查询汉字区位码;
算法:
((汉字的第一个字节-0xal)*97+(汉字的第二个字节-0xal))*32;
区码=汉字机器码高字节-A0;
位玛=汉字的机器码的低位码-A0;
汉字的文本读出的就是汉字的机器码,两个字节就代表一个机器码,高位在前,低位在后!
我还记得!
class
DistrictCode
{
public static void Main()
{
while ( true )
{
Console.Write( " 请输入(输入E退出): " );
string inputString = Console.ReadLine();
if (inputString == " e " || inputString == " E " )
{
break ;
}
else
{
Console.WriteLine(TextToQwm(inputString));
}
}
}
public static string TextToQwm( string character)
{
string coding = "" ;
int i1 = 0 ;
int i2 = 0 ;
int i3 = 0 ;
for ( int i = 0 ; i < character.Length; i ++ )
{
byte [] bytes = System.Text.Encoding.Default.GetBytes(character.Substring(i, 1 ));
i1 = ( short )(bytes[ 0 ]);
if (bytes.Length != 1 )
{
i2 = ( short )(bytes[ 1 ]);
i3 = 1 ;
}
else
{
i2 = 65535 ;
i3 = - 1 ;
}
int chrasc = i1 * 256 + i2 - 65536 ;
if (chrasc > 0 && chrasc < 160 )
{
Console.WriteLine( " 只能输入汉字. " );
}
else
{
if (i3 == - 1 )
{
Console.WriteLine( " 只能输入汉字 " );
}
else
{
string lowCode = System.Convert.ToString(Math.Abs(Convert.ToInt32(System.Convert.ToString(bytes[ 0 ])) - 160 ));
if (lowCode.Length == 1 )
{
lowCode = " 0 " + lowCode;
}
string hightCode = System.Convert.ToString(Math.Abs(Convert.ToInt32(System.Convert.ToString(bytes[ 1 ])) - 160 ));
if (hightCode.Length == 1 )
{
hightCode = " 0 " + hightCode;
}
coding += character.Substring(i, 1 ) + (lowCode + hightCode);
}
}
}
return coding;
}
}
{
public static void Main()
{
while ( true )
{
Console.Write( " 请输入(输入E退出): " );
string inputString = Console.ReadLine();
if (inputString == " e " || inputString == " E " )
{
break ;
}
else
{
Console.WriteLine(TextToQwm(inputString));
}
}
}
public static string TextToQwm( string character)
{
string coding = "" ;
int i1 = 0 ;
int i2 = 0 ;
int i3 = 0 ;
for ( int i = 0 ; i < character.Length; i ++ )
{
byte [] bytes = System.Text.Encoding.Default.GetBytes(character.Substring(i, 1 ));
i1 = ( short )(bytes[ 0 ]);
if (bytes.Length != 1 )
{
i2 = ( short )(bytes[ 1 ]);
i3 = 1 ;
}
else
{
i2 = 65535 ;
i3 = - 1 ;
}
int chrasc = i1 * 256 + i2 - 65536 ;
if (chrasc > 0 && chrasc < 160 )
{
Console.WriteLine( " 只能输入汉字. " );
}
else
{
if (i3 == - 1 )
{
Console.WriteLine( " 只能输入汉字 " );
}
else
{
string lowCode = System.Convert.ToString(Math.Abs(Convert.ToInt32(System.Convert.ToString(bytes[ 0 ])) - 160 ));
if (lowCode.Length == 1 )
{
lowCode = " 0 " + lowCode;
}
string hightCode = System.Convert.ToString(Math.Abs(Convert.ToInt32(System.Convert.ToString(bytes[ 1 ])) - 160 ));
if (hightCode.Length == 1 )
{
hightCode = " 0 " + hightCode;
}
coding += character.Substring(i, 1 ) + (lowCode + hightCode);
}
}
}
return coding;
}
}
这里要注意就去回汉字的字节是同过System.Text.Encoding.GetBytes来取得!还有以上代码都是一个类一类的形式所在每执行一个小类时候都要将其他类的Main()注释.
好这就是C#1.0,今天主要通过代码的实现知道C#的基本使用,精髓部分主要对我平时在学习中注意的问题和大家分享.在下一次将具体谈到C#1.0的类型.也还有实践部分!我想这样的亲密接触应该能感受到C#这种语言的功能强大!
要完成程序代码可以发E-mail ghl133@yahoo.com.cn
---------------------worksguo