1.請編程實現一個冒泡排序算法?
public static void bubble_sort(int[] x) { for (int i = 0; i < x.Length; i++) { for (int j = i; j < x.Length; j++) { if (x[i] < x[j]) //從大到小排序 { int temp; temp = x[i]; x[i] = x[j]; x[j] = temp; } } } } static void Main(string[] args) { int[] huage = { 1, 5, 2, 9, 3, 7, 6,4,8,0}; bubble_sort(huage); foreach (var a in huage) { Console.WriteLine(a ); } }
2.描述一下C#中索引器的實現過程,是否只能根據數字進行索引
索引器(Indexer) 允許一個對象可以像數組一樣被索引。當您為類定義一個索引器時,該類的行為就會像一個 虛擬數組(virtual array) 一樣。您可以使用數組訪問運算符([ ])來訪問該類的實例。
using System; namespace IndexerApplication { class IndexedNames { private string[] namelist = new string[size]; static public int size = 10; public IndexedNames() { for (int i = 0; i < size; i++) { namelist[i] = "N. A."; } } public string this[int index] { get{ string tmp; if( index >= 0 && index <= size-1 ){ tmp = namelist[index]; }else{ tmp = ""; } return (tmp); } set{ if( index >= 0 && index <= size-1 ) { namelist[index] = value; } } } static void Main(string[] args) { IndexedNames names = new IndexedNames(); names[0] = "Zara"; names[1] = "Riz"; names[2] = "Nuha"; names[3] = "Asif"; names[4] = "Davinder"; names[5] = "Sunil"; names[6] = "Rubic"; for ( int i = 0; i < IndexedNames.size; i++ ) { Console.WriteLine(names[i]); } Console.ReadKey(); } } }
3.C#數據類型
在 C# 中,變量分為以下幾種類型:
- 值類型(Value types)
- 引用類型(Reference types)
- 指針類型(Pointer types)
1、值類型變量可以直接分配給一個值。它們是從類 System.ValueType 中派生的。值類型直接包含數據。比如 int、char、float,它們分別存儲數字、字母、浮點數。
2、引用類型不包含存儲在變量中的實際數據,但它們包含對變量的引用。換句話說,它們指的是一個內存位置。使用多個變量時,引用類型可以指向一個內存位置。如果內存位置的數據是由一個變量改變的,其他變量會自動反映這種值的變化。內置的 引用類型有:object、dynamic 和 string。
3、指針類型變量存儲另一種類型的內存地址。C# 中的指針與 C 或 C++ 中的指針有相同的功能。
4.什么是裝箱和拆箱?
裝箱就是隱式的將一個值型轉換為引用型對象。
拆箱就是將一個引用型對象轉換成任意值型。
比如:
int i=0;
Syste.Object obj=i;
這個過程就是裝箱!就是將 i 裝箱!
比如:
int i=0;
System.Object obj=i;
int j=(int)obj;
這個過程前2句是將 i 裝箱,后一句是將 obj 拆箱!
5.常用的調用WebService的方法有哪些?
1.使用WSDL.exe命令行工具。
2.使用VS.NET中的Add Web Reference菜單選項
web service :
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebApplication1 { /// <summary> /// WebService1 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消注釋以下行。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public int add(int a, int b) { return a+b; } } }
調用例子:
class Program { static void Main(string[] args) { WebApplication1.WebService1 wb = new WebService1(); int e = wb.add(4, 5); Console.WriteLine(e); Console.ReadLine(); } }
6.請詳述在C#中類(class)與結構(struct)的異同?
class可以被實例化,屬於引用類型,class可以實現接口和單繼承其他類,還可以作為基類型,是分配在內存的堆上的。
struct屬於值類型,不能作為基類型,但是可以實現接口,是分配在內存的棧上的。
下面的程序演示了結構的用法:
using System; struct Books { public string title; public string author; public string subject; public int book_id; }; public class testStructure { public static void Main(string[] args) { Books Book1; /* 聲明 Book1,類型為 Book */ Books Book2; /* 聲明 Book2,類型為 Book */ /* book 1 詳述 */ Book1.title = "C Programming"; Book1.author = "Nuha Ali"; Book1.subject = "C Programming Tutorial"; Book1.book_id = 6495407; /* book 2 詳述 */ Book2.title = "Telecom Billing"; Book2.author = "Zara Ali"; Book2.subject = "Telecom Billing Tutorial"; Book2.book_id = 6495700; /* 打印 Book1 信息 */ Console.WriteLine( "Book 1 title : {0}", Book1.title); Console.WriteLine("Book 1 author : {0}", Book1.author); Console.WriteLine("Book 1 subject : {0}", Book1.subject); Console.WriteLine("Book 1 book_id :{0}", Book1.book_id); /* 打印 Book2 信息 */ Console.WriteLine("Book 2 title : {0}", Book2.title); Console.WriteLine("Book 2 author : {0}", Book2.author); Console.WriteLine("Book 2 subject : {0}", Book2.subject); Console.WriteLine("Book 2 book_id : {0}", Book2.book_id); Console.ReadKey(); } }