Open ITHelpself opened 3 years ago
// Program.cs
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Tinh dong goi trong C#");
Console.WriteLine("-----------------------------------");
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
// Mảng
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Mang trong C#");
Console.WriteLine("------------------------");
int[] n = new int[10];
int i, j;
for (i=0;i<10;i++)
{
n[i] = i + 100;
}
for (j=0;j<10,j++)
{
Console.WriteLine("Phan tu [{0} = {1}", j, n[j]);
}
Console.ReadKey();
}
}
// mảng trong C#
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Mang trong C#");
Console.WriteLine("------------------------");
int[] n = new int[10];
for (int i=0;i<10;i++)
{
n[i] = i + 100;
}
foreach (int j in n)
{
int i = j - 100;
Console.WriteLine("Phan tu [{0} = {1}", j, j);
i++;
}
Console.ReadKey();
}
}
}
// Chuỗi string
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Cac cach tao chuoi trong C#");
Console.WriteLine("==================================");
//su dung phep gan hang chuoi va toan tu noi chuoi
string fname, lname;
fname = "Nguyen Thi Anh";
lname = "Thu";
string fullname = fname + " " + lname;
Console.WriteLine("Ho va ten:{0}", fullname);
//su dung constructor cua lop string
char[] letters = { 'H', 'e', 'l', 'o' };
string greetings = new string(letters);
Console.WriteLine("\nLoi chao bang tieng Anh: {0}", greetings);
//tu cac phuong thuc ma tra ve mot chuoi
string[] sarray = { "C#", "xin", "chao", "cac", "ban" };
string message = String.Join(" ", sarray);
Console.WriteLine("\nThong diep: {0}", message);
//dinh dang phuong thuc de chuyen doi mot gia tri
DateTime waiting = new DateTime(2020, 8, 1, 17, 58, 1);
string chat = String.Format("Thong diep duoc gui luc {0;t} ngay {0;D}", waiting);
Console.WriteLine("\nThong diep: {0}", chat);
Console.ReadKey();
}
}
// so sanh chuổi
static void Main(string[] args)
{
Console.WriteLine("So sanh chuoi trong C#");
Console.WriteLine("------------------------------");
string str1 = "So sanh chuoi trong C#";
string str2 = "So sanh chuoi trong Csharp";
if (String.Compare(str1, str2)== 0)
{
Console.WriteLine(str1 + " va " + str2 + " la giong nhau.");
}
else
{
Console.WriteLine(str1 + " va " + str2 " la khong giong nhau.");
}
Console.ReadKey();
}
// kiểm tra chuỗi con trong c#
static void Main(string[] args)
{
Console.WriteLine("kiem tra chuoi con trong C#");
Console.WriteLine("=====================================");
string str = "chuoi con trong c#";
if (str.Contains("trong"))
{
Console.WriteLine("tim thay chuoi con 'trong'.");
}
Console.ReadKey();
}
// lấy chuỗi con trong c#
static void Main(string[] args)
{
Console.WriteLine("lay chuoi con trong C#");
Console.WriteLine("=====================================");
string str = "lay chuoi con trong c#";
Console.WriteLine("chuoi ban dau:" + str);
string substr = str.Substring(10);
Console.WriteLine("chuoi con:" + substr);
Console.ReadKey();
}
// nối chuổi trong c#
static void Main(string[] args)
{
Console.WriteLine("Noi chuoi trong C#");
Console.WriteLine("=====================================");
string[] starray = new string[] {"Hoc C# co ban va nang cao.",
"Chuong nay trinh bay ve chuoi trong c#.",
"chung ta dang tim hieu ve noi chuoi trong C#.",
"Chuc cac em hoc tot." };
string str = String.Join("\n", starray);
Console.WriteLine(str);
Console.ReadKey();
}
truy cap cac phan tu trong mang
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("mang trong C#");
Console.WriteLine("-----------------");
int[] n = new int[10]; /*n la mot mang gom 10 so nguyen*/
int i, j;
/*khoi tao ac phan tu cua mang n */
for (i = 0; i < 10; i++)
{
n[i] = i + 100;
}
/*hien thi cac phan tu trong mang */
for (j = 0; j < 10; j++)
{
Console.WriteLine("phan tu [{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
}
/// trang 68
class ThanhVienStatic
{
public static int num;//thanh viên static
public void count ()
{
num++;
}
// phuong thuc static
public static int getNum()
{
return num;
}
}
public class TestCsharp
{
public static void Main(string[] args)
{
Console.WriteLine("Phuong thuc Static trong C#");
Console.WriteLine("------------------------------------\n");
//tao cac doi tuong ThanhVienStatic
ThanhVienStatic s = new ThanhVienStatic();
//goi phương thức
s.count();
s.count();
s.count();
Console.WriteLine("Gia tri cua num: {0}", ThanhVienStatic.getNum());
Console.ReadKey();
}
}
slide 65
class Line
{
private double chieu_dai;
public Line()//constructor
{
Console.WriteLine("doi tuong dang duoc tao.");
}
public void setChieuDai(double len)
{
chieu_dai = len;
}
public double getChieuDai()
{
return chieu_dai;
}
~Line()//destructor
{
Console.WriteLine("Doi tuong dang bi xoa !!!");
}
}
slide 66
class program1
{
public static void Details()
{
// tao doi tuong Line bang constructor
Line line = new Line();
// thiet lap chieu dai cho duong
line.setChieuDai(6.0);
Console.WriteLine("Chieu dai cua duong:{0}", line.getChieuDai());
}
static void Main(string[] args)
{
Console.WriteLine("Destructor trong C#");
Console.WriteLine("--------------------------------");
Details();
GC.Collect();//don dep bo nho
Console.ReadKey();
}
}
slide 56
class Box
{
public double chieu_dai;
public double chieu_rong;
public double chieu_cao;
static void Main(string[] args)
{
Console.WriteLine("Class trong c#");
Console.WriteLine("---------------------------------\n");
Box box1 = new Box();
Box box2 = new Box();
double thetich = 0.0;
box1.chieu_cao = 5.0;
box1.chieu_dai =6.0;
box1.chieu_rong = 7.0;
box2.chieu_cao = 5.0;
box2.chieu_dai = 6.0;
box2.chieu_rong = 7.0;
thetich = box1.chieu_cao * box1.chieu_dai * box1.chieu_rong;
Console.WriteLine("The tich cua box 1: {0}", thetich);
thetich = box2.chieu_cao * box2.chieu_dai * box2.chieu_rong;
Console.WriteLine("The tich cua box 2: {0}", thetich);
Console.ReadKey();
}
}
}
@ThanhPhong212
ThanhPhong212 commented 2 minutes ago
slide 59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lop6
{
class Box
{
public double chieu_dai;
public double chieu_rong;
public double chieu_cao;
public void setChieudai(double len)
{
chieu_dai = len;
}
public void setChieurong(double bre)
{
chieu_rong = bre;
}
public void setChieucao(double hei)
{
chieu_cao= hei;
}
public double tinhtheTich()
{
return chieu_dai * chieu_rong * chieu_cao;
}
public static void main(string[] args)
{
Box box1 = new Box();
Box box2 = new Box();
double the_tich;
box1.setChieurong(6.0);
box1.setChieudai(7.0);
box1.setChieucao(5.0);
box2.setChieurong(12.0);
box2.setChieudai(13.0);
box2.setChieucao(10.0);
the_tich = box1.tinhtheTich();
Console.WriteLine("the tich box1: {0}", the_tich);
the_tich = box2.tinhtheTich();
Console.WriteLine("the tich box2: {0}", the_tich);
Console.ReadKey();
}
}
slide 68
class ThanhVienStatic
{
public static int num;//thanh viên static
public void count ()
{
num++;
}
// phuong thuc static
public static int getNum()
{
return num;
}
}
public class TestCsharp
{
public static void Main(string[] args)
{
Console.WriteLine("Phuong thuc Static trong C#");
Console.WriteLine("------------------------------------\n");
//tao cac doi tuong ThanhVienStatic
ThanhVienStatic s = new ThanhVienStatic();
//goi phương thức
s.count();
s.count();
s.count();
Console.WriteLine("Gia tri cua num: {0}", ThanhVienStatic.getNum());
Console.ReadKey();
}
}
su dung ham class
class Program
{
static void Main(string[] args)
{
Console.WriteLine("trust trong c#");
Console.WriteLine("----------------------)\n");
Book Book1 = new Book(); /*khai bao book1 theo kieu book*/
Book Book2 = new Book(); /*khai bao book2 theo kieu book*/
/*thong tin book1*/
Book1.nhapGiaTri("English grammer in Use", "Raymond Murphy", "tieng anh", 6495407);
/*thong tin book2*/
Book2.nhapGiaTri("toan cao cap", "tran van A", "toan hoc", 6495700);
/*in thong tin book1*/
Console.WriteLine("in thong tin cuon 1:");
Book1.display();
/*in thong tin book2*/
Console.WriteLine("in thong tin cuon 2:");
Book2.display();
Console.ReadKey();
}
}
}
// Rectangle.cs