Last active
October 30, 2023 05:04
-
-
Save HiroNakamura/9591784 to your computer and use it in GitHub Desktop.
Ejemplos 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
/* | |
Ejemplos de código en C# | |
*/ | |
//32 for | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
string[] nombres={"fer","alma","maribel","camila"}; | |
foreach(string n in nombres){ | |
Console.WriteLine("Hola "+n); | |
} | |
Console.WriteLine("--------------------"); | |
for(int i=0;i<nombres.Length;i++){ | |
Console.WriteLine("Hola "+nombres[i]); | |
} | |
} | |
} | |
//31 switch | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
int opc=0; | |
try{ | |
Console.WriteLine("Hola elige una opcion:\n1. Iniciar\n2. Detener\n3. Reiniciar\n4. Estado\n (escribe el numero):"); | |
opc=int.Parse(Console.ReadLine()); | |
switch(opc){ | |
case 1: | |
Console.WriteLine("Iniciando el proceso..."); | |
break; | |
case 2: | |
Console.WriteLine("Deteniendo el proceso..."); | |
break; | |
case 3: | |
Console.WriteLine("Reiniciando el proceso..."); | |
break; | |
case 4: | |
Console.WriteLine("Mostrar el estado del proceso..."); | |
break; | |
default: | |
Console.WriteLine("No existe la opcion..."); | |
break; | |
} | |
}catch(Exception ex){ | |
Console.WriteLine("Ha ocurrido un error: "+ex.Message); | |
} | |
} | |
} | |
/* | |
public static int ExecuteCommand(string Command, int Timeout) | |
{ | |
int ExitCode; | |
ProcessStartInfo ProcessInfo; | |
Process Process; | |
ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command); | |
ProcessInfo.CreateNoWindow = true; | |
ProcessInfo.UseShellExecute = false; | |
Process = Process.Start(ProcessInfo); | |
Process.WaitForExit(Timeout); | |
ExitCode = Process.ExitCode; | |
Process.Close(); | |
return ExitCode; | |
} | |
*/ | |
//30 | |
using System; | |
using System.Diagnostics; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
ProcessStartInfo psi = new ProcessStartInfo(); | |
try{ | |
psi.FileName = "ejecutable.exe"; | |
psi.UseShellExecute = false; | |
psi.RedirectStandardOutput = true; | |
psi.Arguments = " "; | |
Process p = Process.Start(psi); | |
string strOutput = p.StandardOutput.ReadToEnd(); | |
p.WaitForExit(); | |
Console.WriteLine(strOutput); | |
}catch(Exception ex){ | |
Console.WriteLine("Ha ocurrido un error: "+ex.Message); | |
} | |
} | |
} | |
//29 http://www.dotnetperls.com/list | |
using System.Collections.Generic; | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
List<string> dogs = new List<string>(); // Example List | |
dogs.Add("spaniel"); // Contains: spaniel | |
dogs.Add("beagle"); // Contains: spaniel, beagle | |
dogs.Insert(1, "dalmatian"); // Contains: spaniel, dalmatian, beagle | |
foreach (string dog in dogs) // Display for verification | |
{ | |
Console.WriteLine(dog); | |
} | |
} | |
} | |
//28 diccionario | |
using System.Collections.Generic; | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
var dict = new Dictionary<int, bool>(); | |
dict.Add(3, true); | |
dict.Add(5, false); | |
Console.WriteLine("Llaves"); | |
List<int> keys = new List<int>(dict.Keys); | |
foreach (int key in keys){ | |
Console.WriteLine(key); | |
} | |
Console.WriteLine("Valores"); | |
List<bool> valores=new List<bool>(dict.Values); | |
foreach (bool b in valores){ | |
Console.WriteLine(b); | |
} | |
} | |
} | |
//27 listas | |
/* | |
using System; | |
using System.Collections.Generic; | |
//using Gtk; | |
//using GtkSharp; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
List<int> list = new List<int>(); | |
list.Add(2); | |
list.Add(3); | |
list.Add(5); | |
list.Add(7); | |
Console.WriteLine("no. de elementos: "+list.Count); | |
foreach(int l in list){ | |
Console.WriteLine("Hola "+l); | |
} | |
Console.WriteLine("-----------------"); | |
for(int i=0;i<list.Count;i++){ | |
Console.WriteLine("Hola "+list[i]); | |
} | |
//vaciamos la lista | |
list.Clear(); | |
Console.WriteLine("no. de elementos: "+list.Count); | |
int[] numeros=new int[3]; | |
numeros[0]=21; | |
numeros[1]=76; | |
numeros[2]=32; | |
Console.WriteLine("no. de elementos (array): "+numeros.Length); | |
List<int> lista=new List<int>(numeros); | |
foreach(int n in lista){ | |
Console.WriteLine("hola no. "+n); | |
} | |
List<string> heroes=new List<string>(); | |
heroes.Add("spiderman"); | |
heroes.Add("batman"); | |
heroes.Add("superman"); | |
heroes.Add("wolverine"); | |
string linea=string.Join(",",heroes.ToArray()); | |
Console.WriteLine(linea); | |
*/ | |
/* | |
MessageDialog m = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.YesNo, false, "Hello World!"); | |
ResponseType result = (ResponseType)m.Run(); | |
m.Destroy(); | |
if (result == ResponseType.Yes){ | |
Console.WriteLine("Al parecer funciono ...."); | |
} | |
*/ | |
/*Application.Init(); | |
//Create the Window | |
Window myWin = new Window("My first GTK# Application! "); | |
myWin.Resize(200,200); | |
//Create a label and put some text in it. | |
Label myLabel = new Label(); | |
myLabel.Text = "Hello World!!!!"; | |
//Add the label to the form | |
myWin.Add(myLabel); | |
//Show Everything | |
myWin.ShowAll(); | |
Application.Run();*/ | |
// } | |
//} | |
//26 Cadenas | |
using System; | |
using System.Text; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
if(args.Length!=0 && args.Length>=2){ | |
verTexto(args[0],args[1]); | |
}else{ | |
Console.WriteLine("No hay parametros suficientes, deben ser dos cadenas"); | |
} | |
} | |
public static void verTexto(string cad,string cad2){ | |
try{ | |
//equals | |
if(cad2==cad){ | |
Console.WriteLine("Son iguales ambas cadenas"); | |
}else{ | |
Console.WriteLine("No son iguales ambas cadenas"); | |
} | |
//Trim | |
cad=cad.Trim(); | |
cad2=cad2.Trim(); | |
Console.WriteLine("-----"); | |
Console.WriteLine("texto 1: "+cad); | |
Console.WriteLine("texto 2: "+cad2); | |
//CharAt | |
char c1=cad[cad.Length-2]; | |
char c2=cad2[cad2.Length-2]; | |
Console.WriteLine(c1+" - "+c2);//d-m | |
//LowerCase | |
Console.WriteLine("Minusculas:"); | |
Console.WriteLine(cad.ToLower()+" - "+cad2.ToLower()); | |
//UpperCase | |
Console.WriteLine("Mayusculas:"); | |
Console.WriteLine(cad.ToUpper()+" - "+cad2.ToUpper()); | |
//substring | |
Console.WriteLine("-----"); | |
Console.WriteLine(cad.Substring(0, 2));//fe | |
Console.WriteLine(cad2.Substring(2, 1));//m | |
//indexOf | |
Console.WriteLine("-----"); | |
Console.WriteLine(cad.IndexOf("r"));//2 | |
Console.WriteLine(cad2.IndexOf("l"));//1 | |
//replace | |
Console.WriteLine("-----"); | |
Console.WriteLine(cad.Replace("o","a")); | |
Console.WriteLine(cad2.Replace("a","o")); | |
}catch(Exception ex){ | |
Console.WriteLine(ex); | |
} | |
} | |
} | |
//25 ejecutar proceso | |
using System; | |
using System.Diagnostics; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
ProcessStartInfo info = new ProcessStartInfo(); | |
info.UseShellExecute = true; | |
info.FileName = "ejecutable.exe"; | |
info.WorkingDirectory = "C:\\usuario"; | |
Process.Start(info); | |
} | |
} | |
//24 variable is assigned but its value is never used | |
using System; | |
public class Simple{ | |
private string nombre; | |
public Simple(){} | |
public Simple(string nombre){ | |
this.nombre=nombre; | |
Console.WriteLine("titulo libro: "+this.nombre); | |
} | |
public string Nombre{ | |
get; set; | |
} | |
} | |
public class Monaso{ | |
public static void Main(string[] args){ | |
Simple simple=new Simple("El jardín desierto de los olvidados (Crónicas de un desencuentro)"); | |
simple.Nombre="Mi nombre es la soledad"; | |
Console.WriteLine("titulo libro: "+simple.Nombre); | |
} | |
} | |
//23 | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
if(args.Length==0){ | |
Console.WriteLine("No hay argumentos ..."); | |
} | |
int numero=args.Length; | |
Console.WriteLine("no. de argumentos: "+numero); | |
foreach(string arg in args){ | |
Console.WriteLine("hola no. "+arg); | |
} | |
} | |
} | |
//22 http://www.pasteall.org/50414/csharp | |
using System; | |
public class Monaso{ | |
class Libro{ | |
private string titulo; | |
//constructores | |
public Libro(){} | |
public Libro(string titulo){ this.titulo=titulo;} | |
//setter y getter | |
public string Titulo{ | |
get{ return this.titulo;} | |
set{ this.titulo=value;} | |
} | |
} | |
public static void Main(string[] args){ | |
Console.WriteLine("Funcionando correctamente..."); | |
Libro miLibro=new Libro("La columna de hierro"); | |
Console.WriteLine("Título del libro [ "+miLibro.Titulo+" ]"); | |
miLibro.Titulo="A un lado del sol radiante"; | |
Console.WriteLine("Título del libro [ "+miLibro.Titulo+" ]"); | |
} | |
} | |
//21 conversor Celsius y Fahrenheit | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
float valor=0.0F; | |
int opc=0; | |
for(;;){ | |
Console.WriteLine("\t[Conversor Celsius y Fahrenheit]"); | |
try{ | |
Console.WriteLine("Introduce valor(-1 para salir):"); | |
valor=float.Parse(Console.ReadLine()); | |
}catch(Exception ex){ | |
Console.Error.WriteLine("Ha ocurrido un error["+ex.StackTrace+"]"); | |
Environment.Exit(0); | |
} | |
if(valor<0){ | |
Environment.Exit(0); | |
} | |
Console.WriteLine("\t[Menu]\n1. Convertir a Celsius\n2. Convertir a Fahrenheit\n3. Salir"); | |
opc=int.Parse(Console.ReadLine()); | |
switch(opc){ | |
case 1: | |
Console.WriteLine("Valor: "+valor+" en Celsius: "+obtenerCelsius(valor));break; | |
case 2: | |
Console.WriteLine("Valor: "+valor+" en Fahrenheit: "+obtenerFahrenheit(valor));break; | |
case 3:Environment.Exit(0);break; | |
default:Console.WriteLine("Opcion no valida");break; | |
} | |
} | |
} | |
public static float obtenerFahrenheit(float c){ | |
return (1.8F*c)+32.0F; | |
} | |
public static float obtenerCelsius(float f){ | |
return (f-32.0F)/1.8F; | |
} | |
} | |
//20 herencia | |
using System; | |
class Vehiculo{ | |
private string duenyo; | |
private int puertas; | |
private int ruedas; | |
public Vehiculo(){} | |
public Vehiculo(string duenyo,int puertas,int ruedas){ | |
this.duenyo=duenyo; | |
this.puertas=puertas; | |
this.ruedas=ruedas; | |
} | |
public string Duenyo{ | |
get{ return this.duenyo; } | |
set{ this.duenyo=value;} | |
} | |
public int Puertas{ | |
get{ return this.puertas; } | |
set{ this.puertas=value; } | |
} | |
public int Ruedas{ | |
get{ return this.ruedas; } | |
set{ this.ruedas=value;} | |
} | |
public void verDatos(){ | |
Console.WriteLine("dueño: "+this.duenyo); | |
Console.WriteLine("no. puertas: "+this.puertas); | |
Console.WriteLine("no. ruedas: "+this.ruedas); | |
} | |
} | |
class Auto: Vehiculo{ | |
private bool descapotable; | |
public Auto(){} | |
public Auto(string duenyo,int puertas,int ruedas,bool descapotable): base(duenyo,puertas,ruedas){ | |
this.descapotable=descapotable; | |
} | |
public bool Descapotable{ | |
get{ return this.descapotable;} | |
set{ this.descapotable=value;} | |
} | |
} | |
class Camioneta: Vehiculo{ | |
private float carga; | |
public Camioneta(){} | |
public Camioneta(string duenyo,int puertas,int ruedas,float carga): base(duenyo,puertas,ruedas){ | |
this.carga=carga; | |
} | |
public float Carga{ | |
get{ return this.carga;} | |
set{ this.carga=value;} | |
} | |
} | |
public class Monaso{ | |
public static void Main(string[] args){ | |
Vehiculo miVehiculo=new Vehiculo("Ignacio Carmona",4,4); | |
Console.WriteLine("Vehiculo"); | |
miVehiculo.verDatos(); | |
Console.WriteLine("Vehiculo"); | |
miVehiculo.Duenyo="Antonio Lupo"; | |
miVehiculo.Puertas=2; | |
miVehiculo.Ruedas=4; | |
miVehiculo.verDatos(); | |
Auto miAuto=new Auto("Tomas Alvirde",2,4,true); | |
Console.WriteLine("Auto"); | |
miAuto.verDatos(); | |
if(miAuto.Descapotable){ | |
Console.WriteLine("El auto es descapotable"); | |
}else{ | |
Console.WriteLine("el auto no es descapotable"); | |
} | |
Camioneta miCamioneta=new Camioneta(); | |
miCamioneta.Duenyo="Roman Torres"; | |
miCamioneta.Puertas=2; | |
miCamioneta.Ruedas=4; | |
miCamioneta.Carga=120.45F; | |
Console.WriteLine("Camioneta"); | |
miCamioneta.verDatos(); | |
Console.WriteLine("La camioneta es de "+miCamioneta.Carga+" kg"); | |
} | |
} | |
//19 herencia | |
using System; | |
class Persona{ | |
private string nombre; | |
private string apellidoPaterno; | |
private string apellidoMaterno; | |
private int edad; | |
public Persona(){} | |
public Persona(string nombre,string apellidoPaterno,string apellidoMaterno,int edad){ | |
this. nombre=nombre; | |
this.apellidoPaterno=apellidoPaterno; | |
this.apellidoMaterno=apellidoMaterno; | |
this.edad=edad; | |
} | |
public string Nombre{ | |
get{ return this.nombre;} | |
set{ this.nombre=value;} | |
} | |
public string ApellidoPaterno{ | |
get{ return this.apellidoPaterno;} | |
set{ this.apellidoPaterno=value;} | |
} | |
public string ApellidoMaterno{ | |
get{ return this.apellidoMaterno;} | |
set{ this.apellidoMaterno=value;} | |
} | |
public int Edad{ | |
get{ return this.edad;} | |
set{ this.edad=value;} | |
} | |
} | |
class Abuelo: Persona{ | |
private string id_carnet; | |
public Abuelo(){} | |
public Abuelo(string nombre,string apellidoPaterno,string apellidoMaterno,int edad,string id_carnet): base(nombre,apellidoPaterno,apellidoMaterno,edad){ | |
this.id_carnet=id_carnet; | |
} | |
public string Id_Carnet{ | |
get{ return this.id_carnet;} | |
set{ this.id_carnet=value;} | |
} | |
} | |
class Papa: Abuelo{ | |
private string oficio; | |
public Papa(){} | |
public Papa(string nombre,string apellidoPaterno,string apellidoMaterno,int edad,string id_carnet,string oficio): base(nombre,apellidoPaterno,apellidoMaterno,edad,id_carnet){ | |
this.oficio=oficio; | |
} | |
public string Oficio{ | |
get{ return this.oficio;} | |
set{ this.oficio=value;} | |
} | |
} | |
class Nieto: Papa{ | |
public Nieto(){} | |
public Nieto(string nombre,string apellidoPaterno,string apellidoMaterno,int edad,string id_carnet,string oficio): base(nombre,apellidoPaterno,apellidoMaterno,edad,id_carnet,oficio){} | |
} | |
public class Monaso{ | |
public static void Main(string[] args){ | |
Nieto nieto=new Nieto("Horacio","Gomez","Huerta",15,"",""); | |
Console.WriteLine("Nombre: "+nieto.Nombre); | |
Console.WriteLine("pellido paterno: "+nieto.ApellidoPaterno); | |
Console.WriteLine("Apellido materno: "+nieto.ApellidoMaterno); | |
Console.WriteLine("Edad: "+nieto.Edad); | |
} | |
} | |
//18 abrir documento | |
using System; | |
using System.Diagnostics; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
try{ | |
Process.Start("CV.doc"); | |
}catch(Exception ex){ | |
Console.WriteLine("Ha ocurrido un error"+ex); | |
} | |
} | |
} | |
//17 for y foreach | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
int[] numeros=new int[]{0,1,2,3,4,5}; | |
Console.WriteLine("Uso de for"); | |
for(int i=0;i<5;i++){ | |
Console.WriteLine("hola no. "+i); | |
} | |
Console.WriteLine("Uso de foreach"); | |
foreach(int n in numeros){ | |
Console.WriteLine("Hola no. "+n); | |
} | |
Console.WriteLine("Mas ejemplos"); | |
for(int j=0;j<numeros.Length;j++){ | |
Console.WriteLine("Hola no. "+numeros[j]); | |
} | |
} | |
} | |
//16 | |
using System; | |
public class Monaso{ | |
class Libro{ | |
private string titulo; | |
public Libro(){} | |
public Libro(string titulo){ this.titulo=titulo;} | |
public void SetTitulo(string titulo){this.titulo=titulo;} | |
public string GetTitulo(){ return this.titulo; } | |
} | |
public static void Main(string[] args){ | |
Console.WriteLine("El mono esta funcionando correctamente..."); | |
Libro miLibro=new Libro("La columna de hierro"); | |
Console.WriteLine("Titulo del libro: "+miLibro.GetTitulo()); | |
} | |
} | |
//15 http://www.dotnetperls.com/list | |
using System.Collections.Generic; | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
List<string> dogs = new List<string>(); // Example List | |
dogs.Add("spaniel"); // Contains: spaniel | |
dogs.Add("beagle"); // Contains: spaniel, beagle | |
dogs.Insert(1, "dalmatian"); // Contains: spaniel, dalmatian, beagle | |
foreach (string dog in dogs) // Display for verification | |
{ | |
Console.WriteLine(dog); | |
} | |
} | |
} | |
//14 diccionario | |
using System.Collections.Generic; | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
var dict = new Dictionary<int, bool>(); | |
dict.Add(3, true); | |
dict.Add(5, false); | |
Console.WriteLine("Llaves"); | |
List<int> keys = new List<int>(dict.Keys); | |
foreach (int key in keys){ | |
Console.WriteLine(key); | |
} | |
Console.WriteLine("Valores"); | |
List<bool> valores=new List<bool>(dict.Values); | |
foreach (bool b in valores){ | |
Console.WriteLine(b); | |
} | |
} | |
} | |
//13 listas | |
using System; | |
using System.Collections.Generic; | |
//using Gtk; | |
//using GtkSharp; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
List<int> list = new List<int>(); | |
list.Add(2); | |
list.Add(3); | |
list.Add(5); | |
list.Add(7); | |
Console.WriteLine("no. de elementos: "+list.Count); | |
foreach(int l in list){ | |
Console.WriteLine("Hola "+l); | |
} | |
Console.WriteLine("-----------------"); | |
for(int i=0;i<list.Count;i++){ | |
Console.WriteLine("Hola "+list[i]); | |
} | |
//vaciamos la lista | |
list.Clear(); | |
Console.WriteLine("no. de elementos: "+list.Count); | |
int[] numeros=new int[3]; | |
numeros[0]=21; | |
numeros[1]=76; | |
numeros[2]=32; | |
Console.WriteLine("no. de elementos (array): "+numeros.Length); | |
List<int> lista=new List<int>(numeros); | |
foreach(int n in lista){ | |
Console.WriteLine("hola no. "+n); | |
} | |
List<string> heroes=new List<string>(); | |
heroes.Add("spiderman"); | |
heroes.Add("batman"); | |
heroes.Add("superman"); | |
heroes.Add("wolverine"); | |
string linea=string.Join(",",heroes.ToArray()); | |
Console.WriteLine(linea); | |
/* | |
MessageDialog m = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.YesNo, false, "Hello World!"); | |
ResponseType result = (ResponseType)m.Run(); | |
m.Destroy(); | |
if (result == ResponseType.Yes){ | |
Console.WriteLine("Al parecer funciono ...."); | |
} | |
*/ | |
/*Application.Init(); | |
//Create the Window | |
Window myWin = new Window("My first GTK# Application! "); | |
myWin.Resize(200,200); | |
//Create a label and put some text in it. | |
Label myLabel = new Label(); | |
myLabel.Text = "Hello World!!!!"; | |
//Add the label to the form | |
myWin.Add(myLabel); | |
//Show Everything | |
myWin.ShowAll(); | |
Application.Run();*/ | |
} | |
} | |
//12 Cadenas | |
using System; | |
using System.Text; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
if(args.Length!=0 && args.Length>=2){ | |
verTexto(args[0],args[1]); | |
}else{ | |
Console.WriteLine("No hay parametros suficientes, deben ser dos cadenas"); | |
} | |
} | |
public static void verTexto(string cad,string cad2){ | |
try{ | |
//equals | |
if(cad2==cad){ | |
Console.WriteLine("Son iguales ambas cadenas"); | |
}else{ | |
Console.WriteLine("No son iguales ambas cadenas"); | |
} | |
//Trim | |
cad=cad.Trim(); | |
cad2=cad2.Trim(); | |
Console.WriteLine("-----"); | |
Console.WriteLine("texto 1: "+cad); | |
Console.WriteLine("texto 2: "+cad2); | |
//CharAt | |
char c1=cad[cad.Length-2]; | |
char c2=cad2[cad2.Length-2]; | |
Console.WriteLine(c1+" - "+c2);//d-m | |
//LowerCase | |
Console.WriteLine("Minusculas:"); | |
Console.WriteLine(cad.ToLower()+" - "+cad2.ToLower()); | |
//UpperCase | |
Console.WriteLine("Mayusculas:"); | |
Console.WriteLine(cad.ToUpper()+" - "+cad2.ToUpper()); | |
//substring | |
Console.WriteLine("-----"); | |
Console.WriteLine(cad.Substring(0, 2));//fe | |
Console.WriteLine(cad2.Substring(2, 1));//m | |
//indexOf | |
Console.WriteLine("-----"); | |
Console.WriteLine(cad.IndexOf("r"));//2 | |
Console.WriteLine(cad2.IndexOf("l"));//1 | |
//replace | |
Console.WriteLine("-----"); | |
Console.WriteLine(cad.Replace("o","a")); | |
Console.WriteLine(cad2.Replace("a","o")); | |
}catch(Exception ex){ | |
Console.WriteLine(ex); | |
} | |
} | |
} | |
//11 ejecutar proceso | |
using System; | |
using System.Diagnostics; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
ProcessStartInfo info = new ProcessStartInfo(); | |
info.UseShellExecute = true; | |
info.FileName = "ejecutable.exe"; | |
info.WorkingDirectory = "C:\\usuario"; | |
Process.Start(info); | |
} | |
} | |
//10 variable is assigned but its value is never used | |
using System; | |
public class Simple{ | |
private string nombre; | |
public Simple(){} | |
public Simple(string nombre){ | |
this.nombre=nombre; | |
Console.WriteLine("titulo libro: "+this.nombre); | |
} | |
public string Nombre{ | |
get; set; | |
} | |
} | |
public class Monaso{ | |
public static void Main(string[] args){ | |
Simple simple=new Simple("El jardín desierto de los olvidados (Crónicas de un desencuentro)"); | |
simple.Nombre="Mi nombre es la soledad"; | |
Console.WriteLine("titulo libro: "+simple.Nombre); | |
} | |
} | |
//9 paso de argumentos en línea de comandos | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
if(args.Length==0){ | |
Console.WriteLine("No hay parametros..."); | |
} | |
foreach(string arg in args){ | |
Console.WriteLine("Hola no. "+arg); | |
} | |
} | |
} | |
// 8 clases | |
using System; | |
class Persona{ | |
private string nombre; | |
private string apellidoPaterno; | |
private string apellidoMaterno; | |
private int edad; | |
public Persona(){} | |
public Persona(string nombre,string apellidoPaterno,string apellidoMaterno,int edad){ | |
this.nombre=nombre; | |
this.apellidoPaterno=apellidoPaterno; | |
this.apellidoMaterno=apellidoMaterno; | |
this.edad=edad; | |
} | |
public string Nombre{ | |
get{return this.nombre;} | |
set{this.nombre=value;} | |
} | |
public string ApellidoPaterno{ | |
get{return this.apellidoPaterno;} | |
set{this.apellidoPaterno=value;} | |
} | |
public string ApellidoMaterno{ | |
get{return this.apellidoMaterno;} | |
set{this.apellidoMaterno=value;} | |
} | |
public int Edad{ | |
get{return this.edad;} | |
set{this.edad=value;} | |
} | |
public void verDatos(){ | |
Console.WriteLine("[Datos]"); | |
Console.WriteLine("nombre: "+this.nombre); | |
Console.WriteLine("apellido paterno: "+this.apellidoPaterno); | |
Console.WriteLine("apellido materno: "+this.apellidoMaterno); | |
Console.WriteLine("edad: "+this.edad); | |
} | |
} | |
public class Monaso{ | |
public static void Main(string[] args){ | |
string nombre=null, apellidoPaterno=null, apellidoMaterno=null; | |
int edad=0; | |
Console.WriteLine("Introduce nombre: "); | |
nombre=Console.ReadLine(); | |
Console.WriteLine("Introduce apellido paterno: "); | |
apellidoPaterno=Console.ReadLine(); | |
Console.WriteLine("Introduce apellido materno: "); | |
apellidoMaterno=Console.ReadLine(); | |
Console.WriteLine("Introduce edad: "); | |
edad=int.Parse(Console.ReadLine()); | |
Persona pers1=new Persona(); | |
pers1.Nombre=nombre; | |
pers1.ApellidoPaterno=apellidoPaterno; | |
pers1.ApellidoMaterno=apellidoMaterno; | |
pers1.Edad=edad; | |
pers1.verDatos(); | |
} | |
} | |
//7 string | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
Console.WriteLine("Introduce una cadena: "); | |
string cadena=Console.ReadLine(); | |
Console.WriteLine("Tamaño: "+cadena.Length); | |
Console.WriteLine("minusculas: "+cadena.ToLower()); | |
Console.WriteLine("minusculas: "+cadena.ToUpper()); | |
Console.WriteLine(cadena=="Otra cadena"? "Son iguales" : "No son iguales"); | |
Console.WriteLine(cadena.Trim().Length); | |
char c=cadena[2]; | |
Console.WriteLine(c); | |
char[] arr=cadena.ToCharArray(); | |
Array.Reverse(arr); | |
Console.WriteLine("reversa: "+new string(arr)); | |
} | |
} | |
//6 conversor Celsius y Fahrenheit | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
float valor=0.0F; | |
int opc=0; | |
for(;;){ | |
Console.WriteLine("\t[Conversor Celsius y Fahrenheit]"); | |
try{ | |
Console.WriteLine("Introduce valor(-1 para salir):"); | |
valor=float.Parse(Console.ReadLine()); | |
}catch(Exception ex){ | |
Console.Error.WriteLine("Ha ocurrido un error["+ex.StackTrace+"]"); | |
Environment.Exit(0); | |
} | |
if(valor<0){ | |
Environment.Exit(0); | |
} | |
Console.WriteLine("\t[Menu]\n1. Convertir a Celsius\n2. Convertir a Fahrenheit\n3. Salir"); | |
opc=int.Parse(Console.ReadLine()); | |
switch(opc){ | |
case 1: | |
Console.WriteLine("Valor: "+valor+" en Celsius: "+obtenerCelsius(valor));break; | |
case 2: | |
Console.WriteLine("Valor: "+valor+" en Fahrenheit: "+obtenerFahrenheit(valor));break; | |
case 3:Environment.Exit(0);break; | |
default:Console.WriteLine("Opcion no valida");break; | |
} | |
} | |
} | |
public static float obtenerFahrenheit(float c){ | |
return (1.8F*c)+32.0F; | |
} | |
public static float obtenerCelsius(float f){ | |
return (f-32.0F)/1.8F; | |
} | |
} | |
//5 herencia | |
using System; | |
class Vehiculo{ | |
private string duenyo; | |
private int puertas; | |
private int ruedas; | |
public Vehiculo(){} | |
public Vehiculo(string duenyo,int puertas,int ruedas){ | |
this.duenyo=duenyo; | |
this.puertas=puertas; | |
this.ruedas=ruedas; | |
} | |
public string Duenyo{ | |
get{ return this.duenyo; } | |
set{ this.duenyo=value;} | |
} | |
public int Puertas{ | |
get{ return this.puertas; } | |
set{ this.puertas=value; } | |
} | |
public int Ruedas{ | |
get{ return this.ruedas; } | |
set{ this.ruedas=value;} | |
} | |
public void verDatos(){ | |
Console.WriteLine("dueño: "+this.duenyo); | |
Console.WriteLine("no. puertas: "+this.puertas); | |
Console.WriteLine("no. ruedas: "+this.ruedas); | |
} | |
} | |
class Auto: Vehiculo{ | |
private bool descapotable; | |
public Auto(){} | |
public Auto(string duenyo,int puertas,int ruedas,bool descapotable): base(duenyo,puertas,ruedas){ | |
this.descapotable=descapotable; | |
} | |
public bool Descapotable{ | |
get{ return this.descapotable;} | |
set{ this.descapotable=value;} | |
} | |
} | |
class Camioneta: Vehiculo{ | |
private float carga; | |
public Camioneta(){} | |
public Camioneta(string duenyo,int puertas,int ruedas,float carga): base(duenyo,puertas,ruedas){ | |
this.carga=carga; | |
} | |
public float Carga{ | |
get{ return this.carga;} | |
set{ this.carga=value;} | |
} | |
} | |
public class Monaso{ | |
public static void Main(string[] args){ | |
Vehiculo miVehiculo=new Vehiculo("Ignacio Carmona",4,4); | |
Console.WriteLine("Vehiculo"); | |
miVehiculo.verDatos(); | |
Console.WriteLine("Vehiculo"); | |
miVehiculo.Duenyo="Antonio Lupo"; | |
miVehiculo.Puertas=2; | |
miVehiculo.Ruedas=4; | |
miVehiculo.verDatos(); | |
Auto miAuto=new Auto("Tomas Alvirde",2,4,true); | |
Console.WriteLine("Auto"); | |
miAuto.verDatos(); | |
if(miAuto.Descapotable){ | |
Console.WriteLine("El auto es descapotable"); | |
}else{ | |
Console.WriteLine("el auto no es descapotable"); | |
} | |
Camioneta miCamioneta=new Camioneta(); | |
miCamioneta.Duenyo="Roman Torres"; | |
miCamioneta.Puertas=2; | |
miCamioneta.Ruedas=4; | |
miCamioneta.Carga=120.45F; | |
Console.WriteLine("Camioneta"); | |
miCamioneta.verDatos(); | |
Console.WriteLine("La camioneta es de "+miCamioneta.Carga+" kg"); | |
} | |
} | |
//4 herencia | |
using System; | |
class Persona{ | |
private string nombre; | |
private string apellidoPaterno; | |
private string apellidoMaterno; | |
private int edad; | |
public Persona(){} | |
public Persona(string nombre,string apellidoPaterno,string apellidoMaterno,int edad){ | |
this. nombre=nombre; | |
this.apellidoPaterno=apellidoPaterno; | |
this.apellidoMaterno=apellidoMaterno; | |
this.edad=edad; | |
} | |
public string Nombre{ | |
get{ return this.nombre;} | |
set{ this.nombre=value;} | |
} | |
public string ApellidoPaterno{ | |
get{ return this.apellidoPaterno;} | |
set{ this.apellidoPaterno=value;} | |
} | |
public string ApellidoMaterno{ | |
get{ return this.apellidoMaterno;} | |
set{ this.apellidoMaterno=value;} | |
} | |
public int Edad{ | |
get{ return this.edad;} | |
set{ this.edad=value;} | |
} | |
} | |
class Abuelo: Persona{ | |
private string id_carnet; | |
public Abuelo(){} | |
public Abuelo(string nombre,string apellidoPaterno,string apellidoMaterno,int edad,string id_carnet): base(nombre,apellidoPaterno,apellidoMaterno,edad){ | |
this.id_carnet=id_carnet; | |
} | |
public string Id_Carnet{ | |
get{ return this.id_carnet;} | |
set{ this.id_carnet=value;} | |
} | |
} | |
class Papa: Abuelo{ | |
private string oficio; | |
public Papa(){} | |
public Papa(string nombre,string apellidoPaterno,string apellidoMaterno,int edad,string id_carnet,string oficio): base(nombre,apellidoPaterno,apellidoMaterno,edad,id_carnet){ | |
this.oficio=oficio; | |
} | |
public string Oficio{ | |
get{ return this.oficio;} | |
set{ this.oficio=value;} | |
} | |
} | |
class Nieto: Papa{ | |
public Nieto(){} | |
public Nieto(string nombre,string apellidoPaterno,string apellidoMaterno,int edad,string id_carnet,string oficio): base(nombre,apellidoPaterno,apellidoMaterno,edad,id_carnet,oficio){} | |
} | |
public class Monaso{ | |
public static void Main(string[] args){ | |
Nieto nieto=new Nieto("Horacio","Gomez","Huerta",15,"",""); | |
Console.WriteLine("Nombre: "+nieto.Nombre); | |
Console.WriteLine("pellido paterno: "+nieto.ApellidoPaterno); | |
Console.WriteLine("Apellido materno: "+nieto.ApellidoMaterno); | |
Console.WriteLine("Edad: "+nieto.Edad); | |
} | |
} | |
//3 abrir documento | |
using System; | |
using System.Diagnostics; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
try{ | |
Process.Start("CV.doc"); | |
}catch(Exception ex){ | |
Console.WriteLine("Ha ocurrido un error"+ex); | |
} | |
} | |
} | |
//2 for y foreach | |
using System; | |
public class Monaso{ | |
public static void Main(string[] args){ | |
int[] numeros=new int[]{0,1,2,3,4,5}; | |
Console.WriteLine("Uso de for"); | |
for(int i=0;i<5;i++){ | |
Console.WriteLine("hola no. "+i); | |
} | |
Console.WriteLine("Uso de foreach"); | |
foreach(int n in numeros){ | |
Console.WriteLine("Hola no. "+n); | |
} | |
Console.WriteLine("Mas ejemplos"); | |
for(int j=0;j<numeros.Length;j++){ | |
Console.WriteLine("Hola no. "+numeros[j]); | |
} | |
} | |
} | |
//1 ejemplos de clase | |
using System; | |
public class Monaso{ | |
class Libro{ | |
private string titulo; | |
public Libro(){} | |
public Libro(string titulo){ this.titulo=titulo;} | |
public void SetTitulo(string titulo){this.titulo=titulo;} | |
public string GetTitulo(){ return this.titulo; } | |
} | |
public static void Main(string[] args){ | |
Console.WriteLine("El mono esta funcionando correctamente..."); | |
Libro miLibro=new Libro("La columna de hierro"); | |
Console.WriteLine("Titulo del libro: "+miLibro.GetTitulo()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment