文档库 最新最全的文档下载
当前位置:文档库 › CSharp复习

CSharp复习

CSharp复习
CSharp复习

一、填空题、简答题 1、关于C#的开发环境

(1)NET FrameWork 包含多种语言编译器。

(2).NET FrameWork 是Visual https://www.wendangku.net/doc/e011454014.html, 应用程序开发环境的核心。 (3)NET FrameWork 提供了代码执行安全性的代码执行环境。 (4).NET 应用程序的开发和运行必须安装.NET FrameWork 。 (5) CTS 为结构和类分配了固定的内存 (6) CTS 允许不同语言开发的组件互相操作 (7) CTS 用于实施类型安全性

(8)解决方案管理器---文件 几个文件

(9) 类视图窗口--当前项目的类和类型的层次 几个类

(10)Main 函数的完整写法 应用程序中Main 函数不带参数,控制台程序带参数。 (11) Main 函数是所有程序的入口。

3.字符串运算

String t1=”中华人民共和国”;

String t2=t1.substring(2,3);

String t1=lastindexof(“共”);

String t3=t1.substring(p,t1.length-p); 4.简单的数学符号运算。 int p=123456; p=p/100%10; 结果是p=4

5.for /while/switch 语句的理解。 int s=0;

for (int i=1;i<5;i++) {s+=I;} //s=s+i int s=0 int i=1 while(i<5) {s+=i;i++;} int a=6,b=9,c=1; switch (a%b+c)

{case 1:c=a+b;break; case 7:c=a*b;break; case 10:c=a-b;break; default;c=0;} →C=54

6、子类与父类继承的写法及用? 9、类、对象创建,构造函数 10、方法重载、覆盖的定义。

共和国

Program.cs

using System;

using System.Collections.Generic;

using System.Text;

namespace SamepleDemoDetrived

