C# Introduction
C# Introduction
INTRODUCTION TO .NET
FRAMEWORK
The Big Ideas
Overview of C#
Program structure
Basic Syntax
• Traditional views
• C++, Java: Primitive types are “magic” and do not
interoperate with objects
• Smalltalk, Lisp: Primitive types are objects, but at great
performance cost
• C# unifies with no performance cost
• Deep simplicity throughout system
• Improved extensibility and reusability
• New primitive types: Decimal, SQL…
• Collections, etc., work for all types
C# – The Big Ideas
Robust and durable software
• Garbage collection
• No memory leaks and stray pointers
• Exceptions
• Error handling is not an afterthought
• Type-safety
• No uninitialized variables, unsafe casts
• Versioning
• Pervasive versioning considerations in all aspects of language
design
C# – The Big Ideas
Preservation of Investment
• C++ heritage
• Namespaces, enums, unsigned types, pointers (in unsafe
code), etc.
• No unnecessary sacrifices
• Interoperability
• What software is increasingly about
• MS C# implementation talks to XML, SOAP, COM, DLLs,
and any .NET language
• Millions of lines of C# code in .NET
• Short learning curve
• Increased productivity
.NET Framework
• Language Compilers (e.g. C#, VB.NET, J#) will convert the Code/Program to Microsoft
Intermediate Language (MSIL) intern this will be converted to Native Code by CLR.
.NET Framework Class Library (FCL)
• The Framework Class Library is a huge library of reusable types meant to be used
by the managed codes.
• .NET Framework consists of classes, interfaces and value types that help in
speeding up the development process and provide access to system functionality.
C#.NET
• Object oriented programing language
• C# enables development of applications that runs on the .NET Framework.
• Developed by Microsoft
• C# 8
• C# can be used to create:-
1. Windows client applications,
2. XML Web services,
3. distributed components,
4. client-server applications,
5. database applications etc.
C# - Program Structure
A C# program consists of the following parts −
• Namespace declaration
• Specified by the Using Directive
• A class
• Class methods
• Class attributes
• A Main method
• Statements and Expressions
• Comments
• Inline/Single line comments - indicated by two forward slashes //
• Multiline/Block comments – marked by /*…….*/
Program Structure
Namespaces
• Namespaces provide a way to uniquely identify a type
• Provides logical organization of types
• Namespaces can span assemblies
• Can nest namespaces
• There is no relationship between namespaces and file structure
(unlike Java)
• The fully qualified name of a type includes all namespaces
Program Structure
Namespaces
namespace N1 { // N1
class C1 { // N1.C1
class C2 { // N1.C1.C2
}
}
namespace N2 { // N1.N2
class C2 { // N1.N2.C2
}
}
}
Program Structure
Namespaces
• The using statement lets you use types without typing the fully
qualified name
• Can always use a fully qualified name
using N1;
C2 c; // Error! C2 is undefined
N1.N2.C2 d; // One of the C2 classes
C1.C2 e; // The other one
Program Structure
Namespaces
• The using statement also lets you create aliases
using C1 = N1.N2.C1;
using N2 = N1.N2;
C1 a; // Refers to N1.N2.C1
N2.C1 b; // Refers to N1.N2.C1
Program Structure
Namespaces
• Best practice: Put all of your types in a unique namespace
• Have a namespace for your company, project, product, etc.
• Look at how the .NET Framework classes are organized
Program Structure
References
• In Visual Studio you specify references for a project
• Each reference identifies a specific assembly
• Passed as reference (/r or /reference) to the C# compiler
namespace HelloWorldApplication {
class HelloWorld {
static void Main(string[] args) {
/* my first program in C# */
Console.WriteLine("Hello HCE&HSE Programmers");
Console.Read();
}
}
Using Visual Studio.NET
• Types of projects
• Console Application
• Windows Application
• Web Application
• Web Service
• Windows Service
• Class Library
• ...
Using Visual Studio.NET
• Windows
• Solution Explorer
• Class View
• Properties
• Output
• Task List
• Object Browser
• Server Explorer
• Toolbox
Using Visual Studio.NET
• Building
• Debugging
• Break points
• References
• Saving
Using .NET Framework SDK
• Compiling from command line