开始学习,c#
第一个测试代码(控制台应用程序):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The first app in Beginning C# Programming !");
Console.ReadKey();
}
}
}
第二个测试用例(Windows应用程序):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("The first Windows app in the book!");
}
}
}
测试用例三(运算符重载):
#region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The first app in Beginning C# Programming !");
int myInteger;
string myString;
myInteger = 17;
myString = "\"myInteger\" \n is ";
Console.WriteLine("{0} {1}.",myString ,myInteger);
double firstNumber, secondNumber;
string userName;
Console.WriteLine("Enter your name:");
userName = Console.ReadLine();
Console.WriteLine("Welcome {0} !",userName );
Console.WriteLine("Now give me a number:");
firstNumber = Convert.ToDouble(Console .ReadLine ());
Console.WriteLine("Now give me another number:");
secondNumber = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("The sum of {0} and {1} is {2}.\n" ,firstNumber ,secondNumber ,firstNumber +secondNumber );
Console.WriteLine("The sub of {0} and {1} is {2}.\n", firstNumber, secondNumber, firstNumber -secondNumber);
Console.WriteLine("The * of {0} and {1} is {2}.\n", firstNumber, secondNumber, firstNumber * secondNumber);
Console.WriteLine("The / of {0} and {1} is {2}.\n", firstNumber, secondNumber, firstNumber / secondNumber);
Console.WriteLine("The % of {0} and {1} is {2}.\n", firstNumber, secondNumber, firstNumber % secondNumber);
Console.ReadKey();
}
}
}
测试用例四(结构和枚举)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
enum orientation : byte
{
north =1,
south =2,
east =3,
west =4
}
struct rouet
{
public orientation direction;
public double distance;
}
class Program
{
static void Main(string[] args)
{
rouet myRoute;
int myDirection = -1;
double myDistance;
Console.WriteLine("1) North \n 2) South \n 3) East \n 4) West");
do
{
Console.WriteLine ("Select a direction:");
myDirection = Convert.ToInt32(Console .ReadLine ());
}
while ((myDirection < 1) || (myDirection > 4));
Console.WriteLine("input a distance:");
myDistance = Convert.ToDouble( Console.ReadLine() );
myRoute.direction = (orientation)myDirection;
myRoute.distance = myDistance;
Console.WriteLine("myRoute specifies a direction of {0} and a " +
"distance of {1} ",myRoute .direction ,myRoute .distance );
Console.ReadKey();
}
}
}
测试用例五(使用数组)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string[] friendNames = {"Robert Barwell","Mike Parry",
"Jeremy Beacock"};
int i;
Console.WriteLine("Here are {0} of my friends:",
friendNames .Length );
for (i = 0; i < friendNames.Length; i++)
{
Console.WriteLine(friendNames [i]);
}
Console.ReadKey();
}
}
}
c#里面还有可以使用foreach来表示循环,好处是,可以对数组中的值的访问做到只读访问,不会改变数组 中的值。
C#中关于字符串的处理:可以定义string类型。
string类型的变量可以看成是char变量的只读数组。也就是说string类型实际上是一个字符串数组。
定义好的string类型,可以使用这个类型的方法,来做如下 的操作:
改变大小写、消去空格的影响、添加空格使字符串达到指定长度等等一些个字符处理功能
测试用例六(函数相关)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static int MaxValue(int[] intArray)
{
int maxVal = intArray[0];
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
maxVal = intArray[i];
}
return maxVal;
}
static void Main(string[] args)
{
int[] myArray = {1,8,3,6,2,5,9,3,0,2 };
int maxVal = MaxValue(myArray);
Console.WriteLine("The maximm value is {0}" ,maxVal );
Console.ReadKey();
}
}
}
关于函数参数中的两个关键字ref和out
ref例子:
定义一个方法:
public void SetValue(ref int count)
{
count = count * 12;
}
调用方法:
int count = 10;
SetValue(ref count);
Console.Write(count);//这里count为120;
=========================================================
out 例子:
定义一个方法:
public viod SetValue(out int count)
{
count = 15;
count = count * 10;
}
调用该方法:
int count;//注意这里可以不初始化;
SetValue(count);
Cosole.Write(count);//这里count为:150
关于全局变量:
Static修饰的全局变量可以改变它的值,但是const修饰的值不可以改变。
测试用例七(命令行参数)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0} command line arguments were specified:",
args.Length );
foreach (string arg in args)
Console.WriteLine(arg);
Console.ReadKey();
}
}
}
从解决方案右键里面的属性里面传入参数,可以直接打印在控制台中。、
测试用例八(结构函数)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
customerName myCustomer;
myCustomer.firstName = "hai";
myCustomer.lastName = "wen";
Console.WriteLine("{0},{1}",myCustomer .firstName,myCustomer .lastName);
Console.WriteLine(myCustomer .Name ());
}
struct customerName
{
public string firstName, lastName;
public string Name()
{
return firstName + "bobo" + lastName;
}
}
}
}
结构体中可以有函数。