Title: Introduction to C
1Introduction to C
2C its more than just a note
- C is a new language in the C, C family
- Its modern, simple, object-oriented, and type
safe - C is a part of a wide range of technologies
produced by Microsoft known as .NET
3 Isnt C only Microsoft?
- C was designed by Microsoft
- Microsoft has worked to make C an open standard
- The CLI foundation is an open standard
- 3rd Party Compilers Borland C Builder, Mono
(open source)
4THREE GUIDING PRINCIPLES
- Simple Preservation
- Incremental Improvement
- Thoughtful Innovation
5Simple Preservation
- If C or C addresses a problem well, nothing was
changed - Expressions, statements, and overall syntax are
derived from C and C - Programmers from C and C should feel
comfortable from the beginning
6(A few of the) Incremental Improvements
- Variables must be initialized
- if and while expressions must evaluate to Boolean
values to prevent accidental mistakes like
instead of at compilation - No silent fall-through in switch statements
(omitting break statements irritates the
compiler)
7Thoughtful Innovation
- Automatic memory management
- Unified Type System (everything is an object!)
- Properties, Methods, and Events are fundamental
(more on this later) - Enhanced documentation and attributes (more on
this later)
8Show me the CODE
- namespace CSharpPractice
-
- class HelloWord
-
- static void Main()
-
- System.Console.WriteLine(Hello World)
-
-
9One-Stop Programming
- Similar to Java style
- Unlike C there is no need for header files
- The idea hear is that classes should be
encapsulated modules that could easy be added
to other programs
10Namespaces
- Method of grouping elements (such as classes and
other namespaces) to avoid naming collisions - Namespaces are also in C
- Syntax for accessing elements belonging to a
namespace is the . operator - C syntax is the operator
11Why Namespaces??
- Namespaces are meant for HUGE programs and
frameworks - In a huge program with different modules designed
by different companies there may be classes with
the same name - If each company uses their company name as their
namespace, naming collisions are reduced
substantially
12Namespace Example 1
- namespace MyCompany
-
- class Grid
-
-
- class OtherClass
-
-
Now when the user of Grid wants to reference it
the coder would have to first qualify (explain to
the compiler) which class we are talking about by
prefixing the class name with the namespace name
i.e. MyCompany.Grid
13Main method
- Every program must have one class with the method
Main - Works just like main in C
- Must be static (more on this later)
14Writing Text to Console
- To write text to the console in our simple
program we called the method System.Console.WriteL
ine - This probably seems a little tedious
- To save on typing we have the using directive
15using
using System namespace MyCompany class
SimpleProgram static void Main()
Console.WriteLine(hi)
Now since we added the using System directive,
the compiler will use things in the System
namespace without needing to specifically be told
the namespace i.e. we can type Console.WriteLin
e(hi) Instead of System.Console.WriteLine(hi
)
16When you use dont confuse
using A using B namespace A class C
public static method void foo()
System.Console.WriteLine(sup)
namespace B class C public
static method void foo()
System.Console.WriteLine(homie)
\\class with Main defined below
This code wont compile. The using directive
basically puts everything inside a namespace into
the global namespace. Therefore if items in
different namespaces share the same name, using
both of them will introduce ambiguity and the
compiler will scream.
17C (.NET) Applications
- All .NET applications written in .NET compatible
languages have access to the .NET platform and
interoperate seamlessly - This is possible through the MSIL
- MSIL cannot execute directly therefore exe made
in .NET actually call the JITter - The JITter basically compiles the MSIL when a
function is called
18JIT aint it
- Install-time code generation an applications
MSIL can be completely compiled at install-time - EconoJit systems with little RAM can discard
compiled assemblies when low on memory