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
20 changes: 20 additions & 0 deletions snippets/cpp/System/String/.ctor/sbyte_ctor1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SByte_Ctor1.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"

// <Snippet4>
using namespace System;

void main()
{
char chars[] = { 'a', 'b', 'c', 'd', '\x00' };

char* charPtr = chars;
String^ value = gcnew String(charPtr);

Console::WriteLine(value);
}
// The example displays the following output:
// abcd
// </Snippet4>
1 change: 1 addition & 0 deletions snippets/csharp/System/Double/Overview/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Example9.Run();
8 changes: 8 additions & 0 deletions snippets/csharp/System/Double/Overview/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
</PropertyGroup>

</Project>
17 changes: 17 additions & 0 deletions snippets/csharp/System/Double/Overview/precisionlist1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// <Snippet5>
using System;

public class Example9
{
public static void Run()
{
double value1 = 1 / 3.0;
float sValue2 = 1 / 3.0f;
double value2 = (double)sValue2;
Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");
}
}

// The example displays the following output:
// 0.33333333333333331 = 0.3333333432674408: False
// </Snippet5>
4 changes: 4 additions & 0 deletions snippets/csharp/System/Double/Overview/source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public double Celsius {
}
//</snippet4>
}

namespace Snippets5 {
//<snippet5>
public class Temperature {
Expand Down Expand Up @@ -257,6 +258,7 @@ public double Celsius {
}
//</snippet5>
}

namespace Snippets6 {
//<snippet6>
public class Temperature {
Expand Down Expand Up @@ -301,6 +303,7 @@ public double Celsius {
}
//</snippet6>
}

namespace Snippets7 {
//<snippet7>
public class Temperature {
Expand Down Expand Up @@ -345,6 +348,7 @@ public double Celsius {
}
//</snippet7>
}

namespace Snippets8 {
//<snippet8>
public class Temperature {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// <Snippet2>
using System;

public class TestFormatter
{
public static void Main()
{
int acctNumber = 79203159;
Console.WriteLine(String.Format(new CustomerFormatter(), "{0}", acctNumber));
Console.WriteLine(String.Format(new CustomerFormatter(), "{0:G}", acctNumber));
Console.WriteLine(String.Format(new CustomerFormatter(), "{0:S}", acctNumber));
Console.WriteLine(String.Format(new CustomerFormatter(), "{0:P}", acctNumber));
try {
Console.WriteLine(String.Format(new CustomerFormatter(), "{0:X}", acctNumber));
}
catch (FormatException e) {
Console.WriteLine(e.Message);
}
}
}

public class CustomerFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}

public string Format(string format,
object arg,
IFormatProvider formatProvider)
{
if (! this.Equals(formatProvider))
{
return null;
}
else
{
if (String.IsNullOrEmpty(format))
format = "G";

string customerString = arg.ToString();
if (customerString.Length < 8)
customerString = customerString.PadLeft(8, '0');

format = format.ToUpper();
switch (format)
{
case "G":
return customerString.Substring(0, 1) + "-" +
customerString.Substring(1, 5) + "-" +
customerString.Substring(6);
case "S":
return customerString.Substring(0, 1) + "/" +
customerString.Substring(1, 5) + "/" +
customerString.Substring(6);
case "P":
return customerString.Substring(0, 1) + "." +
customerString.Substring(1, 5) + "." +
customerString.Substring(6);
default:
throw new FormatException(
String.Format("The '{0}' format specifier is not supported.", format));
}
}
}
}
// The example displays the following output:
// 7-92031-59
// 7-92031-59
// 7/92031/59
// 7.92031.59
// The 'X' format specifier is not supported.
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <Snippet2>
using System;

public class Example
public class FormatExample2
{
public enum TemperatureScale
{ Celsius, Fahrenheit, Kelvin }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

public class Example
public class FormatExample3
{
public static void Main()
{
Expand Down
7 changes: 4 additions & 3 deletions snippets/csharp/System/FormatException/Overview/format4.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;

public class Example
public class FormatExample4
{
public static void Main()
public static void Run()
{
// <Snippet4>
string formatString = " {0,10} ({0,8:X8})\n" +
"And {1,10} ({1,8:X8})\n" +
" = {2,10} ({2,8:X8})";
int value1 = 16932;
int value2 = 15421;
string result = String.Format(formatString,
string result = string.Format(formatString,
value1, value2, value1 & value2);
Console.WriteLine(result);

// The example displays the following output:
// 16932 (00004224)
// And 15421 (00003C3D)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

public class Example
public class FormatExample5
{
public static void Main()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

public class Example
public class FormatExample7
{
public static void Main()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public CityInfo(String name, int population, Decimal area, int year)
public readonly int Year;
}

public class Example
public class FormatExample10
{
public static void Main()
{
Expand All @@ -38,4 +38,4 @@ private static void ShowPopulationData(CityInfo city)
// The example displays the following output:
// New York in 2010: Population 8,175,133, Area 302.6 sq. feet
// Seattle in 2010: Population 608,660, Area 83.9 sq. feet
// </Snippet10>
// </Snippet10>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

public class Example
public class FormatExample6
{
public static void Main()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

public class Example2
{
public static void Main()
{
// <Snippet8>
DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0);
string city = "Chicago";
int temp = -16;
string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
dat, city, temp);
Console.WriteLine(output);
// The example displays output like the following:
// At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
// </Snippet8>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;

public class Example3
{
public static void Main()
{
// <Snippet9>
// Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
Tuple<string, DateTime, int, DateTime, int>[] cities =
{ Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277,
new DateTime(1950, 1, 1), 1970358),
Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995,
new DateTime(1950, 1, 1), 7891957),
Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808,
new DateTime(1950, 1, 1), 3620962),
Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452,
new DateTime(1950, 1, 1), 1849568) };

// Display header
var header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
"City", "Year", "Population", "Change (%)");
Console.WriteLine(header);
foreach (var city in cities) {
var output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
(city.Item5 - city.Item3)/ (double)city.Item3);
Console.WriteLine(output);
}
// The example displays the following output:
// City Year Population Year Population Change (%)
//
// Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
// New York 1940 7,454,995 1950 7,891,957 5.9 %
// Chicago 1940 3,396,808 1950 3,620,962 6.6 %
// Detroit 1940 1,623,452 1950 1,849,568 13.9 %
// </Snippet9>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

public class Example4
{
public static void Main()
{
// <Snippet12>
var value = String.Format("{0,-10:C}", 126347.89m);
Console.WriteLine(value);
// </Snippet12>
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <Snippet7>
using System;

public class Example
public class FormatExample8
{
public static void Main()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <Snippet8>
using System;

public class Example
public class FormatExample9
{
public static void Main()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// <Snippet9>
using System;

public class Example
public class IFormattableExample3
{
public static void Main()
{
string guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb";
Console.WriteLine(Guid.ParseExact(guidString, "G"));
}
}

// The example displays the following output:
// Unhandled Exception: System.FormatException:
// Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// <Snippet10>
using System;

public class Example
public class FormatExample11
{
public static void Main()
{
Expand Down
Loading
Loading