logo search
CSharp_Prog_Guide

Результат

CoOrds 1: x = 0, y = 0

CoOrds 2: x = 10, y = 10

Example 2

Description

This example demonstrates a feature that is unique to structs. It creates a CoOrds object without using the new operator. If you replace the word struct with the word class, the program will not compile.

Code

public struct CoOrds

{

public int x, y;

public CoOrds(int p1, int p2)

{

x = p1;

y = p2;

}

}

// Declare a struct object without "new."

class TestCoOrdsNoNew

{

static void Main()

{

// Declare an object:

CoOrds coords1;

// Initialize:

coords1.x = 10;

coords1.y = 20;

// Display results:

System.Console.Write("CoOrds 1: ");

System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);

}

}

Output

CoOrds 1: x = 10, y = 20