Skip to content

Commit 566d23e

Browse files
GorafBillWagner
authored andcommitted
Introduction to classes - fix argument's list, code formatting (dotnet#4050)
* sync method's argument list with sample's code * small code formatting * to follow coding conventions by Microsoft
1 parent 64aafc4 commit 566d23e

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

docs/csharp/quick-starts/introduction-to-classes.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace classes
6464
{
6565
}
6666

67-
public void MakeWithdrawal(decimal amount, DateTime date, string payee, string note)
67+
public void MakeWithdrawal(decimal amount, DateTime date, string note)
6868
{
6969
}
7070
}
@@ -164,9 +164,11 @@ Next, test that you are catching error conditions by trying to create an account
164164

165165
```csharp
166166
// Test that the initial balances must be positive:
167-
try {
167+
try
168+
{
168169
var invalidAccount = new BankAccount("invalid", -55);
169-
} catch (ArgumentOutOfRangeException e)
170+
}
171+
catch (ArgumentOutOfRangeException e)
170172
{
171173
Console.WriteLine("Exception caught creating account with negative balance");
172174
Console.WriteLine(e.ToString());
@@ -177,9 +179,11 @@ You use the [`try` and `catch` statements](../language-reference/keywords/try-ca
177179

178180
```csharp
179181
// Test for a negative balance
180-
try {
182+
try
183+
{
181184
account.MakeWithdrawal(750, DateTime.Now, "Attempt to overdraw");
182-
} catch (InvalidOperationException e)
185+
}
186+
catch (InvalidOperationException e)
183187
{
184188
Console.WriteLine("Exception caught trying to overdraw");
185189
Console.WriteLine(e.ToString());

samples/csharp/classes-quickstart/BankAccount.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ public decimal Balance
1313
get
1414
{
1515
decimal balance = 0;
16-
foreach(var item in allTransactions)
16+
foreach (var item in allTransactions)
17+
{
1718
balance += item.Amount;
19+
}
20+
1821
return balance;
1922
}
2023
}
@@ -26,7 +29,7 @@ public BankAccount(string name, decimal initialBalance)
2629
{
2730
this.Number = accountNumberSeed.ToString();
2831
accountNumberSeed++;
29-
32+
3033
this.Owner = name;
3134
MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
3235
}
@@ -40,17 +43,23 @@ public BankAccount(string name, decimal initialBalance)
4043
public void MakeDeposit(decimal amount, DateTime date, string note)
4144
{
4245
if (amount <= 0)
46+
{
4347
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
48+
}
4449
var deposit = new Transaction(amount, date, note);
4550
allTransactions.Add(deposit);
4651
}
4752

4853
public void MakeWithdrawal(decimal amount, DateTime date, string note)
4954
{
5055
if (amount <= 0)
56+
{
5157
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
58+
}
5259
if (Balance - amount < 0)
60+
{
5361
throw new InvalidOperationException("Not sufficient funds for this withdrawal");
62+
}
5463
var withdrawal = new Transaction(-amount, date, note);
5564
allTransactions.Add(withdrawal);
5665
}
@@ -63,7 +72,9 @@ public string GetAccountHistory()
6372

6473
report.AppendLine("Date\t\tAmount\tNote");
6574
foreach (var item in allTransactions)
75+
{
6676
report.AppendLine($"{item.Date.ToShortDateString()}\t{item.Amount}\t{item.Notes}");
77+
}
6778

6879
return report.ToString();
6980
}

samples/csharp/classes-quickstart/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@ static void Main(string[] args)
1717
Console.WriteLine(account.GetAccountHistory());
1818

1919
// Test that the initial balances must be positive:
20-
try {
20+
try
21+
{
2122
var invalidAccount = new BankAccount("invalid", -55);
22-
} catch (ArgumentOutOfRangeException e)
23+
}
24+
catch (ArgumentOutOfRangeException e)
2325
{
2426
Console.WriteLine("Exception caught creating account with negative balance");
2527
Console.WriteLine(e.ToString());
2628
}
2729

2830
// Test for a negative balance
29-
try {
31+
try
32+
{
3033
account.MakeWithdrawal(750, DateTime.Now, "Attempt to overdraw");
31-
} catch (InvalidOperationException e)
34+
}
35+
catch (InvalidOperationException e)
3236
{
3337
Console.WriteLine("Exception caught trying to overdraw");
3438
Console.WriteLine(e.ToString());
3539
}
36-
3740
}
3841
}
3942
}

samples/csharp/classes-quickstart/transaction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class Transaction
77
public decimal Amount { get; }
88
public DateTime Date { get; }
99
public string Notes { get; }
10+
1011
public Transaction(decimal amount, DateTime date, string note)
1112
{
1213
this.Amount = amount;

0 commit comments

Comments
 (0)