본문 바로가기
학교/프로그래밍 언어

C# ready

by ehei 2024. 4. 21.

C# Compiler

// C# compiler
> csc.exe test.cs
// build to assembly
> csc.exe test.cs /target:library
// specify output file
> csc.exe test.cs /out:test.exe

ILdasm

// show intermediate language in .net
// usage
> Ildasm test.exe

 

Base template

namespace IO
{
	class Program
    {
    	// static int or static void
        // can emit the args argument
    	static void Main(string[] args)
        {}
    }
}

Standard IO

System.Console.WriteLine("Hello, World");
using static System.Console;

WriteLine("Hello, World");
using System

int i = 0;
int j = 1;

// like std::format
Console.WriteLine("{0} {1}", i, j);
// interpolate string
Console.WriteLine("$"{i} {j}");
// as raw string
Console.WriteLine(@"\\(\\)"); // \\(\\)
using System;

// read one line as string
string s = Console.ReadLine(); 
// put s
Console.WriteLine(s);

// read one character from the input buffer
int ch = Console.Read();
// put it as integer
Console.WriteLine(ch);
// put it as character
Console.WriteLine((char)ch);

// get it from keyboard not input buffer
// https://learn.microsoft.com/ko-kr/dotnet/api/system.console.readkey?view=net-7.0
ConsoleKeyInfo info = Console.ReadKey();
Console.WriteLine(info.keyChar);

Class syntax

class Foo
{
	// must declare access identifier each fields
	public int foo = 1;
	public int goo = 2;
}

// inherit as public
clas Goo : Foo
{
	/*
	but the compiler put the warning.
	if you want to avoid it, put new keyword
    
	public new int good = 3;
	*/
	public int goo = 3;
}

Foo foo = new Foo();
Goo goo = new Goo();
Console.WriteLine(goo.goo); // put 3
Console.WriteLine((Foo)goo.goo); // put 2
class Foo
{
	public void get() { Console.WriteLine("foo"); }
}

class Goo : Foo
{
	public void get() { Console.WriteLine("goo"); }
}

Foo f = new Foo();
f.get(); // foo
Goo g = new Goo();
g.get(); // goo
Foo fg = new Goo();
fg.get(); // foo. if you want to override the method that it's not virtual member, you must use new
class Foo
{
	public virtual void get() {};
}

class Goo : Foo
{
	// it uses as virtual member in C++
	public override void get() {};
}
override override a virtual method
new instance a method newly

To be continue...