intro to c#
intro to c#
• Goals of C#
• Brief History
• Hello World
• Syntax
• Features
• Type System
Goals of C#
• The classes and data types are common to all of the .NET languages.
1. SIMPLE
2. MODERN
3. OBJECT ORIENTED
4.TYPE SAFE
5.INTEROPERABILITY
6.SCALABLE AND UPDATEABLE
CONCLUSION
• C# is a modern, type safe programming language, object
oriented language that enables programmers to quickly and
easily build solutions for the Microsoft .NET platform
Hello World !!!
using System;
class Hello
{
static void Main()
{
Console.WriteLine(“Hello, World!”);
}
}
Hello World !!!
Class Declaration:
• “Class Hello “ declares a class, which is an object-oriented
construct.
• Since C# is an OOP Language, Everything must be placed
inside a class.
• A class is like a template for what an object looks like and
how it behaves.
• C# is a block-structured language which means code blocks
are always enclosed by braces { and }.
Hello World !!!
The Output:
System.Console.WriteLine(“Hello, World!”);
• Since C# is pure OOP Language, every method should be
part of an object.
• The WriteLine method is a static method of the Console
class, which is located in the namespace called System.
Syntax
• Case-sensitive
• Every Statement should end with a semicolon.
• Curly braces {} enclose code blocks
• Whitespace has no meaning
– Sequences of space, tab, linefeed, carriage return
• Comments:
– /* comment */
– // comment
Executing the Program
Hello.class
Hello.java javac Hello.java java Hello
Hello.exe
Hello.cs csc Hello.cs Hello
Name Spaces in C#
namespace first_space
{
class abc {
public void func()
{
Console.WriteLine("Inside first_space");
}
}
}
namespace second_space
{
class efg
{
public void func()
{
Console.WriteLine("Inside second_space");
}
}
}
class TestClass
{
static void Main(string[] args)
{
abc fc = new abc();
efg sc = new efg();
fc.func();
sc.func();
Console.ReadKey();
}
}
using System;
using first_space;
using first_space.second_space;
namespace first_space {
class abc {
public void func() {
Console.WriteLine("Inside first_space");
}
}
namespace second_space {
class efg {
public void func() {
Console.WriteLine("Inside second_space");
}
}
}
}
class TestClass {
static void Main(string[] args) {
abc fc = new abc();
efg sc = new efg();
fc.func();
sc.func();
Console.ReadKey();
}
}
When the above code is compiled and executed, it
produces the following result −
Inside first_space
Inside second_space
using System;
using first_space;
using nested.second_space;
namespace first_space {
class abc {
public void func() {
Console.WriteLine("Inside first_space");
}
}
}
namespace nested{
namespace second_space {
class efg {
public void func() {
Console.WriteLine("Inside second_space");
}
}
}
Command Line Arguments
a = double.Parse(Console.ReadLine());
Or
a = Convert.ToDouble(Console.ReadLine());
b = Math.Sqrt(a);
Console.WriteLine
Reference
Value Types Types
User-
Predefined User- Predefined Defined
Types Defined Types Types
Types
• Value Types are of fixed length and are stored on the stack
and when a value of a variable is assigned to another
variable, the value is actually copied.
• Value types of C# can be grouped into two as user-defined
and predefined.
• User-Defined are called complex types which include
struct and enumerations.
• Predefined are called simple types are further subdivided
into Numeric, Boolean and Character types.
• Numeric type includes integral, floating point and decimal
types.
Integral Types
type var1,var2,…………..var n;
Initialization
switch(expression)
{
case value-1:
code block-1
break;
case value-2:
code block-2
break;
…………
default:
default-block
break;
}
Conditional Operator
For Example
if(x<0)
flag=0;
else
flag=1;
Can be written as
flag = (x<0) ? 0 : 1
Looping Statements
• Example:
int[] myArray = {1,2,3,4,5};
foreach(int m in myArray)
{
Console.WriteLine(m);
}
Handling Arrays
• To Declare an Array
type [] arrayname;
e.g. int[] myArray;
– Polymorphism
– Inheritance
– Encapsulation
• It’s the concept we use to build new classes using the existing
class definitions.
• Through inheritance we can modify a class the way we want to
create new objects.
• The original class is known as base or parent class and the
modified one is known as derived class or subclass or child
class.
• The concept of inheritance facilitates the reusability of
existing code and thus improves the integrity of programs and
productivity of programmers.
Types of Inheritance
• Single Inheritance. A B
• Multilevel Inheritance.
A B C
(Derived from a derived class)
A
• Multiple Inheritance. C
– Inclusion Polymorphism
- Using Virtual Methods
Exception Handling
• Missing Semicolons
• Missing or mismatch of Brackets in classes and
methods.
• Misspelling of identifiers and keywords.
• Missing double quotes in strings.
• Use of undeclared variables.
• Incompatible types in assignments / initialization.
• Bad references to objects.
• Use of = in the place of == , etc..
Run Time errors
• Phase I –Mark.
• Phase II – Compact.
Phase – I Mark
• Moves all the live objects to the bottom of the heap, leaving free
space at the top.
• It includes,
– GC walks through the heap linearly, looking for contiguous
blocks of garbage objects.
– It then shifts the non-garbage objects down in memory,
removing all of the gaps in the heap.
– Moving objects in memory, invalidates all pointers to the
objects, so GC modifies the apps root so that the pointers
point to the objects new location.
– In addition, if any object contains a pointer to another object,
the GC is responsible for correcting these pointers as well.
Delegates
With an example:
#define DEBUG
#define VC_V7
using System;
public class MyClass
{ public static void Main()
{
#if (DEBUG && !VC_V7)
Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && VC_V7)
Console.WriteLine("VC_V7 is defined");
#elif (DEBUG && VC_V7)
Console.WriteLine("DEBUG and VC_V7 are defined");
#else
Console.WriteLine("DEBUG and VC_V7 are not defined");
#endif
}
}
Writing Unsafe code using C#
using System;
class MyClass
{
public static void Main()
{
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Address is " (int)pData);
}
Will Produce the following message:
} error CS0214: Pointers may only be used in an unsafe context
Unsafe Code Example
Indicates that
using System; the code block is
class MyClass unsafe
{
public unsafe static void Main()
{
int iData = 10;
int* pData = &iData;
Console.WriteLine("Data is " + iData);
Console.WriteLine("Address is " (int)pData);
} Will Produce the following message:
} Data is 10
Address is 1244316