반응형
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
File.WriteAllText("C:\\factory.txt","factory");
}
}
}
c 드라이브에 권한이 걸려 있어서 물론 관리자 권한으로 실행해야한다. exe
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public class Product : IComparable // i는 인터페이스의 약자
{
public string name;
public int price;
public string Name
{
get { return name; }
set { name = value; }
}
public int Price
{
get { return price ; }
set { price = value ; }
}
public override string ToString() //투스트링 메소드를 만들어서 다때려 박아서 출력 가능
{
return Name + ":" + Price + "원" + "\n";
}
int IComparable.CompareTo(object obj)
{
//return Name.CompareTo((obj as Product).Name); //cast를 좀더 우아하게
return Price.CompareTo((obj as Product).Price); //cast를 좀더 우아하게
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<Product> aList = new List<Product>() {
new Product(){Name ="고메" , Price =1000},
new Product(){Name ="군고메" , Price =1300},
new Product(){Name ="찐고메" , Price =1200},
new Product(){Name ="짠고메" , Price =1600},
new Product(){Name ="맛탕" , Price =1900},
};
aList.Sort();
string result = "";
foreach (var item in aList)
{
result = result + item;
}
MessageBox.Show(result);
}
}
반응형