C# 프로그래밍 -> CLR(Command Language Runtime)이 어떤 운영체제 위에서 돈다 -> 그 위에서 C# 앱이 돈다.
값 방식은 Stack
주소 방식은 Heap에 담아 참조한다.
C, C++에서는 직접 개발자가 메모리를 수거했지만, C#에서는 Java처럼 가비지 컬렉터가 존재한다.
전위/후위 연산자
Console.WriteLine(a++) 증가 되기 전 값이 출력된다. 그 이후 증간된다.
삼항 연산자
조건식 ? 참 값 : 거짓 값
비트 연산자
멤버변수들을 c#에서는 필드라고 부름
c#에서는 extends 대신 : 로 상속
class는 참조형식, 구조체는 값형식
var tuple = (123, 789);
온도 센서에서 일정 온도일 때 동작하는 보일러 객체
보일러는 임계치를 갖고 있음
온도 센서의 현재 온도를 보일러가 직접 수정 ? - 임계치 떄문에 안됨
보일러의 임계치를 온도센서가 직접 수정 ?
public 필드는 외부 객체에 의해 오염될 가능성을 열어두는 것
Getter, Setter
get접근자, set 접근자
public 필드를 다루듯 내부 필드에 접근하게 해주는 방법
Class MyClass{
private int myField;
public int MyField{
get
{
return myField;
}
set
{
myField = value;
}
}
}
MyClass obj = new MyClass();
obj.MyField = 3;
Console.WriteLine( obj.MyField);
//퍼블릭 필드를 다루듯 사용하게 해줌
public class NameCard{
public string Name{
get; set // 실제로 필드에 name이라는 변수가 반환됨
}
public string PhoneNumber{
get; set;
}
}
BirthdayInfo birth = new BirthdayInfo(){
Name = "서현",
Birthday = new DateTime(1991, 6, 28)
}
var myInstance = new { Name = "박상현", Age = "17" };
Console.WriteLine( myInstance.Name, myInstance,Age );
int[,] arr = new int[3,4] {1,2,3}, {1,2,3,4};
int[][] jagged = new int[3][];
jagged[0] = new int[5] {1,2,3,4,5};
jagged[1] = new int[] {1,2,3};
// 위처럼 뒤 배열을 가변적으로 할당할 수 있다.
ArrayList list = new ArrayList();
list.Add
list.RemoveAt
list.Insert
que.Enqueue(1);//넣음
que.Dequeue(1);//뻄
Hashtable ht = new Hashtable();
ht{"book"] = "책";
Console.WriteLine(ht["book"]);
;
void CopyArray<T\> (T[ ] source, T[ ] target){
}
CopyArray<int>(source, target);
Class도 그대로 가능하다.
class MyList<T\> where T : MyClass{
}//T가 MyClass거나 그 파생클래스면 된다.
- List<T>
- Queue<T>
- Stack<T>
- Dictionary<TKey, TValue>
Dictionary<string, string\> dic = new Dictionary<>();
private delegate int MyDelegate (int a, int b);
int Plus (int a, int b){
return a + b;
}
MyDelegate Callback;
Callback = new MyDelegate ( Plus ) ;
Console.WriteLine ( Callback (3,4);
Calculate Calc;
Calc = delegate (int a, int b){
return a+b;
}
Console.WriteLine("3+4:{0}", Calc(3,4));
delegate void EventHandler(string message);
class MyNotifier{
public event EventHandler SomethingHappend;
public void DoSomething (int number){
int temp = number % 10;
if ( temp != 0 && temp % 3 == 0){
SomethingHappend(String.Format("{0} : 짝", nubmer));
}
}
}
(매개변수목록) => 식
(매개변수목록) =>
{
문장1;
문장2;
...;
문장N;
}
delegate int Calculate(int a, int b);
static void Main(String[] args)
{
Calculate calc = (int a, int b) => a + b;
Console.WriteLine( calc(1, 2) );
}
delegate void DoSomething();
static void Main(String[] args)
{
DoSomething DoIt = ( ) =>
{
Console.WriteLine("뭔가를");
Console.WriteLine("뭔가를");
Console.WriteLine("뭔가를");
}
DoIt();
}
Func<int> func1 = () => 10; //무조건 10을 반환, <int>는 출력 매개변수
Console.WriteLine(func1());
Func<int, int> func2 = (x) = x*2; //마지막 하나는 무조건 출력 매개변수
Console.WriteLine(func2(3));
Action act1 = () => Console.WriteLine("Action()");
act1();
Action<double, double> act3 = (x, y) => //출력 매개변수가 없음
{
...
}
Expression const1 = Expression.Constant(1); //상수 1
Expression param1 = Expression.Parameter(typeof(int), "x"); //매개 변수 x
Expression exp = Expression.Add( const, param1 ); // 1 + x
Expression<Func<int, int>> lambda1 =
Expression<Func<int, int>>.Lambda<Func<int, int>>(
exp,
new ParameterExpression[] { ParameterExpression) param1 }
);
Func<int, int> compiledExp = lambda1.Compile();
Console.WriteLine( compiledExp(3) ); // x = 3이면 1+x=4이므로 4를 출력
List<Profile> profiles = new List<Profile>();
foreach (Profile profile int arrProfile)
{
if (profile.Height < 175)
profiles.Add(profile);
}
profiles.Sort(
(profile1, profile2)=>
{
return
profile1.Height - profile2.Height;
});
foreach (var profile int profiles)
Console.WriteLine("{0}, {1}",
profile.Name, profile.Height);
var profiles = from profile int arrProfile
where profile.Height < 175
orderby profile.Height
select profile;
foreach (var profile int profiles)
Console.WriteLine("{0}, {1}",
profile.Name, profile.Height);
var 변수의 타입을 select 결과로 최종 추정
group profile by profile.Height < 175 into g
select new { GroupKey = g.key, Priofiles = g };


int a = 0;
Type type = a.GetType();
FiledInfo[] fileds = type.GetFields();
/////

class MyClass
{
[Obsolete ("OldMethod는 폐기되었습니다.")]
public void OldMethod()
{
...
}
}
class History : System.Attribute
{
private string programmer;
public double Version {get; set;}
public string changes {get; set;}
public History(string programmer)
{
this.programmer = programmer;
Version = 1.0;
Changes = "First release";
}
public string Programmer
{ get { return programmer; } }
}
[History("Sean", Version = 0.1, Changes = "2010-11-01")]
[History("Bob", Version = 0.2, Changes = "2010-12-03")]
class MyClaSS
{
public void Func()
{
Console.WriteLine("Func()");
}
}
위처럼 애트리뷰틀 사용하면 실행파일로만 프로그램을 돌려도 콘솔에서 해당 코드에 대한 메모를 확인할 수 있다.


로봇은 관련이 없는데도 기능이 같아 문제 없이 실행이 된다.

namecard.py
class NameCard:
def__init__(self, name, phone):
~~~~~
NameCard(n,p)
C#
ScriptRuntime runtime = Python.CreateRuntime();
dynamic result = runtime.ExecuteFile("namecard.py");
result.name = "박상현"
~~~




프로세스
스레드
멀티 스레드




Action 대리자를 실행
Start() 메소드 : Action 대리자 비동기 실행
Factory.StartNew() 메소드 : Task 객체 생성 및 Action 대리자 비동기 실행
Wait() 메소드 : Action 대리자 실행 완료 대기


