language-features

How to loop through all enum values in C#? [duplicate]

安稳与你 提交于 2019-11-26 03:16:50
问题 This question already has answers here : Closed 7 years ago . This question already has an answer here: How do I enumerate an enum in C#? 26 answers public enum Foos { A, B, C } Is there a way to loop through the possible values of Foos ? Basically? foreach(Foo in Foos) 回答1: Yes you can use the ‍GetValue‍‍‍s method: var values = Enum.GetValues(typeof(Foos)); Or the typed version: var values = Enum.GetValues(typeof(Foos)).Cast<Foos>(); I long ago added a helper function to my private library

The written versions of the logical operators

流过昼夜 提交于 2019-11-26 02:35:38
问题 This is the only place I\'ve ever seen and , or and not listed as actual operators in C++. When I wrote up a test program in NetBeans, I got the red underlining as if there was a syntax error and figured the website was wrong, but it is NetBeans which is wrong because it compiled and ran as expected. I can see ! being favored over not but the readability of and && or seems greater than their grammatical brothers. Why do these versions of the logical operators exist and why does seemingly no

Difference between parseInt and valueOf in java?

戏子无情 提交于 2019-11-26 01:55:34
问题 What\'s the difference between these two methods? They appear to do exactly the same thing to me (also goes for parseFloat() , parseDouble() , parseLong() etc, how are they different from Long.valueOf(string) ? Edit: Also, which of these is preferable and used more often by convention? 回答1: Well, the API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object

Is there more to an interface than having the correct methods

爱⌒轻易说出口 提交于 2019-11-26 01:28:02
问题 So lets say I have this interface: public interface IBox { public void setSize(int size); public int getSize(); public int getArea(); //...and so on } And I have a class that implements it: public class Rectangle implements IBox { private int size; //Methods here } If I wanted to use the interface IBox, i can\'t actually create an instance of it, in the way: public static void main(String args[]) { Ibox myBox=new Ibox(); } right? So I\'d actually have to do this: public static void main

How does “this” keyword work within a function?

元气小坏坏 提交于 2019-11-26 01:18:15
问题 I just came across an interesting situation in JavaScript. I have a class with a method that defines several objects using object-literal notation. Inside those objects, the this pointer is being used. From the behavior of the program, I have deduced that the this pointer is referring to the class on which the method was invoked, and not the object being created by the literal. This seems arbitrary, though it is the way I would expect it to work. Is this defined behavior? Is it cross-browser

What is the python “with” statement designed for?

给你一囗甜甜゛ 提交于 2019-11-25 23:57:17
问题 I came across the Python with statement for the first time today. I\'ve been using Python lightly for several months and didn\'t even know of its existence! Given its somewhat obscure status, I thought it would be worth asking: What is the Python with statement designed to be used for? What do you use it for? Are there any gotchas I need to be aware of, or common anti-patterns associated with its use? Any cases where it is better use try..finally than with ? Why isn\'t it used more widely?

What does the &#39;static&#39; keyword do in a class?

﹥>﹥吖頭↗ 提交于 2019-11-25 23:55:47
问题 To be specific, I was trying this code: package hello; public class Hello { Clock clock = new Clock(); public static void main(String args[]) { clock.sayTime(); } } But it gave the error Cannot access non-static field in static method main So I changed the declaration of clock to this: static Clock clock = new Clock(); And it worked. What does it mean to put that keyword before the declaration? What exactly will it do and/or restrict in terms of what can be done to that object? 回答1: static

Why Doesn&#39;t C# Allow Static Methods to Implement an Interface?

Deadly 提交于 2019-11-25 23:36:14
问题 Why was C# designed this way? As I understand it, an interface only describes behaviour, and serves the purpose of describing a contractual obligation for classes implementing the interface that certain behaviour is implemented. If classes wish to implement that behavour in a shared method, why shouldn\'t they? Here is an example of what I have in mind: // These items will be displayed in a list on the screen. public interface IListItem { string ScreenName(); ... } public class Animal:

Expression Versus Statement

淺唱寂寞╮ 提交于 2019-11-25 23:35:40
问题 I\'m asking with regards to c#, but I assume its the same in most other languages. Does anyone have a good definition of expressions and statements and what the differences are? 回答1: Expression: Something which evaluates to a value. Example: 1+2/x Statement: A line of code which does something. Example: GOTO 100 In the earliest general-purpose programming languages, like FORTRAN, the distinction was crystal-clear. In FORTRAN, a statement was one unit of execution, a thing that you did. The

Are there legitimate uses for JavaScript&#39;s “with” statement?

回眸只為那壹抹淺笑 提交于 2019-11-25 23:03:25
问题 Alan Storm\'s comments in response to my answer regarding the with statement got me thinking. I\'ve seldom found a reason to use this particular language feature, and had never given much thought to how it might cause trouble. Now, I\'m curious as to how I might make effective use of with , while avoiding its pitfalls. Where have you found the with statement useful? 回答1: Another use occurred to me today, so I searched the web excitedly and found an existing mention of it: Defining Variables