You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/casting/about.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ In C# very often, outside of the realm of numeric values and class hierarchies,
8
8
9
9
Note that implicit an explicit cast [operators][operator-overloading] (discussed in (TODO cross-ref-tba)) are available which can bring fairly arbitrary casting to your own types.
10
10
11
-
####Casting Primitive Types - Implicit
11
+
## Casting Primitive Types - Implicit
12
12
13
13
C#'s type system is somewhat stricter than _C_'s or Javascript's and as a consequence, casting operations are more restricted. [Implicit casting][implicit-casts] takes place between two numeric types as long as the "to" type can preserve the scale and sign of the "from" type's value. Note in the documentation the exception for converting to real numbers where precision may be lost.
14
14
@@ -23,15 +23,15 @@ There is no implicit conversion of a numeric (or string) expression to `bool`. T
23
23
24
24
An expression of type `char` can be implicitly cast to `int`. The cast in the opposite direction must be explicit. Not all values of `int` are valid utf 16 chars.
25
25
26
-
####Casting Primitive Types - Explicit
26
+
## Casting Primitive Types - Explicit
27
27
28
28
Where numeric types cannot be cast implicitly you can generally use the explicit cast [operator][cast-operator].
29
29
30
30
Where the value being cast cannot be represented by the "to" type because it is insufficiently wide or there is a sign conflict then an overflow exception may be thrown in the case of integers, or the "to" type variable may take a value of `Infinity` in the case of floats and doubles.
31
31
32
32
An expression of type `int` can be explicitly cast to `char`. This may result in an invalid `char`.
33
33
34
-
####Casting Primitive Types - Examples
34
+
## Casting Primitive Types - Examples
35
35
36
36
```csharp
37
37
intlargeInt=Int32.MaxValue;
@@ -61,7 +61,7 @@ int fromString_bad = Int32.Parse("forty two"); // FormatException is thrown
61
61
62
62
See this [article][checked] for the _**checked**_ keyword.
63
63
64
-
####Type Conversion for types in a hierarchy
64
+
## Type Conversion for types in a hierarchy
65
65
66
66
Any type can be implicitly converted to its base class or interface.
67
67
@@ -112,13 +112,13 @@ if (r is Foo foo3)
112
112
113
113
The [`as`][as-operator] keyword fulfills a similar function to `is` e.g. `var foo = ifoo as Foo;`. In this example `foo` will be `null` if `ifoo` is not of type `Foo` otherwise `ifoo` will be assigned to it.
114
114
115
-
####Custom Cast Operator
115
+
## Custom Cast Operator
116
116
117
117
Types can define their own custom explicit and implicit [cast operators][custom-casts]. See (TODO cross-ref-tba) for coverage of this..
118
118
119
119
Examples of [explicit][big-integer-explicit] and [implicit][big-integer-implicit] casts in the BCL is conversions from the `BigInteger` struct to and from other numeric types
120
120
121
-
####Using `typeof`
121
+
## Using `typeof`
122
122
123
123
If you need to detect the precise type of an object then `is` may be a little too permissive as it will convert an object to a class or any of its base classes. `typeof` and `Object.GetType()` are the solution in this case.
Copy file name to clipboardExpand all lines: concepts/constants/about.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# About
2
2
3
-
####const
3
+
## const
4
4
5
5
The [`const`][constants] modifier can be (and generally should be) applied to any field where its value is known at compile time and will not change during the lifetime of the program.
6
6
@@ -27,7 +27,7 @@ public double Area(double r)
27
27
28
28
Identifying a value with `const` in this way can be useful if it is used multiple times in the method or you want to draw attention to its meaning. There is no performance gain over using literals inline.
29
29
30
-
####readonly
30
+
## readonly
31
31
32
32
The [`readonly`][readonly-fields] modifier can be (and generally should be) applied to any field that cannot be made `const` where its value will not change during the lifetime of the program and is either set by an inline initializer or during instantiation (by the constructor or a method called by the constructor).
Copy file name to clipboardExpand all lines: concepts/equality/about.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
The coding exercise illustrates a number of properties of equality in C#:
4
4
5
-
###`Object.Equals()`
5
+
## `Object.Equals()`
6
6
7
-
####Topics covered by the coding exercise
7
+
### Topics covered by the coding exercise
8
8
9
9
- Simple types (strings and primitives) are typically tested for equality with the `==` and `!=`. This is considered more idiomatic than using the [`Equals()`][object-equals] method which is also available with these types. Java programmers should be alert, when dealing with strings, to the fact that `==` compares by value in C# but by reference in Java when returning to their former language.
10
10
- Reference types (Instances of classes) are compared using the `Equals()` method inherited from `object`. If your goal with the equality test is to ensure that two objects are the exact same instance then relying on `object`'s implementation will suffice. If not, you need to override `object.Equals()`.
@@ -48,7 +48,7 @@ ReferenceEquals(winA, winC);
48
48
// => true
49
49
```
50
50
51
-
####Ancillary topics
51
+
### Ancillary topics
52
52
53
53
- In addition to `public override bool Equals(object obj)` IDEs typically generate the overload `protected bool Equals(FacialFeatures other)` for use when inheritance is involved. A derived class can call the base classe's `Equals()` and then add its own test.
54
54
- Do not use `==` unless you have [overloaded][operator-overloading] the `==` operator, as well as the `Equals()` method in your class (see the `operator-overloading` exercise) or you care only that the references are equal.
Copy file name to clipboardExpand all lines: concepts/equality/introduction.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ An overridden `Equals()` method will contain equality tests on members of simple
10
10
11
11
The `Object` class provides appropriate methods to compare two objects to detect if they are one and the same instance.
12
12
13
-
###`Object.GetHashCode()`
13
+
## `Object.GetHashCode()`
14
14
15
15
The `Object.GetHashCode()` method returns a hash code in the form of a 32 bit integer. The hash code is used by _dictionary_ and _set_ classes such as `Dictionary<T>` and `HashSet<T>`to store and retrieve objects in a performant manner.
Copy file name to clipboardExpand all lines: concepts/integral-numbers/about.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ The types discussed so far are _primitive_ types. Each is paired with a `struct`
21
21
|`uint`|`UInt32`| 32 bit | 0 | +4_294_967_295 |
22
22
|`ulong`|`UInt64`| 64 bit | 0 | +18_446_744_073_709_551_615 |
23
23
24
-
####Casting
24
+
## Casting
25
25
26
26
A variable (or expression) of one type can easily be converted to another. For instance, in an assignment operation, if the type of the value being assigned (rhs) ensures that the value will fit within the range of the type being assigned to (lhs) then there is a simple assignment:
27
27
@@ -45,13 +45,13 @@ The requirement for casting is determined by the two types involved rather than
45
45
46
46
The following paragraphs discuss the casting of integral types. (TODO cross-ref-tba casting) provides a broader discussion of casting and type conversion. See that documentation for a discussion of conversion between integral types and floating-point numbers, `char` and `bool`.
47
47
48
-
#####Casting Primitive Types - Implicit
48
+
### Casting Primitive Types - Implicit
49
49
50
50
C#'s type system is somewhat stricter than _C_'s or Javascript's and as a consequence, casting operations are more restricted. [Implicit casting][implicit-casts] takes place between two numeric types as long as the "to" type can preserve the scale and sign of the "from" type's value.
51
51
52
52
An implicit cast is not signified by any special syntax.
53
53
54
-
#####Casting Primitive Types - Explicit
54
+
### Casting Primitive Types - Explicit
55
55
56
56
Where numeric types cannot be cast implicitly you can generally use the explicit cast [operator][cast-operator].
Copy file name to clipboardExpand all lines: concepts/interfaces/about.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,7 +117,7 @@ By design, C# does not support multiple inheritance, but it facilitates a kind o
117
117
118
118
Moreover, the concept of [polymorphism can be implemented through interfaces][interface-polymorphism] underpins the interface mechanism.
119
119
120
-
####Explicit interface implementation
120
+
## Explicit interface implementation
121
121
122
122
Sometimes method names and signatures can be shared in two different interfaces.
123
123
In order provide a distinct implementation of these methods, C# provides [explicit implementation of interfaces][explicit-implementation]. Note that to use a particular implementation of an interface you need to convert the expression containing referencing the object to that interface. Assignment, casting or passing as a parameter will achieve this.
@@ -166,7 +166,7 @@ There are a number of use cases:
166
166
- Methods with the same name but different return types: if you implement your own collection classes you may find that an explicit interface for the legacy `IEnumerable.GetEnumerator()`, alongside `IEnumerable<T>.GetEnuerator()`, is required. You may never make use of such the interface but the compiler may insist.
167
167
- Methods where there is no clash of names between interfaces but it is desirable that the implementing class uses the name for some related purpose: `IFormattable` has a `ToString()` method which takes a _format type_ parameter as well as parameter of type `IFormatProvider`. A class like `FormattableString` from the Base Class Library (BCL) has the interface to ensure it can be used by routines that take an `IFormattable` but it is more expressive for its main version of `ToString(IFormatProvider)` to omit the _format type_ parameter as it is not used in the implementation and would confuse API users.
168
168
169
-
####Default implementation
169
+
## Default implementation
170
170
171
171
Version 8 of C# addresses a nagging problem with APIs. If you add methods to an interface to enhance functionality for new implementations then it is necessary to modify all the existing implementations of the interface so that they comply with the API-contract even though they have no implementation specific behavior. C# now allows for a _default method_ to be provided as part of the interface (Java developers will be familiar). Previously, when such a change occurred a _version 2_ of the interface would exist alongside the original.
Copy file name to clipboardExpand all lines: concepts/lists/about.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ A collection definition typically includes a place holder in angle brackets, oft
13
13
14
14
Unlike arrays (TODO cross-ref-tba) lists can resize themselves dynamically.
15
15
16
-
####LINQ
16
+
## LINQ
17
17
18
18
Although the built-in API of `List<T>` is rich (including mappings and filters such as `ConvertAll`, `FindAll` and `Foreach`) and its [looping syntax][for-each] is very clear, and you definitely need to be familiar with this API, Language Integrated Query ([LINQ][linq]) is available for many tasks, is even more powerful and widely used and has the advantage of providing a consistent interface across library collections, third-party collections and your own classes. See (TODO cross-ref-tba).
Copy file name to clipboardExpand all lines: concepts/namespaces/about.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ According to the [official documentation][namespaces] namespaces have two princi
22
22
23
23
Namespaces are used widely by the base class library (BCL) to organize its functionality.
24
24
25
-
####References to namespaced types
25
+
## References to namespaced types
26
26
27
27
Types enclosed in namespaces are referred to outside the namespace by prefixing the type name with the dot syntax. Alternatively, and more usually, you can place a `using` directive at the top of the file (or within a namespace) and any types in the imported namespace can be used without the prefix. Within the same namespace there is no need to qualify type names.
28
28
@@ -68,13 +68,13 @@ This [article][using] clearly explains the ways in which the `using` directive c
68
68
-`using static`: avoid having to qualify members with types (a good example is `Math.Max()`).
69
69
-`using MyAlias = YourNamespace;`: substitute a more readable name for the namespace name.
70
70
71
-
####Clash of namespaces
71
+
## Clash of namespaces
72
72
73
73
.NET addresses the issue of two namespaces with the same name in different assemblies where there would be a clash of fully qualified identifier names (perhaps a scenario where multiple versions of an assembly are loaded). This issue is addressed with the [namespace alias qualifier][namespace-alias-qualifier] and the [extern alias][extern-alias].
74
74
75
75
One reason to raise this fairly niche topic is because of its use of the [`::` operator][dot-dot-operator]. You will often see the qualifier `global::` prefixing namespaces, particularly in generated code. The intention here is to avoid a potential clash with some nested namespace or class name. By prefixing a namespace with `global::` you ensure that a top-level namespace is selected.
76
76
77
-
####Note for Java developers
77
+
## Note for Java developers
78
78
79
79
When comparing with the `import` of Java `packages` some differences and similarities should be noted:
0 commit comments