Skip to content
Open
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 @@ -31,14 +31,8 @@ static void ProcessRequest(object? requestId)
PerformLogging();
}

static void PerformDatabaseOperation()
{
Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Processing DB operation for request {_requestId}");
}
static void PerformDatabaseOperation() => Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Processing DB operation for request {_requestId}");

static void PerformLogging()
{
Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Logging request {_requestId}");
}
static void PerformLogging() => Console.WriteLine($"Thread {Environment.CurrentManagedThreadId}: Logging request {_requestId}");
}
// </Snippet1>
40 changes: 19 additions & 21 deletions snippets/csharp/System/TimeSpan/.ctor/ctoriii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,35 @@
class TimeSpanCtorIIIDemo
{
// Create a TimeSpan object and display its value.
static void CreateTimeSpan( int hours, int minutes,
int seconds )
static void CreateTimeSpan(int hours, int minutes,
int seconds)
{
TimeSpan elapsedTime =
new TimeSpan( hours, minutes, seconds );
TimeSpan elapsedTime =
new(hours, minutes, seconds);

// Format the constructor for display.
string ctor = String.Format( "TimeSpan( {0}, {1}, {2} )",
hours, minutes, seconds);
string ctor = $"TimeSpan( {hours}, {minutes}, {seconds} )";

// Display the constructor and its value.
Console.WriteLine( "{0,-37}{1,16}",
ctor, elapsedTime.ToString( ) );
Console.WriteLine($"{ctor,-37}{elapsedTime,16}");
}
static void Main( )

static void Main()
{
Console.WriteLine(
"This example of the TimeSpan( int, int, int ) " +
"\nconstructor generates the following output.\n" );
Console.WriteLine( "{0,-37}{1,16}", "Constructor", "Value" );
Console.WriteLine( "{0,-37}{1,16}", "-----------", "-----" );
"\nconstructor generates the following output.\n");
Console.WriteLine($"{"Constructor",-37}{"Value",16}");
Console.WriteLine($"{"-----------",-37}{"-----",16}");

CreateTimeSpan( 10, 20, 30 );
CreateTimeSpan( -10, 20, 30 );
CreateTimeSpan( 0, 0, 37230 );
CreateTimeSpan( 1000, 2000, 3000 );
CreateTimeSpan( 1000, -2000, -3000 );
CreateTimeSpan( 999999, 999999, 999999 );
}
}
CreateTimeSpan(10, 20, 30);
CreateTimeSpan(-10, 20, 30);
CreateTimeSpan(0, 0, 37230);
CreateTimeSpan(1000, 2000, 3000);
CreateTimeSpan(1000, -2000, -3000);
CreateTimeSpan(999999, 999999, 999999);
}
}

