标签:循环 HERE this ber foreach ini 存储 inter lis
环球宝贝月子怎么样,环球宝贝新一代集团面试题有哪些,以下我整理一些我曾经去面试环球宝贝这家公司的一些C#面试题。
参考答案
static long TotalAllEvenNumbers(int[] intArray)
{
return intArray.Where(i => i % 2 == 0).Sum(i => (long)i);
}
static long TotalAllEvenNumbers(int[] intArray)
{
return (from i in intArray where i % 2 == 0 select (long)i).Sum();
}
关键点
class Program
{
static String location;
static DateTime time;
static void Main()
{
Console.WriteLine(location == null ? "location is null" : location);
Console.WriteLine(time == null ? "time is null" : time.ToString());
}
}
参考答案
location is null
1/1/0001 12:00:00 AM
解释如下:虽然两个变量都未初始化,但是String 是引用类型 ,而 DateTime 是值类型,其默认值为 1/1/1 而非 null
static DateTime time;
/* ... */
if (time == null)
{
/* do something */
}
参考答案
有人可能会认为,由于DateTime变量永远不能为空(它自动初始化为0001年1月1日),因此当DateTime变量与null进行比较时,编译器会报错。但是,由于类型转换,编译器确实允许它,这可能会导致一些让你头疼的问题。
具体来说,==运算符会将等号两边的对象都转换成相同的类型,然后可以进行比较。这就是为什么像这样的写法会给你你期望的结果(而不是因为操作数是不同的类型而导致失败或表现异常):
double x = 5.0;
int y = 5;
Console.WriteLine(x == y); // 输出true
但是,这有时会导致意外行为,比如DateTime变量和null的比较。在这种情况下,DateTime变量和null文字都可以强制转换为Nullable <DateTime>。因此,比较这两个值是合法的,即使结果总是false。
Circle 类的定义如下:
public sealed class Circle
{
// 半径
private double radius;
public double Calculate(Func<double, double> op)
{
return op(radius);
}
}
给你一个 Circle 的实例 circle,计算它的周长
参考答案
circle.Calculate(r => 2 * Math.PI * r);
由于我们无法访问对象的私有半径字段,因此我们告诉对象本身计算周长,方法是将计算函数传递给它。
许多C#程序员回避(或不理解)函数值参数。 虽然在这种情况下这个例子有点人为,但目的是看看申请人是否理解如何制定一个与Method的定义匹配的Calculate调用。
或者,有效(虽然不太优雅)的解决方案是从对象中检索半径值本身,然后使用结果执行计算:
var radius = circle.Calculate(r => r);
var circumference = 2 * Math.PI * radius;
无论哪种方式都有效。 我们主要想看到候选人熟悉并理解如何调用Calculate方法。
兵哥注: 我感觉添加一个静态扩展方法会更好
public static class CircleExtention
{
public static double GetCircumference(this Circle circle)
{
return circle.Calculate(r => Math.PI * r * 2);
}
}
class Program
{
private static int result ;
static void Main()
{
var task = SaySomething();
Console.WriteLine(result);
}
static async Task<int> SaySomething()
{
Console.WriteLine(1);
await Task.Delay(50);
Console.WriteLine(2);
result = 3;
return 4;
}
}
参考答案
输出为
1
0
tip 由于主线程没有等待子线程结束,所以子线程在主线程结束后立即被释放 所以 Console.WriteLine(2); 未被执行
兵哥注
对于 async 方法SaySomething被主线程调用时,主线程会一直执行async的方法,直到遇到await , 主线程将会从线程池中获取一个空闲的子线程,并把await 的Task.Delay方法 以及SaySomething中位于 await 后的所有代码交给子线程处理
可使用一下代码验证
class Program
{
private static int result;
static void Main()
{
Console.WriteLine($"Main Thread:{Thread.CurrentThread.ManagedThreadId}");
var task = SaySomething();
Console.WriteLine(result);
Console.WriteLine(task.Result);
}
static async Task<int> SaySomething()
{
Console.WriteLine($"SaySomething Thread before await:{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine(1);
await Task.Delay(50);
Console.WriteLine($"SaySomething Thread after await:{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine(2);
result = 3;
return 4;
}
}
class Program
{
delegate void Printer();
static void Main()
{
List<Printer> printers = new List<Printer>();
int i = 0;
for (; i < 10; i++)
{
printers.Add(delegate { Console.WriteLine(i); });
}
foreach (var printer in printers)
{
printer();
}
}
}
参考答案
将会输出10个10
因为所有的委托中的变量i都是指向同一块内存地址
兵哥注
对比代码如下
class Program
{
delegate void Printer();
static void Main()
{
List<Printer> printers = new List<Printer>();
int i = 0;
for (; i < 10; i++)
{
var j = i;
printers.Add(delegate { Console.WriteLine(j); });
}
foreach (var printer in printers)
{
printer();
}
}
}
参考答案 可以,因为C#中所有的对象都继承自Object,所以可以使用object[] array = new object[3]; 来存储
参考答案
特点 | class | struct |
---|---|---|
是否支持继承 | 支持 | 不支持 |
引用类型/值类型 | 引用类型 | 值类型 |
值是否可为null | 是 | 否 |
新的实例是否会消耗没存 | 是 | 否(除非被装箱) |
public class TestStatic
{
public static int TestValue = InitTestValue();
private static int InitTestValue()
{
if (TestValue == 0)
{
TestValue = 20;
}
return 30;
}
public TestStatic()
{
if (TestValue == 0)
{
TestValue = 5;
}
}
static TestStatic()
{
if (TestValue == 0)
{
TestValue = 10;
}
}
public void Print()
{
if (TestValue == 5)
{
TestValue = 6;
}
Console.WriteLine("TestValue : " + TestValue);
}
}
public static void Main(string[] args)
{
TestStatic t = new TestStatic();
t.Print();
}
参考答案 TestValue:30
执行顺序:静态变量赋值表达式=>静态构造函数=》构造函数
static void Main(string[] args)
{
try
{
Console.WriteLine("Hello");
throw new ArgumentNullException();
}
catch (ArgumentNullException)
{
Console.WriteLine("A");
}
catch (Exception)
{
Console.WriteLine("B");
}
finally
{
Console.WriteLine("C");
}
Console.ReadKey();
}
参考答案
Hello
A
C
标签:循环 HERE this ber foreach ini 存储 inter lis
原文地址:https://www.cnblogs.com/huanqiujtuan/p/10511216.html