Skip to content

Commit 1070aaa

Browse files
author
Ron Petrusha
authored
Merge pull request #744 from dotnet/master
Update Live with current Master
2 parents 0e83543 + 633843c commit 1070aaa

File tree

4 files changed

+281
-42
lines changed

4 files changed

+281
-42
lines changed

snippets/csharp/VS_Snippets_CLR_System/system.Reflection.Assembly/CS/getcallingassembly1.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,40 @@
33
using System;
44
using System.Reflection;
55
using System.Runtime.CompilerServices;
6-
namespace FirstAssembly {
7-
public class InFirstAssembly {
8-
public static void Main () {
9-
FistMethod ();
10-
SecondAssembly.InSecondAssembly.OtherMethod ();
11-
}
6+
namespace FirstAssembly
7+
{
8+
public class InFirstAssembly
9+
{
10+
public static void Main()
11+
{
12+
FistMethod();
13+
SecondAssembly.InSecondAssembly.OtherMethod();
14+
}
1215

13-
[MethodImpl(MethodImplOptions.NoInlining)]
14-
public static void FirstMethod () {
15-
Console.WriteLine ("FirstMethod called from: " + Assembly.GetCallingAssembly ().FullName);
16-
}
17-
}
16+
[MethodImpl(MethodImplOptions.NoInlining)]
17+
public static void FirstMethod()
18+
{
19+
Console.WriteLine("FirstMethod called from: " + Assembly.GetCallingAssembly().FullName);
20+
}
21+
}
1822
}
1923

2024
// Assembly SecondAssembly
2125
using System;
2226
using System.Reflection;
2327
using System.Runtime.CompilerServices;
2428

25-
namespace SecondAssembly {
26-
class InSecondAssembly () {
27-
[MethodImpl (MethodImplOptions.NoInlining)]
28-
public static void OtherMethod () {
29-
Console.WriteLine ("OtherMehod executing assembly: " + Assembly.GetExecutingAssembly ().FullName);
30-
Console.WriteLine ("OtherMethod called from: " + Assembly.GetCallingAssembly ().FullName);
31-
}
29+
namespace SecondAssembly
30+
{
31+
class InSecondAssembly
32+
{
33+
[MethodImpl(MethodImplOptions.NoInlining)]
34+
public static void OtherMethod()
35+
{
36+
Console.WriteLine("OtherMehod executing assembly: " + Assembly.GetExecutingAssembly().FullName);
37+
Console.WriteLine("OtherMethod called from: " + Assembly.GetCallingAssembly().FullName);
38+
}
39+
}
3240
}
3341
// The example produces output like the following:
3442
// "FirstMethod called from: FirstAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
using System;
2+
3+
namespace operators
4+
{
5+
public static class ArithmeticOperators
6+
{
7+
public static void Examples()
8+
{
9+
Console.WriteLine("==== ++ and -- operators");
10+
Increment();
11+
Decrement();
12+
13+
Console.WriteLine("==== Unary + and - operators");
14+
UnaryPlusAndMinus();
15+
16+
Console.WriteLine("==== *, /, %, +, and - operators");
17+
Multiplication();
18+
IntegerDivision();
19+
IntegerAsFloatingPointDivision();
20+
FloatingPointDivision();
21+
IntegerRemainder();
22+
FloatingPointRemainder();
23+
Addition();
24+
Subtraction();
25+
26+
Console.WriteLine("==== Precedence and associativity examples");
27+
PrecedenceAndAssociativity();
28+
29+
Console.WriteLine("==== Compound assignment");
30+
CompoundAssignment();
31+
32+
Console.WriteLine("==== Special cases");
33+
CheckedUnchecked();
34+
FloatingPointOverflow();
35+
RoundOffErrors();
36+
}
37+
38+
private static void Increment()
39+
{
40+
// <SnippetPrefixIncrement>
41+
double a = 1.5;
42+
Console.WriteLine(a); // output: 1.5
43+
Console.WriteLine(++a); // output: 2.5
44+
Console.WriteLine(a); // output: 2.5
45+
// </SnippetPrefixIncrement>
46+
47+
// <SnippetPostfixIncrement>
48+
int i = 3;
49+
Console.WriteLine(i); // output: 3
50+
Console.WriteLine(i++); // output: 3
51+
Console.WriteLine(i); // output: 4
52+
// </SnippetPostfixIncrement>
53+
}
54+
55+
private static void Decrement()
56+
{
57+
// <SnippetPrefixDecrement>
58+
double a = 1.5;
59+
Console.WriteLine(a); // output: 1.5
60+
Console.WriteLine(--a); // output: 0.5
61+
Console.WriteLine(a); // output: 0.5
62+
// </SnippetPrefixDecrement>
63+
64+
// <SnippetPostfixDecrement>
65+
int i = 3;
66+
Console.WriteLine(i); // output: 3
67+
Console.WriteLine(i--); // output: 3
68+
Console.WriteLine(i); // output: 2
69+
// </SnippetPostfixDecrement>
70+
}
71+
72+
private static void UnaryPlusAndMinus()
73+
{
74+
// <SnippetUnaryPlusAndMinus>
75+
Console.WriteLine(+4); // output: 4
76+
77+
Console.WriteLine(-4); // output: -4
78+
Console.WriteLine(-(-4)); // output: 4
79+
80+
uint a = 5;
81+
var b = -a;
82+
Console.WriteLine(b); // output: -5
83+
Console.WriteLine(b.GetType()); // output: System.Int64
84+
85+
Console.WriteLine(-double.NaN); // output: NaN
86+
// </SnippetUnaryPlusAndMinus>
87+
}
88+
89+
private static void Multiplication()
90+
{
91+
// <SnippetMultiplication>
92+
Console.WriteLine(5 * 2); // output: 10
93+
Console.WriteLine(0.5 * 2.5); // output: 1.25
94+
Console.WriteLine(0.1m * 23.4m); // output: 2.34
95+
// </SnippetMultiplication>
96+
}
97+
98+
private static void IntegerDivision()
99+
{
100+
// <SnippetIntegerDivision>
101+
Console.WriteLine(13 / 5); // output: 2
102+
Console.WriteLine(-13 / 5); // output: -2
103+
Console.WriteLine(13 / -5); // output: -2
104+
Console.WriteLine(-13 / -5); // output: 2
105+
// </SnippetIntegerDivision>
106+
}
107+
108+
private static void IntegerAsFloatingPointDivision()
109+
{
110+
// <SnippetIntegerAsFloatingPointDivision>
111+
Console.WriteLine(13 / 5.0); // output: 2.6
112+
113+
int a = 13;
114+
int b = 5;
115+
Console.WriteLine((double)a / b); // output: 2.6
116+
// </SnippetIntegerAsFloatingPointDivision>
117+
}
118+
119+
private static void FloatingPointDivision()
120+
{
121+
// <SnippetFloatingPointDivision>
122+
Console.WriteLine(16.8f / 4.1f); // output: 4.097561
123+
Console.WriteLine(16.8d / 4.1d); // output: 4.09756097560976
124+
Console.WriteLine(16.8m / 4.1m); // output: 4.0975609756097560975609756098
125+
// </SnippetFloatingPointDivision>
126+
}
127+
128+
private static void IntegerRemainder()
129+
{
130+
// <SnippetIntegerRemainder>
131+
Console.WriteLine(5 % 4); // output: 1
132+
Console.WriteLine(5 % -4); // output: 1
133+
Console.WriteLine(-5 % 4); // output: -1
134+
Console.WriteLine(-5 % -4); // output: -1
135+
// </SnippetIntegerRemainder>
136+
}
137+
138+
private static void FloatingPointRemainder()
139+
{
140+
// <SnippetFloatingPointRemainder>
141+
Console.WriteLine(-5.2f % 2.0f); // output: -1.2
142+
Console.WriteLine(5.9 % 3.1); // output: 2.8
143+
Console.WriteLine(5.9m % 3.1m); // output: 2.8
144+
// </SnippetFloatingPointRemainder>
145+
}
146+
147+
private static void Addition()
148+
{
149+
// <SnippetAddition>
150+
Console.WriteLine(5 + 4); // output: 9
151+
Console.WriteLine(5 + 4.3); // output: 9.3
152+
Console.WriteLine(5.1m + 4.2m); // output: 9.3
153+
// </SnippetAddition>
154+
}
155+
156+
private static void Subtraction()
157+
{
158+
// <SnippetSubtraction>
159+
Console.WriteLine(47 - 3); // output: 44
160+
Console.WriteLine(5 - 4.3); // output: 0.7
161+
Console.WriteLine(7.5m - 2.3m); // output: 5.2
162+
// </SnippetSubtraction>
163+
}
164+
165+
private static void PrecedenceAndAssociativity()
166+
{
167+
// <SnippetPrecedenceAndAssociativity>
168+
Console.WriteLine(2 + 2 * 2); // output: 6
169+
Console.WriteLine((2 + 2) * 2); // output: 8
170+
171+
Console.WriteLine(9 / 5 / 2); // output: 0
172+
Console.WriteLine(9 / (5 / 2)); // output: 4
173+
// </SnippetPrecedenceAndAssociativity>
174+
}
175+
176+
private static void CompoundAssignment()
177+
{
178+
// <SnippetCompoundAssignment>
179+
int a = 5;
180+
a += 9;
181+
Console.WriteLine(a); // output: 14
182+
183+
a -= 4;
184+
Console.WriteLine(a); // output: 10
185+
186+
a *= 2;
187+
Console.WriteLine(a); // output: 20
188+
189+
a /= 4;
190+
Console.WriteLine(a); // output: 5
191+
192+
a %= 3;
193+
Console.WriteLine(a); // output: 2
194+
// </SnippetCompoundAssignment>
195+
}
196+
197+
private static void CheckedUnchecked()
198+
{
199+
// <SnippetCheckedUnchecked>
200+
int a = int.MaxValue;
201+
int b = 3;
202+
203+
Console.WriteLine(unchecked(a + b)); // output: -2147483646
204+
try
205+
{
206+
int d = checked(a + b);
207+
}
208+
catch(OverflowException)
209+
{
210+
Console.WriteLine($"Overflow occured when adding {a} to {b}.");
211+
}
212+
// </SnippetCheckedUnchecked>
213+
}
214+
215+
private static void FloatingPointOverflow()
216+
{
217+
// <SnippetFloatingPointOverflow>
218+
double a = 1.0 / 0.0;
219+
Console.WriteLine(a); // output: Infinity
220+
Console.WriteLine(double.IsInfinity(a)); // output: True
221+
222+
Console.WriteLine(double.MaxValue + double.MaxValue); // output: Infinity
223+
224+
double b = 0.0 / 0.0;
225+
Console.WriteLine(b); // output: NaN
226+
Console.WriteLine(double.IsNaN(b)); // output: True
227+
// </SnippetFloatingPointOverflow>
228+
}
229+
230+
private static void RoundOffErrors()
231+
{
232+
// <SnippetRoundOffErrors>
233+
Console.WriteLine(.41f % .2f); // output: 0.00999999
234+
235+
double a = 0.1;
236+
double b = 3 * a;
237+
Console.WriteLine(b == 0.3); // output: False
238+
Console.WriteLine(b - 0.3); // output: 5.55111512312578E-17
239+
240+
decimal c = 1 / 3.0m;
241+
decimal d = 3 * c;
242+
Console.WriteLine(d == 1.0m); // output: False
243+
Console.WriteLine(d); // output: 0.9999999999999999999999999999
244+
// </SnippetRoundOffErrors>
245+
}
246+
}
247+
}

snippets/csharp/language-reference/operators/Program.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class Program
66
{
77
static void Main(string[] args)
88
{
9-
Console.WriteLine("============== % operator examples =============");
10-
RemainderExamples.Examples();
9+
Console.WriteLine("======== Arithmetic operators examples =========");
10+
ArithmeticOperators.Examples();
1111
Console.WriteLine();
1212

1313
Console.WriteLine("============== + operator examples =============");
@@ -34,14 +34,6 @@ static void Main(string[] args)
3434
ConditionalLogicalOperatorsExamples.Examples();
3535
Console.WriteLine();
3636

37-
Console.WriteLine("============== -- and ++ operator examples =====");
38-
DecrementAndIncrementExamples.Examples();
39-
Console.WriteLine();
40-
41-
Console.WriteLine("============== / operator examples =============");
42-
DivisionExamples.Examples();
43-
Console.WriteLine();
44-
4537
Console.WriteLine("============== == and != operator examples =====");
4638
EqualityAndNonEqualityExamples.Examples();
4739
Console.WriteLine();
@@ -73,10 +65,6 @@ static void Main(string[] args)
7365
Console.WriteLine("============== . operator examples =============");
7466
MemberAccessExamples.Examples();
7567
Console.WriteLine();
76-
77-
Console.WriteLine("============== * operator examples =============");
78-
MultiplicationExamples.Examples();
79-
Console.WriteLine();
8068
}
8169
}
8270
}

snippets/fsharp/lang-ref-2/snippet5005.fs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ let examineNumber x =
1919
| _ -> ()
2020

2121
let findSquareCubes x =
22-
if (match x with
23-
| Cube x -> true
24-
| _ -> false
25-
&&
26-
match x with
27-
| Square x -> true
28-
| _ -> false
29-
)
30-
then printf "%d \n" x
22+
match x with
23+
| Cube x & Square _ -> printfn "%d is a cube and a square" x
24+
| Cube x -> printfn "%d is a cube" x
25+
| _ -> ()
26+
3127

32-
[ 1 .. 1000000 ] |> List.iter (fun elem -> findSquareCubes elem)
28+
[ 1 .. 1000 ] |> List.iter (fun elem -> findSquareCubes elem)

0 commit comments

Comments
 (0)