language-features

Why use Clone()?

不想你离开。 提交于 2019-12-04 08:14:00
What is main purpose of using Clone() in C#? What is the benefit of using it? Jon The idea is that using Clone you can create a new object of the same type as the one you invoke it on, without knowing the exact type of the object you are invoking it on . For example: void Test(ICloneable original) { var cloned = original.Clone(); } Here cloned is of the same runtime type as original , and you did not need to know what that type was to perform the cloning. However the usefulness of ICloneable is pretty much none, because it does not define the semantics of the clone operation: is it a shallow

Is this the correct way of putting HTML in PHP?

丶灬走出姿态 提交于 2019-12-04 04:35:18
问题 I'm new to PHP, and most of the time I have been 'echo'ing my HTML. Today, I found this way of doing it, which makes things 1000 times easier: <?php if(get_field('field_name')){ ?> <p>HTML can go here without echo'ing it</p> <?php } ?> But is it an accepted way? It seems to work every time. Thanks. 回答1: It's mostly about how you like to read your code. With IDE that has autocomplete/code suggestions it is best to use <?php if (...) { ?> so that autocompletion/suggestion features know when to

What is the prefered style for single decision and action statements?

会有一股神秘感。 提交于 2019-12-04 04:11:25
问题 In the case of languages that support single decision and action without brackets, such as the following example: if (var == true) doSomething(); What is the preferred way of writing this? Should brackets always be used, or should their usage be left as a preference of the individual developer? Additionally, does this practice depend on the size of the code block, such as in the following example: if (var == 1) doSomething(1); else if (var > 1 && var < 10) doSomething(2); else { validate(var)

Why can't I use an array index on the return value of a function? [closed]

笑着哭i 提交于 2019-12-04 03:36:33
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . Why can't I do this? explode(',','1,2,3', 1)[0] Every other language supports it. Answers I'm looking for: (since people seem to think this is a pointless rant) Is there some sort of a difference between how a

Which “C# Experimental language feature” is this?

て烟熏妆下的殇ゞ 提交于 2019-12-04 03:03:36
问题 In the example below, Resharper shows "C# Experimental language feature" tooltip on the first curly bracket. I've checked the new features of C# 6.0 but didn't come across a similar one. What is the referred experimental feature? class Class1 { { // <= C# Experimental language feature } } Note: It is an error for .Net Framework 4.5 compiler. "Invalid token '{' in class, struct, or interface member declaration" 回答1: This was for Primary Constructors, a feature which has now been cut from C#6.

Why are there finalizers in java and c#?

天涯浪子 提交于 2019-12-04 02:53:01
I'm not quite understanding why there are finalizers in languages such as java and c#. AFAIK, they: are not guaranteed to run (in java) if they do run, they may run an arbitrary amount of time after the object in question becomes a candidate for finalization and (at least in java), they incur an amazingly huge performance hit to even stick on a class. So why were they added at all? I asked a friend, and he mumbled something about "you want to have every possible chance to clean up things like DB connections", but this strikes me as a bad practice. Why should you rely on something with the

Python 3.0 - dict methods return views - why?

我的梦境 提交于 2019-12-04 00:55:40
dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. http://docs.python.org/dev/3.0/whatsnew//3.0.html First of all how is a view different from an iterator? Secondly, what is the benefit of this change? Is it just for performance reasons? It doesn't seem intuitive to me, i.e., I'm asking for a list of thing (give me all your keys) and I'm getting something else back. Will this confuse people? You are effectively getting a list. It's just not a copy of the internal list, but something that acts as if it where a list but only represents the internal state.

Java Language Specification - Cannot understand 'BlockStatement'

时间秒杀一切 提交于 2019-12-04 00:30:16
I've been examining the Java Language Specification here (instead I should be out having a beer) and I am curious about what a method can contain. The specification states a method body can contain a block MethodBody: Block Where a 'Block' contains 'BlockStatements'. The 'BlockStatement' rule looks like this: BlockStatement : LocalVariableDeclarationStatement ClassOrInterfaceDeclaration [Identifier :] Statement I can understand the 'LocalVariableDeclarationStatement' which could be [final] int x, y, z; However, I don't get why the 'ClassOrInterfaceDeclaration' rule is there. This rule looks

Why was constness removed from Java and C#?

放肆的年华 提交于 2019-12-04 00:30:07
I know this has been discussed many times, but I am not sure I really understand why Java and C# designers chose to omit this feature from these languages. I am not interested in how I can make workarounds (using interfaces, cloning, or any other alternative), but rather in the rationale behind the decision. From a language design perspective, why has this feature been declined? P.S: I'm using words such as "omitted", which some people may find inadequate, as C# was designed in an additive (rather than subtractive) approach. However, I am using such words because the feature existed in C++

Any differences between asInstanceOf[X] and toX for value types?

百般思念 提交于 2019-12-04 00:19:31
I used IntelliJ's ability to convert Java code to Scala code which generally works quite well. It seems that IntelliJ replaced all casts with calls to asInstanceOf . Is there any valid usage of asInstanceOf[Int] , asInstanceOf[Long] etc. for value types which can't be replaced by toInt , toLong , ...? I do not know of any such cases. You can check yourself that the emitted bytecode is the same by compiling a class like class Conv { def b(i: Int) = i.toByte def B(i: Int) = i.asInstanceOf[Byte] def s(i: Int) = i.toShort def S(i: Int) = i.asInstanceOf[Short] def f(i: Int) = i.toFloat def F(i: Int