{

class Program

{

static void Main(string[] args)

{

Person p1 = new Person();//无参数的构造函数

Console.WriteLine("{0},{1},{2}", https://www.wendangku.net/doc/e011454014.html,, p1.Sex, p1.Age);

Person p2 = new Person("王小明", '男', 18);

Console.WriteLine("{0},{1},{2}", https://www.wendangku.net/doc/e011454014.html,, p2.Sex, p2.Age);

Student s1 = new Student("09ds01B004", "王小明", '男', 18);

Console.WriteLine("{0},{1},{2},{3}", s1.StudentID, https://www.wendangku.net/doc/e011454014.html,, s1.Sex, s1.Age);

}

}

}

Student.cs

using System;

using System.Collections.Generic;

using System.Text;

namespace SamepleDemoDetrived

{

class Student:Person

{

string studentID;

public Student(string sID, string n, char s, int a)

: base(n, s, a)

{

StudentID = sID;

}

public string StudentID//类的属性

{

set

{

studentID = value;

}

get

{

return studentID;

}

}

}

Person.cs

using System;

using System.Collections.Generic;

using System.Text;

namespace SamepleDemoDetrived

{

class Person

{

string name;

char sex;

int age;

public Person(string n, char s, int a)//带参数的构造函数

{

name = n;

sex = s;

age = a;

}

public Person()//不带参数的构造函数

{

name = " ";

sex = 'M';

age = 0;

}

public string Name//类的属性

{

set

{

name = value;

}

get

{

return name;

}

}

public char Sex//类的属性

{

set

{

sex = value;

get

{

return sex;

}

}

public int Age//类的属性

{

set

{

age = value;

}

get

{

return age;

}

}

}

}

① 三个类:person.cs,program.cs,student.cs

② student继承person(student:person)

③ program包含Name()引用person和student类

④ student类:子/扩张类Person类:父/基类

⑤ 如果person类中的变量或方法不是私有的,student类中可以访问它

⑥ this 表示当前实例,通过它可以访问类本身的成员base标识父类,可以访问父类的的成

员,直接调用父类的属性和方法

⑦ 重载/覆盖子类与父类中有一个方法相同,则子类的方法覆盖父类的方法

⑧ 构造函数(作用,参数,写法)定义一个类的对象,通过new关键词获取对象

⑨ 子类如何调用父类构造函数?(base)

⑩ 变量/属性作用与区别

? 运行结果

7、常用控件如Label, Timer, ListBox, OpenFileDialog

Label:是标准的windows标签

Timer:按用户定义的间隔引发事件的组件

ListBox:显示用户可以从中选择项的列表

OpenFilesDialog:显示一个对话框,提示用户打开文件

11.创建ArrayList需要引入命名空间?要遍历集合的元素必须实现什么接口?

1、包含4个基接口:IEnumerable、IEnumerator、IComparer、IEqualityComparer

2、IEnumerable和IEnumerator接口:

--> IEnumerator:可理解为一个“实现型”的接口,定义了枚举器(要支持foreach方式的遍历所需要)的具体内容。

包含:MoveNext(),Reset()两个方法和一个只读属性Current(表示取得的枚举数/对象)。

--> IEnumerable:可理解为一个“声明式”的接口,用来声明派生的类是可枚举的。

只包含一个方法:GetEnumerator(),返回的是IEnumerator类型。

12.try…catch语句的作用?

捕捉异常,给出异常提示

14.数据库编程的基本步骤及常用控件。

二、阅读程序,回答问题。

1.给定一个程序的结果

2.根据程序将While改成for,或将for改成While,程序功能保持不变

3.理解子类与父类(继承怎么写,变量,属性,子类构造函数,父类构造函数,程序运行结果)

三、编程题。

排序算法,求平均值算法,接口实现算法,抽签程序。

冒泡排序

using System;

using System.Collections.Generic;

using System.Text;

namespace Bubble

{

class Program

{

static void Main(string[] args)

{

const int n = 5;

int[] arrNumber = new int[n];

for (int i = 0; i < n; i++)

{

int tNumber = int.Parse(System.Console.ReadLine());

arrNumber[i] = tNumber;

}

for(int i=0;i

{

int k=n-1;

for(int j=k;j>i;j--)

{

bool flag=false;

if (arrNumber[j]

{

flag=true;

}

if(flag)

{

int temp=arrNumber[j];

arrNumber[j]=arrNumber[j-1];

arrNumber[j-1]=temp;

}

}

}

for (int i=0;i

{System.Console.WriteLine("arrNumber[{0}]:{1}",i,arrNumber[i]);

}

}

}

}

求平均值

using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;

namespace Average

{

class Program

{

static protected ArrayList arrList = new ArrayList();

static void Main(string[] args)

{

System.Console.Write("请输入要求平均值数值的个数:");

int count = int.Parse(System.Console.ReadLine());

for (int i = 0; i < count; i++)

{

string strNumber = System.Console.ReadLine();

arrList.Add(strNumber);

}

ShowAverage();

}

protected static void ShowAverage()

{

double sum = 0;

int count = 0;

IEnumerator enumerator = arrList.GetEnumerator();

while (enumerator.MoveNext())

{

string strTemp = enumerator.Current.ToString();

double dbTemp = double.Parse(strTemp);

sum += dbTemp;

count++;

}

double average = sum / count;

System.Console.WriteLine("以上数据的平均值为:{0}", average);

}

}

}

接口实现算法

新建一个控制台项目,名为DemoInterface

在Main函数中添加如下代码:

Rectangle Myre = new Rectangle(2, 8);

Console.WriteLine(Myre.GetArea());

Circle MyCur = new Circle(10);

Console.WriteLine(MyCur.GetArea());

单击”项目”→”添加类”→添加一个接口,名为Ishape.cs,并添加如下代码:interface Ishape

{

double GetArea();//抽象方法只能写成这个样子

}

单击”项目”→”添加类”→添加一个类,名为Rectangle.cs,并添加如下代码:class Rectangle : Ishape //实现接口

{

public double width;

public double height;

public Rectangle(double width, double height)

{

this.width = width;

this.height = height;//构造函数

}

public double GetArea()

{

return this.height * this.width;

}

}

再次单击”项目”→”添加类”→添加一个类,名为Circle.cs,并添加如下代码:class Circle : Ishape //实现接口

{

public double radius;

public Circle(double radius)

{

this.radius = radius; //构造函数

}

public double GetArea()

{

return (Math.PI) * this.radius * this.radius;

}

}

抽签程序

using System;

using System.Collections.Generic;

using https://www.wendangku.net/doc/e011454014.html,ponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Collections;

namespace draw_lots

{

public partial class MainForm : Form

{

static protected ArrayList arrList = new ArrayList();

static protected ArrayList GropNameList = new ArrayList();

static protected ArrayList IndexGropNameList = new ArrayList();

public MainForm()

{

InitializeComponent();

}

private void OpenFilebtn_Click(object sender, EventArgs e)

{

if (this.openFileDialog1.ShowDialog() == DialogResult.OK)

{

if (openFileDialog1.FileName != "")

{

string temp = "";

StreamReader myread = new StreamReader(openFileDialog1.FileName, Encoding.Default);

try

{

string mys = myread.ReadLine();

int i = 0;

while (mys != null)

{

arrList.Add(mys);

string[] strArray = mys.ToString().Split(',');//按班级分割

temp = strArray[1];

https://www.wendangku.net/doc/e011454014.html,boBoxClassList.Items.Add(temp);//汇集班级

mys = myread.ReadLine();

i++;

}

if(https://www.wendangku.net/doc/e011454014.html,boBoxClassList.Items.Count>0)

https://www.wendangku.net/doc/e011454014.html,boBoxClassList.Text ="请选择班级";

}

catch (Exception mye)//

{

MessageBox.Show("文件读数据出错!" + mye.ToString());

}

finally

{

myread.Close();//

}

}

}

}

private void comboBoxClassList_SelectedIndexChanged(object sender, EventArgs e)

{

IEnumerator enumerator = arrList.GetEnumerator();

this.listBoxGroupName.Items.Clear();

GropNameList.Clear();

IndexGropNameList.Clear();

while (enumerator.MoveNext())

{

string tempstring1 = enumerator.Current.ToString();

string[] strArray = tempstring1.ToString().Split(',');

if (https://www.wendangku.net/doc/e011454014.html,boBoxClassList.Text == strArray[1])

{

tempstring1=tempstring1.Substring(8, tempstring1.Length - 8);

tempstring1=tempstring1.Replace("(","");

tempstring1=tempstring1.Replace(")","");

string[] strArraytemp = tempstring1.ToString().Split(';');;

for (int i = 0; i < strArraytemp.Length; i++)

{

string temp = strArraytemp[i].ToString();

GropNameList.Add(temp);

IndexGropNameList.Add(temp.Substring(0, https://www.wendangku.net/doc/e011454014.html,stIndexOf("组")));//汇集组号

this.listBoxGroupName.Items.Add(temp);//汇集组号及成员名单

}

break;

}

}

}

相关文档