C# tutorial : Notes With Code Examples / PDF
C# tutorial : Notes With Code Examples / PDF
Introduction:
-
C# is a strong typed language
-
C# is completely Object Oriented
-
.NET Framework v1.x is required to run
programs written in C#
-
Microsoft Visual Studio’s version of C# is
called Visual C#
-
csc is a C# compiler
-
Code should be written using UTF-8 character
format with any file name (not necessarily .cs)
-
Compilation Steps:
File à Unicode Characters à Lexical Analysis à Syntactic Analysis à IL
-
C# compiler can generate two types of files (also
known as assemblies), .exe (or C# programs), .dll (or C# libraries)
-
C# Entry Points:
o static void Main () { }
o int void Main () { return 0; }
o static void Main (string[] args) { }
o int void Main (string[] args { return args != null ? args.Length :
0; }
-
Example:
o Creating a
C# Program: Save the following code in
ANY file
class Test {
static void Main (){}
}
Open command-prompt and run
csc filename i.e.
csc Tests.cs
o Compiling
C# library: Save the following code in
ANY file
class Test {
}
Open command-prompt and run
csc
/target:library filename
i.e. csc
/target:library Tests.cs
C# Memory Management:
-
Automatic Memory Management: System.GC.Collect(),
System.GC.SupressFinalize()
-
Types are contained in Application Domain
(instances)
-
Destructors are called by GC in
deterministically
-
Example:
using System;
public class ForceGC {
static object myObject =
new ForceGC(new string(new char[]
{'A','B','C'}));
static void Main ()
{
GC.SuppressFinalize(myObject);
//GC.Collect();
}
internal ForceGC(string s)
{
Console.WriteLine("myObject has been
created with: " + s);
}
~ForceGC()
{
Console.WriteLine(myObject + " will
be destroyed now.");
}
}
Literals In C#:
-
Integers Suffix: Any combination of ULulà 4 x 3 = 12, Ff/Dd/mM
-
Hex Prefix:
0x/0X
-
Strings:
o Regular/Verbatim, “hello”,”\”hello\””/@”hello”,@”””hello”””
o string == System.String
-
Example:
using System;
public class Literals {
static void Main ()
{
long @long
= 25L;
ushort @ushort = (ushort) 25Ul;
int hex = 0X19;
string regular = "Hello";
string withQuotes = "\"Hello\"";
string verbatim = @" ""Hello"" ";
Console.WriteLine(@"@long: {0},
@ushort: {1}, hex: {2}, regular: {3},
withQuotes: {4},
verbatim: {5}", @long, @ushort, hex,
regular, withQuotes,
verbatim);
}
}
Rarely Used Operators of C#:
-
Left Shift : <<,
<<=
-
Right Shift: >>,
>>=
-
Bitwise Negation: ~
-
Integer bitwise AND, boolean logical AND: &
-
Integer bitwise XOR, boolean logical XOR: ^
-
Integer bitwise OR, boolean logical OR: |
-
Example:
using System;
public class RareOperators {
static void Main ()
{
Console.WriteLine("8 <<
2:\t{0}", 8 << 2);
Console.WriteLine("8 >>
2:\t{0}", 8 >>2);
Console.WriteLine("~8:\t{0}",
~8);
Console.WriteLine("8 &
2:\t{0}", 8 & 2);
Console.WriteLine("8 ^ 2:\t{0}",
8 ^ 2);
Console.WriteLine("2 | 8:\t{0}",
2 | 8);
}
}
Output:
8 << 2: 32
(Same as *)
8 >> 2: 2
(Same as /)
~8: -9
8 & 2: 0
8 ^ 2: 10 (Same as +)
2 | 8: 10 (Same as +)
Pre-processing Directives In C#:
-
Used to tell the compiler while code to
process/skip........
C# tutorial : Notes With Code Examples / PDF
0 commentaires: