顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2018年5月15日 星期二

新.NET web framework - Blazor


Tool:Visual Studio Enterprise 2017 15.7.1
SDK:.NET Core SDK 2.1.300 -RC1
OS:Windows 10
Other:ASP.NET Core Blazor Language Services


Blazor是一個新的.NET web framework,取代以網使用JavaScript語言,以WebAssembly標準為基礎,改用C#/Razor、HTML以建立執行在瀏覽器上的用戶端應用程式,有了Blazor就可以使用.NET進行全端開發(full stack web development)。


Browser + Razor = Blazor

是否有一天會取代掉JavaScript呢?讓我們看下去。

以下步驟為建立Blazor網站筆記。


2016年11月21日 星期一

C# 7 Tuple Types與Tuple Literals

 

Tool:Visual Studio 2017 RC
OS:Windows Server 2012
.NET Framework : 4.5.2、C# 7

C# 7 新增語法新增tuple types 與 tuple literals。可以讓C# Function回傳兩個以上的值。 以下步驟,在Visual Studio 2017 RC版,建立一個Console程式進行測試。

2016年4月9日 星期六

使用C#存取MongoDB

Tool:Visual Studio 2015 Enterprise Update 1
DB:MongoDB 3.2
OS:Windows 10
C#

以下為測試使用C#存取MongoDB的小筆記。要使用MongoDB之前,需要先安裝MongoDB環境,可以參考這篇文章。

2016年1月28日 星期四

C# Interactive 視窗

 

Tool:Visual Studio 2015 Enterprise Update1
OS:Windows 10
C# 6

在Visual Studio 2015 Update 1版本中,新增C# Interactive 視窗,它是一個即時運算視窗,可以讓你測試C#語法,或使用.NET Framework的功能。

C# Interactive 視窗可以

  • 支援智慧型提示 (IntelliSense) 功能,方便撰寫程式碼來進行測試。
  • 支援syntax-coloring
  • 支援Script檔,搭配csi執行
  • more…

 

2015年9月7日 星期一

Q&A:為單一ASPX檔的命名空間取別名

 

Tool:Visual Studio 2013 Ultimate Update 4
OS:Windows 8.1
C#

Q:在C#程式碼中,我們可以使用using語法,為命名空間取別名,以縮短程式的撰寫語法。那麼如何為單一ASPX檔的命名空間取別名呢?

2015年7月29日 星期三

Dynamic Partial View

Tool:Visual Studio 2013 Ultimate Update 4
OS:Windows 8.1
ASP.NET MVC 5

Partial View是一個可以重複的單位,Partial View可以是強型別的Partial View或Dynamic的。

 

2015年7月1日 星期三

在MVC檢視中繪製統計圖表

Tool:Visual Studio 2013 Ultimate Update 4
OS:Windows 8.1
ASP.NET MVC 5

Razor引擎支援Chart Helper可以讓你在MVC檢視中繪製統計圖表。

 

2014年12月3日 星期三

Tips : 串接擴充方法–C#

 

Tool:Visual Studio 2013 Ultimate Update 4
OS:Windows 8.1

在C#中,可以定義Extension Method (擴充方法),為型別新增新功能,例如以下為String型別新增Print1與Print2方法,在Main方法中,所有字串都可以叫用Print1與Print2方法,但是要分兩行叫用:

class Program {
  static void Main( string[ ] args ) {
    string s1 = "mary";
    s1.Print1( );
    s1.Print2( );
  }
}
static class StringExtension {
  public static void Print1( this string s ) {
    Console.WriteLine( "Print 1 : " + s );
  }
  public static void Print2( this string s ) {
    Console.WriteLine( "Print 2 : " + s );
  }
}

2014年2月19日 星期三

在WebSite使用ASP.NET Web API

Tool:Visual Studio 2013 Ultimate
OS:Windows 8.1
.NET Framework : 4.5.1

在Visual Studio 2013 開發Web API時,若採用Web Site方式建立網站,預設沒有輔助功能可以快速建立Web API,以下步驟說明如何在WebSite建立及使用Web API

  • New Web Site

image

  • Empty

image

  • NuGet

image

image

image

 

image

image

image

image

image

Code:

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Http" %>
<script RunAt="server">

    void Application_Start( object sender , EventArgs e ) {
        // Code that runs on application startup
        System.Web.Http.GlobalConfiguration.Configure( config =>
               config.Routes.MapHttpRoute(
                  name: "DefaultApi" ,
                  routeTemplate: "api/{controller}/{id}" ,
                  defaults: new { id = System.Web.Http.RouteParameter.Optional }
              ) );
    }

image

  • Page_Load

 

protected void Page_Load( object sender , EventArgs e ) {
  HttpClient client = new HttpClient( );
  client.BaseAddress = new Uri( "http://localhost:18541" );

  HttpResponseMessage resp = client.GetAsync( "api/values" ).Result;
  IEnumerable<string> data = null;
  if ( resp.IsSuccessStatusCode ) {
    data = resp.Content.ReadAsAsync<IEnumerable<string>>( ).Result;

  }

  foreach ( var item in data ) {
    Response.Write( item );
    Response.Write( "<br/>" );
  }
}

 

  • Test

