Skip to content

Commit 62cab30

Browse files
authoredNov 28, 2018
bug fixes and improvements (dotnet#484)
1 parent 85ce9d6 commit 62cab30

File tree

7 files changed

+82
-290
lines changed

7 files changed

+82
-290
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,8 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
33
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProjectGuid>{A240B8F5-35D8-4E14-8993-0D3515B8B0E5}</ProjectGuid>
74
<OutputType>Exe</OutputType>
8-
<NoStandardLibraries>false</NoStandardLibraries>
9-
<AssemblyName>Enumerable</AssemblyName>
10-
<RootNamespace>Enumerable</RootNamespace>
11-
<FileUpgradeFlags>
12-
</FileUpgradeFlags>
13-
<UpgradeBackupLocation>
14-
</UpgradeBackupLocation>
15-
<NoWin32Manifest>true</NoWin32Manifest>
16-
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
17-
<OldToolsVersion>3.5</OldToolsVersion>
18-
<TargetFrameworkProfile />
19-
<PublishUrl>publish\</PublishUrl>
20-
<Install>true</Install>
21-
<InstallFrom>Disk</InstallFrom>
22-
<UpdateEnabled>false</UpdateEnabled>
23-
<UpdateMode>Foreground</UpdateMode>
24-
<UpdateInterval>7</UpdateInterval>
25-
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
26-
<UpdatePeriodically>false</UpdatePeriodically>
27-
<UpdateRequired>false</UpdateRequired>
28-
<MapFileExtensions>true</MapFileExtensions>
29-
<ApplicationRevision>0</ApplicationRevision>
30-
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
31-
<IsWebBootstrapper>false</IsWebBootstrapper>
32-
<UseApplicationTrust>false</UseApplicationTrust>
33-
<BootstrapperEnabled>true</BootstrapperEnabled>
5+
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>
346
</PropertyGroup>
35-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
36-
<DebugSymbols>true</DebugSymbols>
37-
<DebugType>full</DebugType>
38-
<Optimize>false</Optimize>
39-
<OutputPath>.\bin\Debug\</OutputPath>
40-
<DefineConstants>DEBUG;TRACE</DefineConstants>
41-
<Prefer32Bit>false</Prefer32Bit>
42-
</PropertyGroup>
43-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
44-
<DebugType>pdbonly</DebugType>
45-
<Optimize>true</Optimize>
46-
<OutputPath>.\bin\Release\</OutputPath>
47-
<DefineConstants>TRACE</DefineConstants>
48-
<Prefer32Bit>false</Prefer32Bit>
49-
</PropertyGroup>
50-
<ItemGroup>
51-
<Reference Include="System" />
52-
<Reference Include="System.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
53-
<SpecificVersion>False</SpecificVersion>
54-
<HintPath>C:\Windows\Microsoft.NET\Framework\v3.5.20123\System.Core.dll</HintPath>
55-
</Reference>
56-
<Reference Include="System.Data" />
57-
<Reference Include="System.Xml" />
58-
</ItemGroup>
59-
<ItemGroup>
60-
<Compile Include="enumerable.cs" />
61-
</ItemGroup>
62-
<ItemGroup>
63-
<None Include="app.config" />
64-
</ItemGroup>
65-
<ItemGroup>
66-
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
67-
<Visible>False</Visible>
68-
<ProductName>.NET Framework 3.5 SP1</ProductName>
69-
<Install>true</Install>
70-
</BootstrapperPackage>
71-
</ItemGroup>
72-
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets" />
73-
<ProjectExtensions>
74-
<VisualStudio AllowExistingFolder="true" />
75-
</ProjectExtensions>
7+
768
</Project>

‎snippets/csharp/VS_Snippets_CLR_System/system.Linq.Enumerable/CS/enumerable.cs

+12-102
Large diffs are not rendered by default.

‎snippets/csharp/VS_Snippets_VBCSharp/CsLINQEncapsulatedComparer/CS/EncapsulatedComparer.cs

+10-26
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void Main(string[] args)
7171
// that have duplicates in the second array.
7272

7373
IEnumerable<ProductA> duplicates =
74-
store1.Intersect(store2, new ProductComparer());
74+
store1.Intersect(store2);
7575

7676
foreach (var product in duplicates)
7777
Console.WriteLine(product.Name + " " + product.Code);
@@ -89,7 +89,7 @@ apple 9
8989
//excluding duplicates.
9090

9191
IEnumerable<ProductA> union =
92-
store1.Union(store2, new ProductComparer());
92+
store1.Union(store2);
9393

9494
foreach (var product in union)
9595
Console.WriteLine(product.Name + " " + product.Code);
@@ -151,7 +151,6 @@ lemon 12
151151
orange 4
152152
lemon 12
153153
*/
154-
155154
//</Snippet7>
156155

157156
//SEQUENCEEQUAL
@@ -173,42 +172,27 @@ lemon 12
173172
174173
Equal? True
175174
*/
176-
177175
//</Snippet8>
178176
Console.ReadLine();
179177

180178
}
181179

182180
//<Snippet9>
183-
public class ProductA
184-
{
181+
public class ProductA: IEquatable<ProductA>
182+
{
185183
public string Name { get; set; }
186184
public int Code { get; set; }
187-
}
188185

189-
public class ProductComparer : IEqualityComparer<ProductA>
190-
{
191-
192-
public bool Equals(ProductA x, ProductA y)
186+
public bool Equals(ProductA other)
193187
{
194-
//Check whether the objects are the same object.
195-
if (Object.ReferenceEquals(x, y)) return true;
188+
if (other is null)
189+
return false;
196190

197-
//Check whether the products' properties are equal.
198-
return x != null && y != null && x.Code.Equals(y.Code) && x.Name.Equals(y.Name);
191+
return this.Name == other.Name && this.Code == other.Code;
199192
}
200193

201-
public int GetHashCode(ProductA obj)
202-
{
203-
//Get hash code for the Name field if it is not null.
204-
int hashProductName = obj.Name == null ? 0 : obj.Name.GetHashCode();
205-
206-
//Get hash code for the Code field.
207-
int hashProductCode = obj.Code.GetHashCode();
208-
209-
//Calculate the hash code for the product.
210-
return hashProductName ^ hashProductCode;
211-
}
194+
public override bool Equals(object obj) => Equals(obj as ProductA);
195+
public override int GetHashCode() => (Name, Code).GetHashCode();
212196
}
213197
//</Snippet9>
214198

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
</Project>

‎snippets/visualbasic/VS_Snippets_CLR_System/system.Linq.Enumerable/VB/Enumerable.vb

+1-86
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<RootNamespace>VB</RootNamespace>
6+
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>
7+
</PropertyGroup>
8+
9+
</Project>

‎snippets/visualbasic/VS_Snippets_VBCSharp/CsLINQEncapsulatedComparer/VB/EncapsulatedComparer.vb

+38-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,25 @@ Public Class Product
3535
Return hashProductName Xor hashProductCode
3636
End Function
3737
End Class
38-
3938
'</Snippet1>
4039

4140
Module Module1
4241

4342
Sub Main()
43+
44+
' This snippet is different than #2 by using ProductA (not Product).
45+
' Some samples here need to use ProductA in conjunction with
46+
' ProductComparer, which implements IEqualityComparer (not IEquatable).
47+
'<Snippet10>
48+
Dim store1() As ProductA =
49+
{New Product With {.Name = "apple", .Code = 9},
50+
New Product With {.Name = "orange", .Code = 4}}
51+
52+
Dim store2() As ProductA =
53+
{New Product With {.Name = "apple", .Code = 9},
54+
New Product With {.Name = "lemon", .Code = 12}}
55+
'</Snippet10>
56+
4457
' <Snippet2>
4558
Dim store1() As Product =
4659
{New Product With {.Name = "apple", .Code = 9},
@@ -137,15 +150,14 @@ Module Module1
137150

138151
' This code produces the following output:
139152
'
153+
' apple 9
140154
' orange 4
141155
' lemon 12
142-
143156
' </Snippet7>
144157

145158
' SEQUENCEEQUAL
146159

147160
' <Snippet8>
148-
149161
Dim storeA() As Product =
150162
{New Product With {.Name = "apple", .Code = 9},
151163
New Product With {.Name = "orange", .Code = 4}}
@@ -165,6 +177,28 @@ Module Module1
165177

166178
Console.ReadLine()
167179

168-
169180
End Sub
170181
End Module
182+
183+
' <Snippet9>
184+
Public Class ProductA
185+
Inherits IEquatable(Of ProductA)
186+
187+
Public Property Name As String
188+
Public Property Code As Integer
189+
190+
Public Function Equals(ByVal other As ProductA) As Boolean
191+
If other Is Nothing Then Return False
192+
Return Me.Name = other.Name AndAlso Me.Code = other.Code
193+
End Function
194+
195+
Public Overrides Function Equals(ByVal obj As Object) As Boolean
196+
Return Equals(TryCast(obj, ProductA))
197+
End Function
198+
199+
Public Overrides Function GetHashCode() As Integer
200+
Return (Name, Code).GetHashCode()
201+
End Function
202+
203+
End Class
204+
' </Snippet9>

0 commit comments

Comments
 (0)
Please sign in to comment.