25
MSP Monday Date: May 26, 2014

C Sharp: Basic to Intermediate Part 01

Embed Size (px)

Citation preview

Page 1: C Sharp: Basic to Intermediate Part 01

MSP Monday

Date: May 26, 2014

Page 2: C Sharp: Basic to Intermediate Part 01

C#

Basic to Intermediate

Part 1 Presented by Zafor Iqbal

[email protected]

Page 3: C Sharp: Basic to Intermediate Part 01

Target Audience

This Lecture has been prepared for the beginners who has basic understanding of object oriented programming or Java.

Page 4: C Sharp: Basic to Intermediate Part 01

Environment Setup

What do we need?

• A Windows PC

• Visual Studio 2010/2012/2013

• Lots of passion to code!

Page 5: C Sharp: Basic to Intermediate Part 01

Introduction

What is C Sharp(C#)?

C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg.

Anders Hejlsberg

Page 6: C Sharp: Basic to Intermediate Part 01

Basic Syntax

Hello World Example in C#

using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { /* my first program in C# */ Console.WriteLine("Hello World"); Console.ReadLine(); } } }

Page 7: C Sharp: Basic to Intermediate Part 01

Data Types

In C#, variables are categorized into the following types:

Value types; i.e. bool, byte, char, decimal,double etc. Reference types; Object type; i.e. object obj=50; Dynamic type; dynamic d= 400; String type; i.e. String str = “MSP Monday”; Pointer types: Pointer type variables store the memory address of another type.

Pointers in C# have the same capabilities as in C or C++. i.e. char* ch; int* inr;

Page 8: C Sharp: Basic to Intermediate Part 01

Type Conversion

Implicit type conversion - these conversions are performed by C# in a type-safe manner. Examples : conversions from smaller to larger integral types and conversions from derived classes to base classes.

Explicit type conversion - these conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.

Page 9: C Sharp: Basic to Intermediate Part 01

Type Conversion Examples

namespace TypeConversionApplication{ class ExplicitConversion { static void Main(string[] args){ double d = 5673.74; int i; i = (int)d; // cast double to int. Console.WriteLine(i); Console.ReadKey(); } } }

Page 10: C Sharp: Basic to Intermediate Part 01

Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. Syntax for variable definition in C# is: <data_type> <variable_name> = value; i.e. double pi = 3.14159;

Page 11: C Sharp: Basic to Intermediate Part 01

Constants and Literals

The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.

The constants are treated just like regular variables except that their values

cannot be modified after their definition.

Page 12: C Sharp: Basic to Intermediate Part 01

Example

using System; namespace DeclaringConstants{ class Program { static void Main(string[] args){ const double pi = 3.14159; // constant declaration double r; Console.WriteLine("Enter Radius: "); r = Convert.ToDouble(Console.ReadLine()); double areaCircle = pi * r * r; Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle); Console.ReadLine(); } } }

Page 13: C Sharp: Basic to Intermediate Part 01

Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C# is rich in built-in operators and provides the following type of operators: Arithmetic Operators; i.e. +,-,*,/,% etc. Relational Operators; i.e. ==,!=,>,<,>=,<= etc. Logical Operators; i.e. &&, ||, ! etc. Bitwise Operators; i.e. &,|,^ etc. Assignment Operators; i.e. =,+=.-=;*= etc. Misc Operators; i.e. sizeof(),typeof(),is,as etc.

Page 14: C Sharp: Basic to Intermediate Part 01

Conditional Statements

Page 15: C Sharp: Basic to Intermediate Part 01

LOOP

Page 16: C Sharp: Basic to Intermediate Part 01

The Infinite Loop?

using System; namespace Loops{ class Program{ static void Main(string[] args) { for ( ; ; ) { Console.WriteLine(“Yo, is this ever gonna end?"); } } } }

Page 17: C Sharp: Basic to Intermediate Part 01

Encapsulation

Encapsulation is defined 'as the process of enclosing one or more items

within a physical or logical package'. Encapsulation, in object oriented

programming methodology, prevents access to implementation details.

Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers:

Public Private Protected Internal Protected internal

Page 18: C Sharp: Basic to Intermediate Part 01

Encapsulation Public : Any public member can be accessed from outside the class.

Private : Private access specifier allows a class to hide its member variables and member functions

from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.

Protected : Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance.

Internal : Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.

Protected internal : The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance.

Page 19: C Sharp: Basic to Intermediate Part 01

Arrays

An array stores a fixed-size sequential collection of elements of the same

type. An array is used to store a collection of data, but it is often more useful

to think of an array as a collection of variables of the same type.

Initializing an Array double[] balance = new double[10]; Assigning Values to an Array double[] balance = new double[10]; balance[0] = 4500.0;

Page 20: C Sharp: Basic to Intermediate Part 01

String

In C#, you can use strings as array of characters, however, more common

practice is to use the string keyword to declare a string variable. The string

keyword is an alias for the System.String class.

Creating a String Object You can create string object using one of the following methods: By assigning a string literal to a String variable By using a String class constructor By using the string concatenation operator (+) By retrieving a property or calling a method that returns a string By calling a formatting method to convert a value or object to its string representation

Page 21: C Sharp: Basic to Intermediate Part 01

Example

using System; namespace StringApplication { class Program { static void Main(string[] args) { //from string literal and string concatenation string fname, lname; fname = “MSP"; lname = “Monday"; string fullname = fname + lname; Console.WriteLine("Full Name: {0}", fullname);

Page 22: C Sharp: Basic to Intermediate Part 01

Example Continued

//by using string constructor char[] letters = { 'H', 'e', 'l', 'l','o' }; string greetings = new string(letters); Console.WriteLine("Greetings: {0}", greetings); //methods returning string string[] sarray = { "Hello", "From", “MSP", “Monday" }; string message = String.Join(" ", sarray); Console.WriteLine("Message: {0}", message);

Page 23: C Sharp: Basic to Intermediate Part 01

Example Continued

//formatting method to convert a value DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1); string chat = String.Format("Message sent at {0:t} on {0:D}", waiting); Console.WriteLine("Message: {0}", chat); Console.ReadKey() ; } } }

Page 24: C Sharp: Basic to Intermediate Part 01

That’s it for today!

We will resume from next Session!

Thank you