image

 

image

2013年1月29日 星期二

ExpandoObject

C# 支援 dynamic,你可以建立System.Dynamic.ExpandoObject物件,可動態新增、移除成員。成員包含屬性、方法、事件等。

例如以下動態建立一個ExpandoObject物件,新增EmpID 、EmpName 與Age 三個屬性,並新增一個Print 方法:

dynamic emp = new System.Dynamic.ExpandoObject( );
emp.EmpID = 1;
emp.EmpName = "Mary";
emp.Age = 25;
emp.Print = (Action) ( ( ) => {
                                            Console.WriteLine( 
                                              emp.EmpID.ToString( ) + "," +
                                              emp.EmpName + "," +
                                              emp.Age.ToString( ));
                                        }
                            );
Console.WriteLine( emp.EmpID);
Console.WriteLine( emp.EmpName);
Console.WriteLine( emp.Age);
emp.Print( );

若要移除其成員,可以使用Remove方法,例如以下移除Age:

   ( (IDictionary<String , Object>) emp ).Remove( "Age" );

在Visual Studio 2012中斷模式檢視時,Age已移除:

image

2010年9月9日 星期四

.NET 4 - BigInteger

BigInteger就是很大很大的整數.....

  • 使用前專案要加入參考

  • 加入命名空間
using System.Numerics;

  • 範例
//設為1
BigInteger i = BigInteger.One;
Console.WriteLine( i.ToString());



//值是否為1, True
Console.WriteLine( i.IsOne);

//是否為偶數 False
Console.WriteLine( i.IsEven);



//一旦設定值,BigInteger是不可變的,
//以下變動值的動作將產生新的BigInteger物件
i++;



string s = "1111111111111111111111111111111111111111111111111111111111";
//string轉換成BigInteger
i = BigInteger.Parse( s );

//N0格式化字串,精確度只能50個,超過50用0取代,印出:
//N0:1,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,100,000,000
Console.WriteLine( "N0:{0:N0}" , i );


//C格式化字串,有小數點,精確度只能50個,超過50用0取代,印出:
//C:NT$1,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,100,000,000.00
Console.WriteLine( "C:{0:C}" , i );

//R格式化字串,精確度完全保留,印出:
//R:1111111111111111111111111111111111111111111111111111111111
Console.WriteLine( "R:{0:R}" , i );

2009年6月18日 星期四

C# 4 Covariant & Contravariant

C#4封閉的泛型支援Covariant & Contravariant,例如以下程式在C# 3不可編譯;在C# 4可正常執行



1 class outputsafe

2 {

3 static void Main(string[] args)

4 {

5 IEnumerable<string> data = new string[] { "a", "b" };

6 PrintData(data);

7 }

8 public static void PrintData(IEnumerable<object> o)

9 {

10 foreach (var t in o)

11 {

12 Console.WriteLine(t);

13 }

14 }

15 }

2009年6月17日 星期三

C# 4 具名參數

參數可以命名,例如:

1 class Program

2 {

3 static void Main(string[] args) {

4 Console.WriteLine(Add(x:20,y:10));

5 }

6 static int Add(int x, int y)

7 {

8 return x + y;

9 }

10 }

C# 4 選擇性引數(Optional Arguments)

宣告方法的引數,給個預設值就行了,例如:

1 class Program {

2 static void Main(string[] args) {

3 Console.WriteLine(Add(10, 20));

4 Console.WriteLine(Add(10, 20,30));

5 }

6 static int Add(int x, int y, int z = 0) {

7 return x + y + z;

8 }

9 }

2008年9月9日 星期二

匿名型別與資料繫結(Data Binding)

宣告一個List集合,初始化兩個項目,皆為匿名型別
可以直接將集合進行資料繫結,顯示在Windows forms應用程式中的ComboBox與DataGridView中:

執行結果:

2008年9月8日 星期一

foreach結合LINQ語法

foreach結合LINQ語法之後,要篩選資料更簡單! 以下篩選出陣列中,所有的偶數

為匿名型別加上方法

以下為匿名型別加上方法,方法利用 Func泛型 delegate宣告,方法可傳入兩個參數,i與j,方法中將 i 與 j相加後, 回傳加總後的整數值,而宣告匿名型別時,可以將泛型 delegate指定給匿名型別的成員XAddY:

2008年6月30日 星期一

判斷數值與轉換數值的擴充方法 (C#)

顧名思義,C#的擴充方法功能可以延伸C#的功能,以下設計兩個擴充方法,IsNumber用來判斷某個字串是否可以轉型成數值(int); ToNumber方法用來將字串轉型成數值:
public static class StringExtensions {
public static bool IsNumber(this string data) {
int num;
bool result = Int32.TryParse(data, out num);
return result;

}
public static int ToNumber(this string data) {
int num;
bool result = Int32.TryParse(data, out num);
if (result)
return num;
else
return 0;
}
}

宣告擴充方法後,後續在程式中便可以很容易使用,如:
string test = "5000";
Console.WriteLine(test.IsNumber().ToString());
int i = test.ToNumber();
Console.WriteLine(i.ToString());

總網頁瀏覽量