Created
June 22, 2014 01:06
-
-
Save Fhernd/5daec30fbded50618cda to your computer and use it in GitHub Desktop.
Diferencia entre una clase y una estructura. En C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Articulos.Preguntas | |
{ | |
// Definición estructura para la entidad `Punto`: | |
public struct EstructuraPunto | |
{ | |
public int x; | |
public int y; | |
public EstructuraPunto(int x, int y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
} | |
// Define la clase para la entidad `Punto`: | |
public class ClasePunto | |
{ | |
public int x; | |
public int y; | |
public ClasePunto(int x, int y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
} | |
public sealed class EstructuraVsClase | |
{ | |
public static void Main() | |
{ | |
EstructuraPunto estructuraPunto = new EstructuraPunto(); | |
estructuraPunto.x = 11; | |
estructuraPunto.y = 11; | |
Console.WriteLine("\nValores iniciales de la estructura son: x = {0}, y = {1}", | |
estructuraPunto.x.ToString(), estructuraPunto.y.ToString()); | |
ModificacionEstructuraPunto(estructuraPunto); | |
Console.WriteLine("Después de invocar `ModificacionEstructuraPunto`, los valores de la estructura son: x = {0}, y = {1}", | |
estructuraPunto.x.ToString(), estructuraPunto.y.ToString()); | |
Console.WriteLine(); | |
ClasePunto clasePunto = new ClasePunto(11, 11); | |
Console.WriteLine("\nValores iniciales de la clase son: x = {0}, y = {1}", | |
clasePunto.x.ToString(), clasePunto.y.ToString()); | |
ModificacionClasePunto(clasePunto); | |
Console.WriteLine("Después de invocar `ModificacionClasePunto`, los valores de la clase son: x = {0}, y = {1}", | |
clasePunto.x.ToString(), clasePunto.y.ToString()); | |
Console.WriteLine(); | |
} | |
static void ModificacionEstructuraPunto (EstructuraPunto punto) | |
{ | |
punto.x = 19; | |
punto.y = 19; | |
Console.WriteLine ("\nMétodo `ModificacionEstructuraPunto`"); | |
Console.WriteLine ("Los valores modificados del parámetro `punto`: x = {0}, y = {1}", punto.x.ToString(), punto.y.ToString()); | |
} | |
static void ModificacionClasePunto(ClasePunto punto) | |
{ | |
punto.x = 19; | |
punto.y = 19; | |
Console.WriteLine ("\nMétodo `ModificacionClasePunto`"); | |
Console.WriteLine ("Los valores modificados del parámetro `punto`: x = {0}, y = {1}", punto.x.ToString(), punto.y.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment