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
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public ref class MyTreeNode_FirstNode: public Form
private:
void FillMyTreeView()
{

// Add customers to the ArrayList of 'Customer' objects.
for ( int iIndex = 1; iIndex <= 10; iIndex++ )
{
Expand All @@ -100,23 +100,23 @@ public ref class MyTreeNode_FirstNode: public Form
}
}


// Suppress repainting the TreeView until it is fully created.
myTreeView->BeginUpdate();

// Clear the TreeView each time the method is called.
myTreeView->Nodes->Clear();
TreeNode^ myMainNode = gcnew TreeNode( "CustomerList" );
myTreeView->Nodes->Add( myMainNode );

// Add a root treenode for each 'Customer' in the ArrayList.
while ( myEnum->MoveNext() )
{
Customer^ myCustomer2 = safe_cast<Customer^>(myEnum->Current);
TreeNode^ myTreeNode1 = gcnew TreeNode( myCustomer2->CustomerName );
myTreeNode1->ForeColor = Color::Orange;
myTreeView->Nodes[ 0 ]->Nodes->Add( myTreeNode1 );

// Add a child for each 'Order' in the current 'Customer'.
IEnumerator^ myEnum = myCustomer2->CustomerOrders->GetEnumerator();
while ( myEnum->MoveNext() )
Expand All @@ -126,10 +126,10 @@ public ref class MyTreeNode_FirstNode: public Form
}
}


// Reset the cursor back to the default for all controls.
::Cursor::Current = Cursors::Default;

// Begin repainting the TreeView.
myTreeView->EndUpdate();
if ( myTreeView->Nodes[ 0 ]->IsExpanded == false )
Expand Down Expand Up @@ -194,33 +194,33 @@ public ref class MyTreeNode_FirstNode: public Form
// <Snippet1>
void myCheckBox_CheckedChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{

// If the check box is checked, expand all the tree nodes.
if ( myCheckBox->Checked == true )
{
myTreeView->ExpandAll();
}
else
{
// If the check box is not cheked, collapse the first tree node.

// If the check box is not checked, collapse the first tree node.
myTreeView->Nodes[ 0 ]->FirstNode->Collapse();
MessageBox::Show( "The first and last node of CutomerList root node is collapsed" );
MessageBox::Show( "The first node of CustomerList root node is collapsed" );
}
}
// </Snippet1>

// <Snippet2>
void myButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{

// Set the tree view's PathSeparator property.
myTreeView->PathSeparator = ".";

// Get the count of the child tree nodes contained in the SelectedNode.
int myNodeCount = myTreeView->SelectedNode->GetNodeCount( true );
Decimal myChildPercentage = ((Decimal)myNodeCount / (Decimal)myTreeView->GetNodeCount( true )) * 100;

// Display the tree node path and the number of child nodes it and the tree view have.
MessageBox::Show( String::Concat( "The '", myTreeView->SelectedNode->FullPath, "' node has ", myNodeCount, " child nodes.\nThat is ", String::Format( "{0:###.##}", myChildPercentage ), "% of the total tree nodes in the tree view control." ) );
}
Expand Down Expand Up @@ -258,13 +258,13 @@ public ref class MyTreeNode_FirstNode: public Form
array<Char>^temp3 = {'@','->',',','!'};
if ( e->Label->IndexOfAny( temp3 ) == -1 )
{

// Stop editing without cancelling the label change.
e->Node->EndEdit( false );
}
else
{

// Cancel the label edit action, place it in edit mode.
e->CancelEdit = true;
MessageBox::Show( "Invalid tree node label.\n The invalid characters are: '@', '.', ', ', '!'", "Node Label Edit" );
Expand All @@ -273,7 +273,7 @@ public ref class MyTreeNode_FirstNode: public Form
}
else
{

// Cancel the label edit action, place it in edit mode.
e->CancelEdit = true;
MessageBox::Show( "Invalid tree node label. The label cannot be blank", "Node Label Edit" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ private void myCheckBox_CheckedChanged(object sender, System.EventArgs e)
}
else
{
// If the check box is not cheked, collapse the first tree node.
// If the check box is not checked, collapse the first tree node.
myTreeView.Nodes[0].FirstNode.Collapse();
MessageBox.Show("The first and last node of CutomerList root node is collapsed");
MessageBox.Show("The first node of CustomerList root node is collapsed");
}
}
// </Snippet1>
Expand All @@ -198,9 +198,9 @@ private void myButton_Click(object sender, System.EventArgs e)
(decimal)myTreeView.GetNodeCount(true)) * 100;

