没有合适的资源?快使用搜索试试~ 我知道了~
首页最新C#/.Net/数据库笔试资料大整理
最新C#/.Net/数据库笔试资料大整理
需积分: 10 28 下载量 112 浏览量
更新于2023-03-16
评论
收藏 339KB DOC 举报
最新C#/.Net/数据库笔试资料大整理,找了很久 整理了很久 现在无偿奉献出来 希望对大家能有所帮助
资源详情
资源评论
资源推荐
C#/.Net/数据库笔试资料
C#资料(一)
1.静态成员和非静态成员的区别?
答:
静态变量使用 static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static
修饰符声明的变量称做非静态变量,在对象被实例化时创建,通过对象进行访问一个类的
所有实例的同一静态变量都是同一个值,同一个类的不同实例的同一非静态变量可以是不
同的值静态函数的实现里不能使用非静态成员,如非静态变量、非静态函数等
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example01
{
class Program
{
class Class1
{
public static String staticStr = "Class";
public String notstaticStr = "Obj";
}
static void Main(string[] args)
{
//静态变量通过类进行访问,该类所有实例的同一静态变量都是同一个值
Console.WriteLine("Class1's staticStr: {0}", Class1.staticStr);
Class1 tmpObj1 = new Class1();
tmpObj1.notstaticStr = "tmpObj1";
Class1 tmpObj2 = new Class1();
tmpObj2.notstaticStr = "tmpObj2";
//非静态变量通过对象进行访问,不同对象的同一非静态变量可以有不同的值
Console.WriteLine("tmpObj1's notstaticStr: {0}", tmpObj1.notstaticStr);
Console.WriteLine("tmpObj2's notstaticStr: {0}", tmpObj2.notstaticStr);
Console.ReadLine();
}
}
}
结果:
Class1's staticStr: Class
tmpObj1's notstaticStr: tmpObj1
tmpObj2's notstaticStr: tmpObj2
2.const 和 static readonly 区别?
答:const
用 const 修饰符声明的成员叫常量,是在编译期初始化并嵌入到客户端程序
static readonly
用 static readonly 修饰符声明的成员依然是变量,只不过具有和常量类似的使用方法:通过
类进行访问、初始化后不可以修改。但与常量不同的是这种变量是在运行期初始化
示例:
测试类:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example02Lib
{
public class Class1
{
public const String strConst = "Const";
public static readonly String strStaticReadonly = "StaticReadonly";
//public const String strConst = "Const Changed";
//public static readonly String strStaticReadonly = "StaticReadonly
Changed";
}
}
客户端代码:
using System;
using System.Collections.Generic;
using System.Text;
using Example02Lib;
namespace Example02
{
class Program
{
static void Main(string[] args)
{
//修改 Example02 中 Class1 的 strConst 初始值后,只编译 Example02Lib 项目
//然后到资源管理器里把新编译的 Example02Lib.dll 拷贝 Example02.exe 所在的目录,
执行 Example02.exe
//切不可在 IDE 里直接调试运行因为这会重新编译整个解决方案!!
//可以看到 strConst 的输出没有改变,而 strStaticReadonly 的输出已经改变
//表明 Const 变量是在编译期初始化并嵌入到客户端程序,而 StaticReadonly 是在运
行时初始化的
Console.WriteLine("strConst : {0}", Class1.strConst);
Console.WriteLine("strStaticReadonly : {0}", Class1.strStaticReadonly);
Console.ReadLine();
}
}
}
结果:
strConst : Const
strStaticReadonly : StaticReadonly
修改后的示例:
测试类:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example02Lib
{
public class Class1
{
//public const String strConst = "Const";
//public static readonly String strStaticReadonly = "StaticReadonly";
public const String strConst = "Const Changed";
public static readonly String strStaticReadonly = "StaticReadonly Changed";
}
}
结果
strConst : Const
strStaticReadonly : StaticReadonly Changed
3.extern 是什么意思?
答:extern 修饰符用于声明由程序集外部实现的成员函数经常用于系统 API 函数的调用
(通过 DllImport )。注意,和 DllImport 一起使用时要加上 static 修饰符也可以用于对于
同一程序集不同版本组件的调用(用 extern 声明别名)不能与 abstract 修饰符同时使用
示例:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Example03
{
class Program
{
//注意 DllImport 是一个 Attribute Property,在 System.Runtime.InteropServices 命名空间
中定义
//extern 与 DllImport 一起使用时必须再加上一个 static 修饰符
[DllImport("User32.dll")]
public static extern int MessageBox(int Handle, string Message,
string Caption, int Type);
static int Main()
{
string myString;
Console.Write("Enter your message: ");
myString = Console.ReadLine();
return MessageBox(0, myString, "My Message Box", 0);
}
}
}
结果:
4.abstract 是什么意思?
答:abstract 修饰符可以用于类、方法、属性、事件和索引指示器(indexer),表示其为抽
象成员.abstract 不可以和 static 、virtual 一起使用声明为 abstract 成员可以不包括实现代码,
但只要类中还有未实现的抽象成员(即抽象类),那么它的对象就不能被实例化,通常用
于强制继承类必须实现某一成员
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example04
{
#region 基类,抽象类
public abstract class BaseClass
{
//抽象属性,同时具有 get 和 set 访问器表示继承类必须将该属性实现为可读写
public abstract String Attribute
{
get;
set;
}
//抽象方法,传入一个字符串参数无返回值
public abstract void Function(String value);
//抽象事件,类型为系统预定义的代理(delegate):EventHandler
public abstract event EventHandler Event;
//抽象索引指示器,只具有 get 访问器表示继承类必须将该索引指示器实现为只读
public abstract Char this[int Index]
{
get;
}
}
#endregion
#region 继承类
public class DeriveClass : BaseClass
{
private String attribute;
public override String Attribute
{
get
{
return attribute;
}
set
{
剩余63页未读,继续阅读
yzy11
- 粉丝: 24
- 资源: 73
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- ExcelVBA中的Range和Cells用法说明.pdf
- 基于单片机的电梯控制模型设计.doc
- 主成分分析和因子分析.pptx
- 共享笔记服务系统论文.doc
- 基于数据治理体系的数据中台实践分享.pptx
- 变压器的铭牌和额定值.pptx
- 计算机网络课程设计报告--用winsock设计Ping应用程序.doc
- 高电压技术课件:第03章 液体和固体介质的电气特性.pdf
- Oracle商务智能精华介绍.pptx
- 基于单片机的输液滴速控制系统设计文档.doc
- dw考试题 5套.pdf
- 学生档案管理系统详细设计说明书.doc
- 操作系统PPT课件.pptx
- 智慧路边停车管理系统方案.pptx
- 【企业内控系列】企业内部控制之人力资源管理控制(17页).doc
- 温度传感器分类与特点.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0