Training
Module
Perform basic operations on numbers in C# - Training
Learn the operators and techniques used to perform basic math operations on numeric data.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The Basic sample demonstrates how to implement a data contract. Data contracts allow you to pass structured data to and from services. This sample is based on the Getting Started but uses complex numbers instead of basic numeric types.
In this sample, the service is hosted by Internet Information Services (IIS) and the client is a console application (.exe).
Note
The setup procedure and build instructions for this sample are located at the end of this topic.
The service contract for this service uses complex numbers, as shown in the following sample code.
// Define a service contract.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
ComplexNumber Add(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2);
}
The DataContractAttribute and DataMemberAttribute attributes have been applied to the definition of the ComplexNumber
class to indicate which fields of the class can be passed over the wire between the client and the service, as shown in the following sample code.
[DataContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public class ComplexNumber
{
[DataMember]
public double Real = 0.0D;
[DataMember]
public double Imaginary = 0.0D;
public ComplexNumber(double real, double imaginary)
{
this.Real = real;
this.Imaginary = imaginary;
}
}
The service implementation calculates and returns the appropriate result, accepting and returning numbers of the ComplexNumber
type.
// This is the service class that implements the service contract.
public class CalculatorService : ICalculator
{
public ComplexNumber Add(ComplexNumber n1, ComplexNumber n2)
{
return new ComplexNumber(n1.Real + n2.Real, n1.Imaginary +
n2.Imaginary);
}
public ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2)
{
return new ComplexNumber(n1.Real - n2.Real, n1.Imaginary -
n2.Imaginary);
}
public ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2)
{
double real1 = n1.Real * n2.Real;
double imaginary1 = n1.Real * n2.Imaginary;
double imaginary2 = n2.Real * n1.Imaginary;
double real2 = n1.Imaginary * n2.Imaginary * -1;
return new ComplexNumber(real1 + real2, imaginary1 +
imaginary2);
}
public ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2)
{
ComplexNumber conjugate =
new ComplexNumber(n2.Real, -1*n2.Imaginary);
ComplexNumber numerator = Multiply(n1, conjugate);
ComplexNumber denominator = Multiply(n2, conjugate);
return new ComplexNumber(numerator.Real / denominator.Real,
numerator.Imaginary);
}
}
The client implementation also uses complex numbers. Both the service contract and the data contract are defined in the source file generatedClient.cs, which is generated by the ServiceModel Metadata Utility Tool (Svcutil.exe) from service metadata.
// Create a client.
DataContractCalculatorClient client = new DataContractCalculatorClient();
// Call the Add service operation.
ComplexNumber value1 = new ComplexNumber()
{
Real = 1,
Imaginary = 2
};
ComplexNumber value2 = new ComplexNumber()
{
Real = 3,
Imaginary = 4
};
ComplexNumber result = proxy.Add(value1, value2);
Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i",
value1.Real, value1.Imaginary, value2.Real, value2.Imaginary,
result.Real, result.Imaginary);
…
}
When you run the sample, the requests and responses of the operation are displayed in the client console window. Press ENTER in the client window to shut down the client.
Add(1 + 2i, 3 + 4i) = 4 + 6i
Subtract(1 + 2i, 3 + 4i) = -2 + -2i
Multiply(2 + 3i, 4 + 7i) = -13 + 26i
Divide(3 + 7i, 5 + -2i) = 0.0344827586206897 + 41i
Press <ENTER> to terminate client.
Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.
Training
Module
Perform basic operations on numbers in C# - Training
Learn the operators and techniques used to perform basic math operations on numeric data.