关于c#的各种疑惑 [英] miscellaneous doubts on c#

查看:81
本文介绍了关于c#的各种疑惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有些疑惑>



1)什么是默认容量一个集合(比如哈希表或列表等)?

2)假设我说:



int [] d; ////编译时错误

static int [] d; ////运行时错误



为什么会这样?



3)为什么我们不能在try和catch语句后的finally子句中添加return语句?



4)this关键字的确切含义是什么?





谢谢和问候

Hello,

I have some doubts>

1) What is the default capacity of a collection(say a hashtable or a list etc)?
2) Suppose i say :

int[] d; ////compile time error
static int[] d;////run time error

why so?

3) Why can we not add a return statement in the "finally" clause after the try and catch statement?

4) What is the exact significance of "this" keyword?


Thanks and regards

推荐答案

1)没有一个定义 - 这意味着它不是你应该依赖的任何东西,因为它是一个实现细节,如有变更,恕不另行通知。使用Reflector进行快速检查说List和Dictionary初始化为空,但是以不同的方式增加它们的大小。

2)从什么时候开始?

3)因为当你有在C#中的finally子句中,C#规范声明 finally子句中的每个语句都必须执行。因此,不能在finally子句中使用return语句,因为它可能允许语句错过。另外,这里会发生什么?

1) There isn't one defined - which means that it isn't anything your should rely on as it's an implementation detail and subject to change without notice in future updates. A quick check with Reflector says that List and Dictionary are initialized to empty, but increase their size in different ways.
2) Since when?
3) Because when you have a finally clause in C#, the C# specification states that every statement within the finally clause must execute. Because of this, it is not possible to use a return statement within a finally clause as it could allow statements to be "missed". In addition, what should happen here?
private int MyMethod()
    {
    try
        {
        return SomeFunctionReturningInt();
        }
    finally
        {
        return -1;
        }
    }

必须在最终 c>阻止,然后最终尝试建立返回值?讨厌!

4)这个是运行时类的当前实例。大部分时间你根本不需要它,但它允许你将当前实例传递给方法:

SomeFunctionReturningInt would have to be called before the finally block, and then the finally tries to establish the return value? Nasty!
4) this is the current instance of the class at runtime. Most of teh time you don't need it at all, but it allows you to pass the current instance to a method:

int i = GetValue(this);

或者明确说明当本地值覆盖类时你的意思级别值:

Or to clarify exactly which value you mean when a local value overrides a class level value:

private int setting;
private void DoSomething(int setting)
    {
    this.setting = setting;
    }


这篇关于关于c#的各种疑惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