大家好,这是自己自行整理的c#面试题,方便自己学习的同时分享出来。
using System;
public abstract class AbstProgram // 必须声明为抽象类
{
public abstract string abstMethod(); // 抽象方法
public virtual void virMethod() // 虚方法
{
Console.WriteLine("hello, virmethod");
}
}
public class Subclass: AbstProgram
{
// 抽象方法必须实现
public override string abstMethod()
{
return "hello,abstMethod";
}
// 虚方法可以实现,也可以选择不实现
//public override void virMethod()
//{
// Console.WriteLine("hello,virmethod");
//}
}
public interface IMyInterface // 接口
{
public void intMethod();
}
public abstract class AbstProgram // 抽象类
{
public abstract void abstMethod();
}
public class SubClass: AbstProgram, IMyInterface
{
public override void abstMethod()
{
Console.WriteLine("hello,abstMethod");
}
public void intMethod()
{
Console.WriteLine("hello,intMethod");
}
}
// 定义接口
interface IMyInterface
{
void MethodToImplement(); // 接口成员
}
class InterfaceImplementer: IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}
// 类实现接口
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}
public abstract class Program
{
public abstract string abstmethod(); // abstract
public virtual void virmethod() // virtual
{
Console.WriteLine("hello,virmethod");
}
}
public sealed class SubClass: Program // sealed
{
public override string abstmethod() // override
{
return "hello,abstmethod";
}
}
using System;
class Program
{
public static readonly int NumberA = NumberB * 10;
public static readonly int NumberB = 10;
public const int NumberC = NumberD * 10;
public const int NumberD = 10;
static void Main(string[] args)
{
Console.WriteLine("NumberA is {0}, NumberB is {1}.", NumberA, NumberB); //NumberA is 0, NumberB is 10.
Console.WriteLine("NumberC is {0}, NumberD is {1}.", NumberC, NumberD); //NumberC is 100, NumberD is 10.
Console.ReadLine();
}
}
public class Program
{
// 重载 (overload)
public void method(int num1)
{
}
public void method(string str1)
{
}
// 虚方法
public virtual void virtmethod()
{
Console.WriteLine("hello,virtmethod");
}
}
public class SubClass: Program
{
// 重写 (override)
public override void virtmethod()
{
Console.WriteLine("hello,overload");
}
}
// Books结构体实现
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
class Program
{
public void test1(ref int i)
{
i = 99;
}
public void test2(out int i)
{
i = 11;
}
static void Main(string[] args)
{
Program p = new Program();
int i = 1;
p.test1(ref i); // i = 99
int j;
p.test2(out j); // j = 10
}
}
//引用类型(由于使用了'Class')
class SomeRef { public Int32 x; }
//值类型(由于使用了'Struct')
struct SomeVal { public Int32 x; }
class Program
{
static void ValueTypeDemo()
{
SomeRef r1 = new SomeRef();
SomeVal v1 = new SomeVal();
r1.x = 5;
v1.x = 5;
Console.WriteLine(r1.x); //显示"5"
Console.WriteLine(v1.x); //同样显示"5"
SomeRef r2 = r1;
SomeVal v2 = v1;
r1.x = 8;
v1.x = 9;
Console.WriteLine(r1.x); //显示"8"
Console.WriteLine(r2.x); //显示"8"
Console.WriteLine(v1.x); //显示"9"
Console.WriteLine(v2.x); //显示"5"
}
}
public class Solution
{
static void Main(string[] args)
{
int i = 123; //声明一个int类型的变量i,并初始化为123
object obj = i; //执行装箱操作
Console.WriteLine("装箱操作:值为{0},装箱之后对象为{1}", i, obj);
int j = (int)obj; //执行拆箱操作
Console.WriteLine("拆箱操作:装箱对象为{0},值为{1}", obj, j);
}
}
delegate int NumberChanger(int n);
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int GetNum()
{
return num;
}
static void Main(string[] args)
{
// 创建委托实例
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
// 使用委托对象调用方法
nc1(25);
Console.WriteLine("Value of Num: {0}", GetNum()); // Value of Num: 35
nc2(5);
Console.WriteLine("Value of Num: {0}", GetNum()); // Value of Num: 175
Console.ReadKey();
}
}
public class Student
{
public int Age { get; }
public string Name { get; }
public Student()
{
Console.WriteLine("对象被创建。");
}
public Student(string name) : this()
{
this.Name = name;
}
public Student(string name, int age) : this(name)
{
this.Age = age;
}
}
public class Program
{
static void Main(string[] args)
{
Student student_1 = new Student();
Student student_2 = new Student("姓名", 14);
Console.WriteLine(student_2.Name + ' ' + student_2.Age);
}
}
class DemoProgram { };
sealed class SealedProgram: DemoProgram { };
public class Solution
{
static void Main()
{
var testTask = new Task(() =>
{
Console.WriteLine("hello, Task");
});
Console.WriteLine(testTask.Status); // Created
testTask.Start();
}
}
public class Solution
{
static void Main(string[] args)
{
ThreadTest test = new ThreadTest(); //创建ThreadTest类的一个实例
Thread thread = new Thread(new ThreadStart(test.MyThread)); //调用test实例的MyThread方法
thread.Start(); //启动线程
Console.ReadKey();
}
}
public class ThreadTest
{
public void MyThread()
{
Console.WriteLine("这是一个实例方法");
}
}
// where T:new()指明了创建T的实例时应该具有构造函数
class ItemFactory<T> where T : new()
{
public T GetNewItem()
{
return new T();
}
}
class Class1
{
};
class Prorgam
{
Class1 o = new Class1(); // 用于创建对象和调用构造函数
int myInt = new int(); // 也用于为值类型调用默认的构造函数
}
using System; // 引用命名空间;
using myClass1 = NameSpace1.MyClass; // 为命名空间或类型创建别名;
using myClass2 = NameSpace2.MyClass;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
myClass1 my1 = new myClass1();
Console.WriteLine(my1);
using (myClass2 my2 = new myClass2()) // 释放资源(关闭文件流);
{
Console.WriteLine(my2);
}
}
}
}
namespace NameSpace1
{
public class MyClass
{
public override string ToString()
{
return "hello, myClass1";
}
}
}
namespace NameSpace2
{
public class MyClass: IDisposable
{
public void Dispose()
{
throw new NotImplementedException();
}
public override string ToString()
{
return "hello, myClass2";
}
}
}
class Solution
{
static int Fib(int n)
{
if (n <= 0)
return 0;
if (n < 3)
return 1;
return Fib(n - 1) + Fib(n - 2);
}
static void Main(string[] args)
{
Console.WriteLine(Fib(30));
}
}
本章部分图文内容来源于各网站资料与网友提供, 笔者整理收集,方便学习参考,版权属于原作者。