我是C#(和OOP)的新手.当我有一些如下代码:
class Employee
{
// some code
}
class Manager : Employee
{
//some code
}
Run Code Online (Sandbox Code Playgroud)
问题1:如果我有其他代码执行此操作:
Manager mgr = new Manager();
Employee emp = (Employee)mgr;
Run Code Online (Sandbox Code Playgroud)
这Employee是一个Manager,但当我把它这样投射到Employee它意味着我正在向上倾斜它?
问题2:
当我有几个Employee类对象时,有些但不是全部都是它们Manager,我怎么能在可能的情况下将它们转发?
该var类型是否与VariantVB相同?何时object可以接受任何数据类型,这两者之间有什么区别?
最近我参加了一次采访.给我一个代码片段.我知道,面试官是从albhari的线程样本中获取的.
public static void Main()
{
try
{
new Thread (Go).Start();
}
catch (Exception ex)
{
// We'll never get here!
Console.WriteLine ("Exception!");
}
}
static void Go() { throw null; }
Run Code Online (Sandbox Code Playgroud)
将上述代码修改为
public static void Main()
{
new Thread (Go).Start();
}
static void Go()
{
try
{
...
throw null; // this exception will get caught below
...
}
catch (Exception ex)
{
Typically log the exception, and/or signal another thread
that we've come unstuck
...
}
}
Run Code Online (Sandbox Code Playgroud)
将是处理异常的好候选人. …
仅供我澄清:
我可以在.NET Framework中抛出CLS-Complaint和非CLS投诉异常吗?.我正在使用C#3.0.
当我抓住异常
catch(Exception ex)
{
}
Run Code Online (Sandbox Code Playgroud)
我在使用IUnknown接口时有点不清楚.IUnknown接口是否仅适用于COM/COM +对象和一些非托管代码,如Win API?
我正在阅读C#文章.它暗示着
在catch块的末尾,您有三个选择:
•重新抛出相同的异常,通知异常调用堆栈中的代码
.
•抛出不同的异常,为调用堆栈中较高的代码提供更丰富的异常信息.
•让螺纹脱离锁扣块的底部.
我无法理解这些要点.如果你通过一个简单的例子澄清它,那将是一个很大的帮助.
提前致谢.
更新: 当我需要处理rethrown异常时,我是否需要嵌套的try .. catch块之类的
try
{
try
{
}
catch(InvalidOperationException exp)
{
throw;
}
}
catch(Exception ex)
{
// handle the exception thrown by inner catch block
// (in this case the "throw" clause inside the inner "catch")
}
}
Run Code Online (Sandbox Code Playgroud) 当我指定时间间隔为零时
static void Main()
{
Thead.Sleep(0);
}
Run Code Online (Sandbox Code Playgroud)
Main()线程会放弃它获取的CPU资源还是不等待它会继续吗?
我从Jrista的帖子中回答了以下例子.
找到Twentyone计数
int[] numbers = new[] { 1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 3, 21 };
var twentyoneCount = numbers.Where(n => n == 21).Count();
Run Code Online (Sandbox Code Playgroud)
假设我使用"Func"委托如何获得计数?
我试过(原谅我的语法错误)
var otherway= Func <int> numbers= x => x==21;
Run Code Online (Sandbox Code Playgroud)