Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions snippets/csharp/tour/classes-and-objects/ListBasedExamples.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace ListExamples
{
using System;
public class List<T>
public class MyList<T>
{
// Constant
const int defaultCapacity = 4;
Expand All @@ -11,7 +11,7 @@ public class List<T>
int count;

// Constructor
public List(int capacity = defaultCapacity)
public MyList(int capacity = defaultCapacity)
{
items = new T[capacity];
}
Expand Down Expand Up @@ -60,9 +60,9 @@ protected virtual void OnChanged() =>
Changed?.Invoke(this, EventArgs.Empty);

public override bool Equals(object other) =>
Equals(this, other as List<T>);
Equals(this, other as MyList<T>);

static bool Equals(List<T> a, List<T> b)
static bool Equals(MyList<T> a, MyList<T> b)
{
if (Object.ReferenceEquals(a, null)) return Object.ReferenceEquals(b, null);
if (Object.ReferenceEquals(b, null) || a.count != b.count)
Expand All @@ -81,32 +81,32 @@ static bool Equals(List<T> a, List<T> b)
public event EventHandler Changed;

// Operators
public static bool operator ==(List<T> a, List<T> b) =>
public static bool operator ==(MyList<T> a, MyList<T> b) =>
Equals(a, b);

public static bool operator !=(List<T> a, List<T> b) =>
public static bool operator !=(MyList<T> a, MyList<T> b) =>
!Equals(a, b);
}

public class ExampleCode
{
public static void ListExampleOne()
{
List<string> list1 = new List<string>();
List<string> list2 = new List<string>(10);
MyList<string> list1 = new MyList<string>();
MyList<string> list2 = new MyList<string>(10);
}

public static void ListExampleTwo()
{
List<string> names = new List<string>();
MyList<string> names = new MyList<string>();
names.Capacity = 100; // Invokes set accessor
int i = names.Count; // Invokes get accessor
int j = names.Capacity; // Invokes get accessor
}

public static void ListExampleThree()
{
List<string> names = new List<string>();
MyList<string> names = new MyList<string>();
names.Add("Liz");
names.Add("Martha");
names.Add("Beth");
Expand All @@ -118,10 +118,10 @@ public static void ListExampleThree()
}
public static void ListExampleFour()
{
List<int> a = new List<int>();
MyList<int> a = new MyList<int>();
a.Add(1);
a.Add(2);
List<int> b = new List<int>();
MyList<int> b = new MyList<int>();
b.Add(1);
b.Add(2);
Console.WriteLine(a == b); // Outputs "True"
Expand All @@ -138,7 +138,7 @@ static void ListChanged(object sender, EventArgs e)
}
public static void Usage()
{
List<string> names = new List<string>();
MyList<string> names = new MyList<string>();
names.Changed += new EventHandler(ListChanged);
names.Add("Liz");
names.Add("Martha");
Expand Down