/*
This example of the TimeSpan( int, int, int )
Expand Down
40 changes: 19 additions & 21 deletions snippets/csharp/System/TimeSpan/.ctor/ctoriiii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,33 @@
class Example
{
// Create a TimeSpan object and display its value.
static void CreateTimeSpan( int days, int hours,
int minutes, int seconds )
static void CreateTimeSpan(int days, int hours,
int minutes, int seconds)
{
TimeSpan elapsedTime =
new TimeSpan( days, hours, minutes, seconds );
TimeSpan elapsedTime =
new(days, hours, minutes, seconds);

// Format the constructor for display.
string ctor =
String.Format( "TimeSpan( {0}, {1}, {2}, {3} )",
days, hours, minutes, seconds);
string ctor =
$"TimeSpan( {days}, {hours}, {minutes}, {seconds} )";

// Display the constructor and its value.
Console.WriteLine( "{0,-44}{1,16}",
ctor, elapsedTime.ToString( ) );
Console.WriteLine($"{ctor,-44}{elapsedTime,16}");
}
static void Main( )

static void Main()
{
Console.WriteLine( "{0,-44}{1,16}", "Constructor", "Value" );
Console.WriteLine( "{0,-44}{1,16}", "-----------", "-----" );
Console.WriteLine($"{"Constructor",-44}{"Value",16}");
Console.WriteLine($"{"-----------",-44}{"-----",16}");

CreateTimeSpan( 10, 20, 30, 40 );
CreateTimeSpan( -10, 20, 30, 40 );
CreateTimeSpan( 0, 0, 0, 937840 );
CreateTimeSpan( 1000, 2000, 3000, 4000 );
CreateTimeSpan( 1000, -2000, -3000, -4000 );
CreateTimeSpan( 999999, 999999, 999999, 999999 );
}
}
CreateTimeSpan(10, 20, 30, 40);
CreateTimeSpan(-10, 20, 30, 40);
CreateTimeSpan(0, 0, 0, 937840);
CreateTimeSpan(1000, 2000, 3000, 4000);
CreateTimeSpan(1000, -2000, -3000, -4000);
CreateTimeSpan(999999, 999999, 999999, 999999);
}
}
// The example displays the following output:
// Constructor Value
// ----------- -----
Expand Down
44 changes: 21 additions & 23 deletions snippets/csharp/System/TimeSpan/.ctor/ctoriiiii.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
//<Snippet4>
// Example of the TimeSpan( int, int, int, int, int ) constructor.
// Example of the TimeSpan( int, int, int, int, int ) constructor.
using System;

class TimeSpanCtorIIIIIDemo
{
// Create a TimeSpan object and display its value.
static void CreateTimeSpan( int days, int hours,
int minutes, int seconds, int millisec )
static void CreateTimeSpan(int days, int hours,
int minutes, int seconds, int millisec)
{
TimeSpan elapsedTime = new TimeSpan(
days, hours, minutes, seconds, millisec );
TimeSpan elapsedTime = new(
days, hours, minutes, seconds, millisec);

// Format the constructor for display.
string ctor =
String.Format( "TimeSpan( {0}, {1}, {2}, {3}, {4} )",
days, hours, minutes, seconds, millisec);
string ctor =
$"TimeSpan( {days}, {hours}, {minutes}, {seconds}, {millisec} )";

// Display the constructor and its value.
Console.WriteLine( "{0,-48}{1,24}",
ctor, elapsedTime.ToString( ) );
Console.WriteLine($"{ctor,-48}{elapsedTime,24}");
}

static void Main( )
static void Main()
{
Console.WriteLine(
Console.WriteLine(
"This example of the " +
"TimeSpan( int, int, int, int, int ) " +
"\nconstructor generates the following output.\n" );
Console.WriteLine( "{0,-48}{1,16}", "Constructor", "Value" );
Console.WriteLine( "{0,-48}{1,16}", "-----------", "-----" );
"\nconstructor generates the following output.\n");
Console.WriteLine($"{"Constructor",-48}{"Value",16}");
Console.WriteLine($"{"-----------",-48}{"-----",16}");

CreateTimeSpan( 10, 20, 30, 40, 50 );
CreateTimeSpan( -10, 20, 30, 40, 50 );
CreateTimeSpan( 0, 0, 0, 0, 937840050 );
CreateTimeSpan( 1111, 2222, 3333, 4444, 5555 );
CreateTimeSpan( 1111, -2222, -3333, -4444, -5555 );
CreateTimeSpan( 99999, 99999, 99999, 99999, 99999 );
}
}
CreateTimeSpan(10, 20, 30, 40, 50);
CreateTimeSpan(-10, 20, 30, 40, 50);
CreateTimeSpan(0, 0, 0, 0, 937840050);
CreateTimeSpan(1111, 2222, 3333, 4444, 5555);
CreateTimeSpan(1111, -2222, -3333, -4444, -5555);
CreateTimeSpan(99999, 99999, 99999, 99999, 99999);
}
}

/*
This example of the TimeSpan( int, int, int, int, int )
Expand Down
46 changes: 23 additions & 23 deletions snippets/csharp/System/TimeSpan/.ctor/ctorl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@
class TimeSpanCtorLDemo
{
// Create a TimeSpan object and display its value.
static void CreateTimeSpan( long ticks )
static void CreateTimeSpan(long ticks)
{
TimeSpan elapsedTime = new TimeSpan( ticks );
TimeSpan elapsedTime = new(ticks);

// Format the constructor for display.
string ctor = String.Format( "TimeSpan( {0} )", ticks );
string ctor = $"TimeSpan( {ticks} )";

// Pad the end of a TimeSpan string with spaces if
// it does not contain milliseconds.
string elapsedStr = elapsedTime.ToString( );
int pointIndex = elapsedStr.IndexOf( ':' );
string elapsedStr = elapsedTime.ToString();
int pointIndex = elapsedStr.IndexOf(':');

pointIndex = elapsedStr.IndexOf( '.', pointIndex );
if( pointIndex < 0 ) elapsedStr += " ";
pointIndex = elapsedStr.IndexOf('.', pointIndex);
if (pointIndex < 0) elapsedStr += " ";

// Display the constructor and its value.
Console.WriteLine( "{0,-33}{1,24}", ctor, elapsedStr );
Console.WriteLine($"{ctor,-33}{elapsedStr,24}");
}
static void Main( )

static void Main()
{
Console.WriteLine(
Console.WriteLine(
"This example of the TimeSpan( long ) constructor " +
"\ngenerates the following output.\n" );
Console.WriteLine( "{0,-33}{1,16}", "Constructor", "Value" );
Console.WriteLine( "{0,-33}{1,16}", "-----------", "-----" );

CreateTimeSpan( 1 );
CreateTimeSpan( 999999 );
CreateTimeSpan( -1000000000000 );
CreateTimeSpan( 18012202000000 );
CreateTimeSpan( 999999999999999999 );
CreateTimeSpan( 1000000000000000000 );
}
}
"\ngenerates the following output.\n");
Console.WriteLine($"{"Constructor",-33}{"Value",16}");
Console.WriteLine($"{"-----------",-33}{"-----",16}");

CreateTimeSpan(1);
CreateTimeSpan(999999);
CreateTimeSpan(-1000000000000);
CreateTimeSpan(18012202000000);
CreateTimeSpan(999999999999999999);
CreateTimeSpan(1000000000000000000);
}
}

/*
This example of the TimeSpan( long ) constructor
Expand Down
52 changes: 26 additions & 26 deletions snippets/csharp/System/TimeSpan/Add/add1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@

public class Example
{
public static void Main()
{
// <Snippet1>
TimeSpan baseTimeSpan = new TimeSpan(1, 12, 15, 16);
public static void Main()
{
// <Snippet1>
TimeSpan baseTimeSpan = new(1, 12, 15, 16);

// Create an array of timespan intervals.
TimeSpan[] intervals = {
TimeSpan.FromDays(1.5),
TimeSpan.FromHours(1.5),
TimeSpan.FromMinutes(45),
// Create an array of timespan intervals.
TimeSpan[] intervals = [
TimeSpan.FromDays(1.5),
TimeSpan.FromHours(1.5),
TimeSpan.FromMinutes(45),
TimeSpan.FromMilliseconds(505),
new TimeSpan(1, 17, 32, 20),
new TimeSpan(-8, 30, 0)
};
new TimeSpan(1, 17, 32, 20),
new TimeSpan(-8, 30, 0)
];

// Calculate a new time interval by adding each element to the base interval.
foreach (var interval in intervals)
Console.WriteLine(@"{0,-10:g} {3} {1,15:%d\:hh\:mm\:ss\.ffff} = {2:%d\:hh\:mm\:ss\.ffff}",
baseTimeSpan, interval, baseTimeSpan.Add(interval),
interval < TimeSpan.Zero ? "-" : "+");
// Calculate a new time interval by adding each element to the base interval.
foreach (var interval in intervals)
Console.WriteLine(@"{0,-10:g} {3} {1,15:%d\:hh\:mm\:ss\.ffff} = {2:%d\:hh\:mm\:ss\.ffff}",
baseTimeSpan, interval, baseTimeSpan.Add(interval),
interval < TimeSpan.Zero ? "-" : "+");

// The example displays the following output:
// 1:12:15:16 + 1:12:00:00.0000 = 3:00:15:16.0000
// 1:12:15:16 + 0:01:30:00.0000 = 1:13:45:16.0000
// 1:12:15:16 + 0:00:45:00.0000 = 1:13:00:16.0000
// 1:12:15:16 + 0:00:00:00.5050 = 1:12:15:16.5050
// 1:12:15:16 + 1:17:32:20.0000 = 3:05:47:36.0000
// 1:12:15:16 - 0:07:30:00.0000 = 1:04:45:16.0000
// </Snippet1>
}
// The example displays the following output:
// 1:12:15:16 + 1:12:00:00.0000 = 3:00:15:16.0000
// 1:12:15:16 + 0:01:30:00.0000 = 1:13:45:16.0000
// 1:12:15:16 + 0:00:45:00.0000 = 1:13:00:16.0000
// 1:12:15:16 + 0:00:00:00.5050 = 1:12:15:16.5050
// 1:12:15:16 + 1:17:32:20.0000 = 3:05:47:36.0000
// 1:12:15:16 - 0:07:30:00.0000 = 1:04:45:16.0000
// </Snippet1>
}
}
28 changes: 13 additions & 15 deletions snippets/csharp/System/TimeSpan/Compare/compare1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ static void Main()
{
// <Snippet1>
// Define a time interval equal to two hours.
TimeSpan baseInterval = new TimeSpan( 2, 0, 0);
TimeSpan baseInterval = new(2, 0, 0);

// Define an array of time intervals to compare with
// the base interval.
TimeSpan[] spans = {
TimeSpan[] spans = [
TimeSpan.FromSeconds(-2.5),
TimeSpan.FromMinutes(20),
TimeSpan.FromHours(1),
TimeSpan.FromHours(1),
TimeSpan.FromMinutes(90),
baseInterval,
TimeSpan.FromDays(.5),
TimeSpan.FromDays(1)
};
baseInterval,
TimeSpan.FromDays(.5),
TimeSpan.FromDays(1)
];

// Compare the time intervals.
foreach (var span in spans) {
int result = TimeSpan.Compare(baseInterval, span);
Console.WriteLine("{0} {1} {2} (Compare returns {3})",
baseInterval,
result == 1 ? ">" : result == 0 ? "=" : "<",
span, result);
foreach (var span in spans)
{
int result = TimeSpan.Compare(baseInterval, span);
Console.WriteLine($"{baseInterval} {(result == 1 ? ">" : result == 0 ? "=" : "<")} {span} (Compare returns {result})");
}

// The example displays the following output:
Expand All @@ -38,5 +36,5 @@ static void Main()
// 02:00:00 < 12:00:00 (Compare returns -1)
// 02:00:00 < 1.00:00:00 (Compare returns -1)
// </Snippet1>
}
}
}
}
Loading
Loading