0% found this document useful (0 votes)
32 views2 pages

04 Ejemplo Modificar Struct

The document is a C# program that defines a structure for employee records and includes methods to update employee names based on their ID. It demonstrates two different approaches to modify the name of an employee with ID 100 from 'Ana' to 'Erica' and then to 'Maria'. The program also prints the original and updated employee lists to the console.

Uploaded by

Jorling Víctor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

04 Ejemplo Modificar Struct

The document is a C# program that defines a structure for employee records and includes methods to update employee names based on their ID. It demonstrates two different approaches to modify the name of an employee with ID 100 from 'Ana' to 'Erica' and then to 'Maria'. The program also prints the original and updated employee lists to the console.

Uploaded by

Jorling Víctor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace actualizar
{
class Program
{
public struct emp
{
public int id;
public string nombre;
}

public static void Actual1(List<emp> e)


{
for(int i=0;i< e.Count();i++)
{
if (e[i].id == 100)
{
emp et = e[i]; //obtener
et.nombre = "Erica"; // modificar
e[i] = et; //devolver
}
}
}
public static void Actual2(List<emp> e)
{
var nuevo = e.Find(x => x.id == 100);
int ind = e.IndexOf(nuevo);
nuevo.nombre = "Maria";
e[ind] = nuevo;
}
public static void Imp(string m, List<emp> e)
{
Console.WriteLine(m);
foreach (var c in e)
{
Console.WriteLine(c.id);
Console.WriteLine(c.nombre);
Console.WriteLine("=======================");
}
}
static void Main(string[] args)
{
List<emp> e = new List<emp>()
{
new emp { id=100, nombre="Ana"},
new emp { id=200, nombre="Alicia"},
new emp {id=300, nombre="Andrea" }
};

Imp("ORIGINAL",e);
Actual1(e);
Imp("ACTUAL1",e);
Actual2(e);
Imp("ACTUAL2",e);
Console.ReadKey();

}
}
}

You might also like