Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions csharp/language-reference/operators/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace operators
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("============== - operator examples =============");
SubtractionOperator.Examples();
Console.WriteLine();
}
}
}
58 changes: 58 additions & 0 deletions csharp/language-reference/operators/SubtractionOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;

namespace operators
{
public static class SubtractionOperator
{
public static void Examples()
{
DelegateRemoval();
SubtractAndAssign();
}

private static void DelegateRemoval()
{
// <SnippetDelegateRemoval>
Action a = () => Console.Write("a");
Action b = () => Console.Write("b");

var abbaab = a + b + b + a + a + b;
abbaab(); // output: abbaab
Console.WriteLine();

var ab = a + b;
var abba = abbaab - ab;
abba(); // output: abba
Console.WriteLine();

var aba = a + b + a;
var first = abbaab - aba;
first(); // output: abbaab
Console.WriteLine();

var nihil = abbaab - abbaab;
Console.WriteLine(nihil is null); // output: True
// </SnippetDelegateRemoval>
}

private static void SubtractAndAssign()
{
// <SnippetSubtractAndAssign>
int i = 5;
i -= 9;
Console.WriteLine(i);
// Output: -4

Action a = () => Console.Write("a");
Action b = () => Console.Write("b");
var printer = a + b + a;
printer(); // output: aba

Console.WriteLine();
printer -= a;
printer(); // output: ab
// </SnippetSubtractAndAssign>
Console.WriteLine();
}
}
}
14 changes: 14 additions & 0 deletions csharp/language-reference/operators/operators.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<LangVersion>7.3</LangVersion>
<StartupObject>operators.Program</StartupObject>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using namespace System::IO;
// Display any validation errors.
static void ValidationCallBack( Object^ /*sender*/, ValidationEventArgs^ e )
{
Console::WriteLine( L"Validation Error: {0}", e->Message );
Console::WriteLine( L"Validation Error:\n {0}", e->Message );
Console::WriteLine();
}

int main()
Expand All @@ -25,7 +26,7 @@ int main()
XmlReaderSettings^ settings = gcnew XmlReaderSettings;
settings->ValidationType = ValidationType::Schema;
settings->Schemas = sc;
settings->ValidationEventHandler += gcnew ValidationEventHandler( ValidationCallBack );
settings->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallBack);

// Create the XmlReader object.
XmlReader^ reader = XmlReader::Create( L"booksSchemaFail.xml", settings );
Expand All @@ -36,4 +37,14 @@ int main()

return 1;
}
// The example displays output like the following:
// Validation Error:
// The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author'
// in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in
// namespace 'urn:bookstore-schema'.
//
// Validation Error:
// The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name'
// in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in
// namespace 'urn:bookstore-schema'.
//</snippet1>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//<snippet1>
// <Snippet1>
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

public class Sample {

public class Sample
{
public static void Main() {

// Create the XmlSchemaSet class.
Expand All @@ -18,19 +18,28 @@ public static void Main() {
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
settings.ValidationEventHandler += ValidationCallBack;

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);

// Parse the file.
while (reader.Read());

}

// Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e) {
Console.WriteLine("Validation Error: {0}", e.Message);
Console.WriteLine($"Validation Error:\n {e.Message}\n");
}
}
//</snippet1>
// The example displays output like the following:
// Validation Error:
// The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author'
// in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in
// namespace 'urn:bookstore-schema'.
//
// Validation Error:
// The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name'
// in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in
// namespace 'urn:bookstore-schema'.
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'<snippet1>
Imports System
' <Snippet1>
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

public class Sample

public shared sub Main()
Public Module Sample
Public Sub Main()

' Create the XmlSchemaSet class.
Dim sc as XmlSchemaSet = new XmlSchemaSet()
Expand All @@ -24,14 +22,25 @@ public class Sample
Dim reader as XmlReader = XmlReader.Create("booksSchemaFail.xml", settings)

' Parse the file.
while reader.Read()
end while
While reader.Read()
End While

end sub
End Sub

' Display any validation errors.
private shared sub ValidationCallBack(sender as object, e as ValidationEventArgs)
Console.WriteLine("Validation Error: {0}", e.Message)
end sub
end class
'</snippet1>
Private Sub ValidationCallBack(sender as object, e as ValidationEventArgs)
Console.WriteLine($"Validation Error:{vbCrLf} {e.Message}")
Console.WriteLine()
End Sub
End Module
' The example displays output like the following:
' Validation Error:
' The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author'
' in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in
' namespace 'urn:bookstore-schema'.
'
' Validation Error:
' The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name'
' in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in
' namespace 'urn:bookstore-schema'.
' </Snippet1>