// Display the tree node path and the number of child nodes it and the tree view have.
MessageBox.Show("The '" + myTreeView.SelectedNode.FullPath + "' node has "
+ myNodeCount.ToString() + " child nodes.\nThat is "
+ string.Format("{0:###.##}", myChildPercentage)
MessageBox.Show("The '" + myTreeView.SelectedNode.FullPath + "' node has "
+ myNodeCount.ToString() + " child nodes.\nThat is "
+ string.Format("{0:###.##}", myChildPercentage)
+ "% of the total tree nodes in the tree view control.");
}
// </Snippet2>
Expand Down Expand Up @@ -288,4 +288,4 @@ public Order(string myOrderID )
{
this.OrderID = myOrderID;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;

namespace operators
{
public static class EqualityAndNonEqualityExamples
{
public static void Examples()
{
Console.WriteLine("Value types:");
ValueTypesEquality();

Console.WriteLine("Strings:");
StringEquality();

Console.WriteLine("Reference types:");
ReferenceTypesEquality.Main();

Console.WriteLine("Non equality:");
NonEquality();
}

private static void ValueTypesEquality()
{
// <SnippetValueTypesEquality>
int a = 1 + 2 + 3;
int b = 6;
Console.WriteLine(a == b); // output: True

char c1 = 'a';
char c2 = 'A';
Console.WriteLine(c1 == c2); // output: False
Console.WriteLine(c1 == char.ToLower(c2)); // output: True
// </SnippetValueTypesEquality>
}

private static void StringEquality()
{
// <SnippetStringEquality>
string s1 = "hello!";
string s2 = "HeLLo!";
Console.WriteLine(s1 == s2.ToLower()); // output: True

string s3 = "Hello!";
Console.WriteLine(s1 == s3); // output: False
// </SnippetStringEquality>
}

// Rationale for the structure of the next snippet.
// A method cannot contain a class definition. Thus, a standard way to include snippet doesn't work.
// We want snippets to be interactive. Thus, the whole snippet has a structure of the console program.
// (Running the code without the ReferenceTypesEquality class doesn't produce any output in the interactive Output control.)

// <SnippetReferenceTypesEquality>
public class ReferenceTypesEquality
{
public class MyClass
{
private int id;

public MyClass(int id) => this.id = id;
}

public static void Main()
{
var a = new MyClass(1);
var b = new MyClass(1);
var c = a;
Console.WriteLine(a == b); // output: False
Console.WriteLine(a == c); // output: True
}
}
// </SnippetReferenceTypesEquality>

private static void NonEquality()
{
// <SnippetNonEquality>
int a = 1 + 1 + 2 + 3;
int b = 6;
Console.WriteLine(a != b); // output: True

string s1 = "Hello";
string s2 = "Hello";
Console.WriteLine(s1 != s2); // output: False

object o1 = 1;
object o2 = 1;
Console.WriteLine(o1 != o2); // output: True
// </SnippetNonEquality>
}
}
}
4 changes: 4 additions & 0 deletions snippets/csharp/language-reference/operators/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ static void Main(string[] args)
Console.WriteLine("============== / operator examples =============");
DivisionExamples.Examples();
Console.WriteLine();

Console.WriteLine("============== == and != operator examples =====");
EqualityAndNonEqualityExamples.Examples();
Console.WriteLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

class TimePeriod
{
private double seconds;
private double _seconds;

public double Hours
{
get { return seconds / 3600; }
get { return _seconds / 3600; }
set {
if (value < 0 || value > 24)
throw new ArgumentOutOfRangeException(
$"{nameof(value)} must be between 0 and 24.");

seconds = value * 3600;
_seconds = value * 3600;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

public class Person
{
private string firstName;
private string lastName;
private string _firstName;
private string _lastName;

public Person(string first, string last)
{
firstName = first;
lastName = last;
_firstName = first;
_lastName = last;
}

public string Name => $"{firstName} {lastName}";
public string Name => $"{_firstName} {_lastName}";
}

public class Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

public class SaleItem
{
string name;
decimal cost;
string _name;
decimal _cost;

public SaleItem(string name, decimal cost)
{
this.name = name;
this.cost = cost;
_name = name;
_cost = cost;
}

public string Name
{
get => name;
set => name = value;
get => _name;
set => _name = value;
}

public decimal Price
{
get => cost;
set => cost = value;
get => _cost;
set => _cost = value;
}
}

Expand Down
Loading