Tutorial by Examples

public struct Vector { public int X; public int Y; public int Z; } public struct Point { public decimal x, y; public Point(decimal pointX, decimal pointY) { x = pointX; y = pointY; } } struct instance fields can be set via a p...
With constructor: Vector v1 = new Vector(); v1.X = 1; v1.Y = 2; v1.Z = 3; Console.WriteLine("X = {0}, Y = {1}, Z = {2}",v1.X,v1.Y,v1.Z); // Output X=1,Y=2,Z=3 Vector v1 = new Vector(); //v1.X is not assigned v1.Y = 2; v1.Z = 3; Console.WriteLine("X = {0}, Y = {1}, Z =...
public interface IShape { decimal Area(); } public struct Rectangle : IShape { public decimal Length { get; set; } public decimal Width { get; set; } public decimal Area() { return Length * Width; } }
Sinse structs are value types all the data is copied on assignment, and any modification to the new copy does not change the data for the original copy. The code snippet below shows that p1 is copied to p2 and changes made on p1 does not affect p2 instance. var p1 = new Point { x = 1, y =...

Page 1 of 1