C#學(xué)習(xí)進(jìn)階Hello World的17種寫法代碼分享
C# Hello World寫法入門:
1. 初學(xué)者
public class HelloWorld
{
public static void Main()
{
System.Console.WriteLine("HELLO WORLD");
}
}
2. 改進(jìn)的HELLO WORLD
using System;
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("HELLO WORLD");
}
}
3. 命令行形式
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine(args[0]);
}
}
4. 構(gòu)造函數(shù)
using System;
public class HelloWorld
{
public HelloWorld()
{
Console.WriteLine("HELLO WORLD");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
}
}
C# Hello World寫法進(jìn)階:
5. 面向?qū)ο?/P>
using System;
public class HelloWorld
{
public void helloWorld()
{
Console.WriteLine("HELLO WORLD");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.HelloWorld();
}
}
6. 從其他類
using System;
public class HelloWorld
{
public static void Main()
{
HelloWorldHelperClass hwh = new HelloWorldHelperClass();
hwh.writeHelloWorld();
}
}
public class HelloWorldHelperClass
{
public void writeHelloWorld()
{
Console.WriteLine("Hello World");
}
}
7. 繼承
abstract class HelloWorldBase
{
public abstract void writeHelloWorld();
}
class HelloWorld : HelloWorldBase
{
public override void writeHelloWorld()
{
Console.WriteLine("Hello World");
}
}
class HelloWorldImp
{
static void Main()
{
HelloWorldBase hwb = HelloWorld;
HelloWorldBase.writeHelloWorld();
}
}
8. 靜態(tài)構(gòu)造函數(shù)
using System;
public class HelloWorld
{
private static string strHelloWorld;
static HelloWorld()
{
strHelloWorld = "Hello World";
}
void writeHelloWorld()
{
Console.WriteLine(strHelloWorld);
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}
9. 異常處理
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
try
{
Console.WriteLine(args[0]);
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine(e.ToString());
}
}
}
10. 名字空間
using System;
namespace HelloLibrary
{
public class HelloMessage
{
public string Message
{
get
{
return "Hello, World!!!";
}
}
}
}
//------
using System;
using HelloLibrary;
namespace HelloApplication
{
class HelloApp
{
public static void Main(string[] args)
{
HelloMessage m = new HelloMessage();
}
}
}
11. 屬性
using System;
public class HelloWorld
{
public string strHelloWorld
{
get
{
return "Hello World";
}
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
Console.WriteLine(cs.strHelloWorld);
}
}
12. 代理
using System;
class HelloWorld
{
static void writeHelloWorld()
{
Console.WriteLine("HelloWorld");
}
static void Main()
{
SimpleDelegate d = new SimpleDelegate(writeHelloWorld);
d();
}
}
13. 使用屬性
#define DEBUGGING
using System;
using System.Diagnostics;
public class HelloWorld : Attribute
{
[Conditional("DEBUGGING")]
public void writeHelloWorld()
{
Console.WriteLine("Hello World");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}
14. 接口
using System;
interface IHelloWorld
{
void writeHelloWorld();
}
public class HelloWorld : IHelloWorld
{
public void writeHelloWorld()
{
Console.WriteLine("Hello World");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}
C# Hello World的特別寫法:
15. 動態(tài)Hello World
using System;
using System.Reflection;
namespace HelloWorldNS
{
public class HelloWorld
{
public string writeHelloWorld()
{
return "HelloWorld";
}
public static void Main(string[] args)
{
Type hw = Type.GetType(args[0]);
// Instantiating a class dynamically
object[] nctorParams = new object[] { };
object nobj = Activator.CreateInstance(hw, nctorParams);//, nctorParams);
// Invoking a method
object[] nmthdParams = new object[] { };
string strHelloWorld = (string)hw.InvokeMember("writeHelloWorld", BindingFlags.Default | BindingFlags.InvokeMethod, null, nobj, nmthdParams);
Console.WriteLine(strHelloWorld);
}
}
}
16. 不安全代碼Hello World
using System;
public class HelloWorld
{
unsafe public void writeHelloWorld(char[] chrArray)
{
fixed (char* parr = chrArray)
{
char* pch = parr;
for (int i = 0; i < chrArray.Length; i++)
Console.Write(*(pch + i));
}
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
char[] chrHelloWorld = new char[] { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };
hw.writeHelloWorld(chrHelloWorld);
}
}
17. 使用InteropServices
using System;
using System.Runtime.InteropServices;
class Class1
{
[DllImport("kernel32")]
private static extern int Beep(int dwFreq, int dwDuration);
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Beep(1000, 2000);
}
}
相關(guān)文章
C#中LINQ?to?DataSet操作及DataTable與LINQ相互轉(zhuǎn)換
這篇文章介紹了C#中LINQ?to?DataSet操作及DataTable與LINQ相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
VS2019配置OpenCV4.1.0詳細(xì)教程與測試代碼(推薦)
這篇文章主要介紹了VS2019配置OpenCV4.1.0詳細(xì)教程與測試代碼,本文通過截圖實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C#實(shí)現(xiàn)仿QQ抽屜式窗體的設(shè)計(jì)方法
QQ軟件對于絕大多數(shù)的人來說再熟悉不過了,它以使用方便、界面美觀及功能完善而著稱,本文給大家介紹了C#實(shí)現(xiàn)仿QQ抽屜式窗體的設(shè)計(jì)方法,主要通過使用API函數(shù)WindowFromPoint和GetParent實(shí)現(xiàn)仿QQ的抽屜式窗體,需要的朋友可以參考下2024-04-04
C#創(chuàng)建WCF服務(wù)控制臺應(yīng)用程序詳解
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建WCF服務(wù)控制臺應(yīng)用程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法
這篇文章主要介紹了VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

