Chương trình C# đơn giản
Đợi mãi chẳng thấy bác nào post bài tiếp thôi thì em cũng đú theo câu vài bài chơi
Gì thì gì cũng phải làm 1 cái Hello World trước chứ nhỉ
Gõ y thế này vào khoan hãy thắc mắc :
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
Mấy cái linh tinh khoan chú ý , ngó vô cái này cái :
Code:
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
Hàm Main sẽ dc gọi đầu tiên để thực thi chương trình . Console.WriteLine ở đây là để ghi dòng chữ Hello World ra màn hình console . Nếu trên cùng bạn ko dùng Using System thì bạn phải ghi rõ là System.Console.WriteLine . Sau mỗi dòng lệnh phải có dấu chấm phẩy (
nhé .
Ai dùng VS 2005 thì làm phát có sẵn code thêm dòng WriteLine vào là xong .
Tiếp theo là về phần ghi chú các đoạn code :
nếu chỉ có 1 dòng thì chỉ cần dùng dấu // sau đó là dòng ghi chú của bạn . Nếu nhiều dòng thì có thể dùng /* dòng 1 , dòng 2 ... */
VD :
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
//Xuat ra man hinh
Console.WriteLine("Hello World");
/* Ket qua xuat ra man hinh se~ la` :
* Hello World
*/
}
}
}
Bi giờ quay lại với cái chương trình đầu tiên này
Bi giờ quay ngược lại nói đến cái chứ class , ko phải là lớp học đâu nhé
Bạn có thể tưởng tượng class giống như 1 cái gì đó trừu tượng bao gồm các đối tượng mà các đối tượng đó có chung các kiểu như có bánh xe hay gì đó chẳng hạn .
VD như thay vì bạn có các đối tượng như xe hơi , xe máy .... , bạn gom chung nó vào 1 class gọi là Class xe .
Phù , giở sách ra coi tiếp , lâu lắm rồi em ko coi tới cái này , quên sạch rồi , bác nào biết bổ xung tiếp giúp em , đi nấu gói mì ăn cái
Giờ thì nói đến Function , em dốt văn nên chả biết viết sao thôi thì dùng VD để nói vậy
Bây giờ tới chương trình cộng 2 số
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Tong
{
class Program
{
static double a = 2, b = 4, c;
static double Tong()
{
c = a + b;
return c;
}
static void Main(string[] args)
{
Tong();
Console.WriteLine("Tong cua a + b = {0}",c);
}
}
}
ở đây vì a,b,c nằm trong class Tong và hàm Main cũng nằm trong class Tong nên bạn phải khai báo khóa Static .
bây giờ nới đến các Function , chú ý đoạn code này :
Code:
static double Tong()
{
c = a + b;
return c;
}
đây dc gọi là function Tong . double là kiểu dữ liệu trả về cho biến c , nếu bạn ko muốn return bất cứ cái gì thì thay double bằng void vì đôi khi function của bạn chỉ dùng để thực hiện một hành động nào đó mà ko cần trả ra . ở đây khi function đc khai báo static double Tong() thì bạn phải có return c .
---
Codered(UDS)
ví dụ một!
Built in Attributes
ví dụ thể hiện một hàm dùng cho Debug
PHP Code:
using System;
using System.Diagnostics;
namespace AdvancedDotnet
{
class MainClass
{
[Conditional("DEBUG")]
public static void MsgDebug(string msg){
Console.WriteLine(msg);
}
public static void Main()
{
Console.WriteLine("this testing for ConditionalAttribute, Before calling MsgDebug()");
MsgDebug("Trying out Attributes");
Console.Read();
}
}
}
khi chay chú ý ở 2 chế độ Debug và Release
nếu chạy Debug ta sẽ thấy
Quote:
this testing for ConditionalAttribute, Before calling MsgDebug()
Trying out Attributes
ví dụ 2
gọi dll từ ngôn ngữ bất kỳ
PHP Code:
using System;
using System.Runtime.InteropServices;
namespace AdvancedDotnet
{
class MainClass
{
///Calc.dll chính là tên dll cần gọi ở đây mình gọi dll calculator của window!
[DllImport("Calc.dll")]
///Khai báo lại hàm Add của calculator chú ý: hàm Add là hàm khai đã khai báo trong calculator
public static extern int Add(int var1,int var2);
public static void Main(string[] args)
{
int a = Add(15, 20);
Console.WriteLine("Result: {0}",a);
Console.Read();
}
}
}
___________
dauden(UDS)
Ví dụ 3!
Creating Custom Attributes(tạo một Attributes)
* Lớp tạo Attributes
PHP Code:
/*
* Created by SharpDevelop.
* NetDevelop Co., Ltd.
* Author: Tuan Anh Nguyen Ngoc
* Date: 11/24/2006
* Contact Information.
* - Email:
info.netdevelop@gmail.com * - Handheld: +84 905 202 088
*/
using System;
namespace AdvancedDotnet
{
/// <summary>
/// Định nghĩa Author Attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Struct,AllowMultiple=false,Inherited=false)]
public class AuthorAttribute : Attribute
{
private string _sName;
public AuthorAttribute(string name)
{
this._sName = name;
}
}
}
Mình không có nhiều thời gian nên các bạn có thể tự tìm hiểu thêm, chú các đến Enumerator của AttributeTargets.
Sử dụng Attribute tự tạo tương tự như sử dụng Attribute.
* Lớp gọi(sử dụng) Attributes
PHP Code:
/*
* Created by SharpDevelop.
* NetDevelop Co., Ltd.
* Author: Tuan Anh Nguyen Ngoc
* Date: 11/21/2006
* Contact Information.
* - Email:
info.netdevelop@gmail.com * - Handheld: +84 905 202 088
*/
using System;
using System.Resources;
using System.Reflection;
namespace AdvancedDotnet
{
class MainClass
{
[Author("tuan anh nguyen ngoc")]
public static void Main()
{
///do samething
}
}
}
Quote:
[Author("tuan anh nguyen ngoc")]
Author chính là Attributes ta tạo ra
chúc các bạn thành công! có vấn đề gì cần hỏi có thể liên hệ với mình ở thông tin trên,
dauden(UDS)
mình mởi đầu một cái ví dụ lấy nội dung một file!
PHP Code:
using System;
using System.IO;
using System.Text;
/*
* Created by SharpDevelop.
* NetDevelop Co., Ltd.
* Author: Tuan Anh Nguyen Ngoc
* Date: 11/21/2006
* Contact Information.
* - Email:
info.netdevelop@gmail.com * - Handheld: +84 905 202 088
*/
namespace Ans.Utilities
{
public class File
{
/// <summary>
/// define string fullpath.
/// </summary>
private string _sFullPath;
/// <summary>
/// Contruction of this class.
/// </summary>
public File() { }
/// <summary>
/// Contruction of this class.
/// </summary>
/// <param name="_pfullpath">The string fullpath.</param>
public File(string _pfullpath)
{
///set fullpath.
this.FullPath = _pfullpath;
}
/// <summary>
/// Get/Set fullpath string of file.
/// </summary>
public string FullPath
{
///return fullpath.
get { return this._sFullPath; }
///set fullpath.
set { this._sFullPath = value; }
}
/// <summary>
/// Get file content.
/// </summary>
/// <returns>return file content as string.</returns>
private string FileContent()
{
///define string result.
string _sResult ="";
///check file path. throw exception if is null or empty.
if(this.FullPath == null || this.FullPath.Equals(""))
///throw exception.
throw new Exception("Can not get content! The file is not define or declare.");
else{
///check file exists, throw exception if it isn't exist.
if (System.IO.File.Exists(this.FullPath))
{
///Create filestream with filemode open and fileaccess read.
FileStream _oFileStream = new FileStream(this.FullPath, FileMode.Open, FileAccess.Read);
///Create byte array.
Byte[] _oByte = new byte[1024];
///Create UTF8Encoding.
UTF8Encoding _oUTF8Encoding = new UTF8Encoding();
///while filestream read byte > 0.
while (_oFileStream.Read(_oByte, 0, _oByte.Length)>0)
{
_sResult += _oUTF8Encoding.GetString(_oByte);
}
}
else
///throw exception.
throw new Exception("Can not get content! The file can not be found.");
}
///return result.
return _sResult;
}
/// <summary>
/// Get file content.
/// orverload fileContent method.
/// </summary>
/// <param name="_pfullpath">The fullpath string.</param>
/// <returns>return file content as string.</returns>
public string FileContent(string _pfullpath)
{
///set fullpath.
this.FullPath = _pfullpath;
///call fileContent method, return result.
return FileContent();
}
}
}
__________________
dauden(UDS)