Skip to content

Commit d4e291c

Browse files
Update Enum Topic
1 parent 9b7fc66 commit d4e291c

7 files changed

Lines changed: 109 additions & 7 deletions

File tree

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
```Cs
2-
// TODO
2+
enum Color
3+
{
4+
Red,
5+
Green,
6+
Blue
7+
}
8+
9+
enum FileAccess
10+
{
11+
Read = 1,
12+
Write = 2,
13+
Execute = 4
14+
}
15+
16+
Color color = Color.Red;
17+
int value = (int)Color.Red;
18+
Color parsed = Enum.Parse<Color>("Red");
319
```
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
```Go
2-
// TODO
2+
// Go has no enum keyword.
3+
// Use constants with iota.
4+
5+
type Color int
6+
7+
const (
8+
Red Color = iota
9+
Green
10+
Blue
11+
)
12+
13+
color := Red
314
```
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
```Java
2-
// TODO
2+
enum Color {
3+
RED,
4+
GREEN,
5+
BLUE
6+
}
7+
8+
enum HttpStatus {
9+
OK(200),
10+
NOT_FOUND(404);
11+
12+
private final int code;
13+
14+
HttpStatus(int code) {
15+
this.code = code;
16+
}
17+
18+
public int getCode() {
19+
return code;
20+
}
21+
}
22+
23+
Color color = Color.RED;
24+
String name = color.name();
325
```
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
```Javascript
2-
// TODO
2+
// No Native Enum Support.
3+
// Use frozen objects or TypeScript enums when static typing is needed.
4+
5+
const Color = Object.freeze({
6+
Red: "red",
7+
Green: "green",
8+
Blue: "blue",
9+
});
10+
11+
const color = Color.Red;
312
```
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
```Python
2-
# TODO
2+
from enum import Enum
3+
4+
class Color(Enum):
5+
RED = 1
6+
GREEN = 2
7+
BLUE = 3
8+
9+
color = Color.RED
10+
name = color.name
11+
value = color.value
312
```
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
```Rust
2-
// TODO
2+
enum Color {
3+
Red,
4+
Green,
5+
Blue,
6+
}
7+
8+
enum Message {
9+
Quit,
10+
Move { x: i32, y: i32 },
11+
Write(String),
12+
}
13+
14+
let color = Color::Red;
15+
16+
match color {
17+
Color::Red => println!("red"),
18+
Color::Green => println!("green"),
19+
Color::Blue => println!("blue"),
20+
}
321
```
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
```Typescript
2-
// TODO
2+
enum Color {
3+
Red,
4+
Green,
5+
Blue,
6+
}
7+
8+
enum HttpStatus {
9+
Ok = 200,
10+
NotFound = 404,
11+
}
12+
13+
const color: Color = Color.Red;
14+
15+
// String enum
16+
enum Direction {
17+
Up = "UP",
18+
Down = "DOWN",
19+
}
320
```

0 commit comments

Comments
 (0)