C#學(xué)習(xí)進(jìn)階Hello World的17種寫(xiě)法代碼分享
C# Hello World寫(xiě)法入門(mén):
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寫(xiě)法進(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. 從其他類(lèi)
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的特別寫(xiě)法:
15. 動(dòng)態(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)文章
Unity Shader實(shí)現(xiàn)素描風(fēng)格的渲染
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)素描風(fēng)格的渲染,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#實(shí)現(xiàn)注冊(cè)碼注冊(cè)機(jī)制效果詳解
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)注冊(cè)碼注冊(cè)機(jī)制效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01c# 代碼調(diào)試技巧和如何遠(yuǎn)程調(diào)試
這篇文章主要介紹了c# 代碼調(diào)試技巧和如何遠(yuǎn)程調(diào)試,幫助大家更好的理解和使用c#編程語(yǔ)言,感興趣的朋友可以了解下2020-11-11c#模擬平拋運(yùn)動(dòng)動(dòng)畫(huà)的方法詳解
本篇文章是對(duì)使用c#模擬平拋運(yùn)動(dòng)動(dòng)畫(huà)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06c# Process.Start()找不到系統(tǒng)文件的解決方法
vs1027在X64應(yīng)用程序下執(zhí)行process.start()時(shí),OK;但是在X86應(yīng)用程序下執(zhí)行process.start(),報(bào)錯(cuò):找不到系統(tǒng)文件,本文就詳細(xì)的介紹一下解決方法,感興趣的可以了解一下2023-09-09