Lecture No. 04
Dated: 27-03-2025
Microsoft wanted to extend java to communicate it with COM
. They wanted a clean-room
implementation of java
.
Clean Room Design
It is also known as Chinese wall technique where you reverse engineer something and copy its design, to dodge the copyright or trademark secrets associated with that thing.
However, this so called independent product cannot dodge patent restrictions.
C#
C#
was originally called COOL
(C like object oriented language). C#
is the language meanwhile .NET
is the platform.
Features
- No global
variables
orfunctions
. - Locals cannot shadow
global variables
. - There is a strict
boolean
type. - Memory address pointers can only be used in specifically marked "unsafe" blocks and require permissions.
- No instruction to "free" memory. Only garbage collection.
- Try-finally block.
- No "multiple inheritance" but interfaces supported.
- Operator overloading allowed.
- More type-safe (only integer widening allowed).
- Enumeration members are scoped.
- Property syntax for getters and setters.
- No checked exceptions.
- some functional programming features like function objects and lambda expressions.
Types
There are 2 types
Value Types
Values like int
, float
, char
, DateTime
, enum
and struct
store their actual data.
They can be always created, copied and have default values.
Reference Types
Values like object
, string
and array
store reference
to data.
They have an identity, meaning comparisons check if two references point to the same object rather than if their contents are equal unless they are overridden.
public class GenericList<T>
{
public void Add(T input) { }
}
class TestGenericList
{
private class ExampleClass { }
static void Main()
{
// Value type example (boxing and unboxing)
int foo = 42; // Value type
object bar = foo; // foo is boxed to bar
int foo2 = (int)bar; // Unboxed back to value type
// Declare a list of type int.
GenericList<int> list1 = new GenericList<int>();
// Declare a list of type string.
GenericList<string> list2 = new GenericList<string>();
// Declare a list of type ExampleClass.
GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
Console.WriteLine("Generic lists created successfully.");
}
}
Preprocessor Directives
#if
#else
#endif
#region
#endregion
Comments
//
Single Line/**/
Multiple line
Xml Documentation Style
public class Foo {
/// <summary>A summary of the method.</summary>
/// <param name="firstParam">A description of the parameter.</param>
/// <remarks>Remarks about the method.</remarks>
public static void Bar(int firstParam) { }
}
Hello World
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, world!");
}
}
Console
is a static class
in the System namespace
.
The GUI version of hello world
:
class Program {
static void Main() {
Messagebox.Show("Hello world!");
}
}
// Namespace Declaration
using System;
// Program start class
class interactivewelcome
{
// Main begins program execution.
public static void Main()
{
// Write to console/get input
Console.Write("What is your name?: ");
Console.WriteLine("Hello, {0}! ", Console.ReadLine());
Console.WriteLine("Welcome to the C# Station Tutorial!");
}
}