Академический Документы
Профессиональный Документы
Культура Документы
NET Programming
Day 2 C# Basics
Objectives
Explain the need for C# Discuss flow of control of a C# program List the fundamental data types in C# Discuss the concepts of Boxing and UnBoxing Checked and unchecked keywords Ref, out, params keywords Discuss Structures and enumerators Discuss a simple C# program
Introduction to C#
C# compiler is the most efficient compiler in the .NET family Provides native support for COM (Component Object Model) Enhances developer productivity and increases safety, by enforcing strict type checking Allows restricted use of native pointers
C# Program Flow
A simple C# program
/* This is the Hello world Program in C# */ using System; class HelloWorldDemo { public static void Main() { Console.WriteLine (This is the Hello World program); } }
C# Constructs
Variables in C# are declared as follows AccessModifier DataType Variable
Public Private Protected int string float
C# Constructs Contd
To use a keyword as a variable name, prefix the variable name with a @ symbol.
using System; class VariableDemo { public static void Main() { string @string; @string; @string = string is a keyword but used as a variable name in this example; Console.WriteLine (@string); (@string); } }
C# Constructs Contd
using System; class DefaultValDemo { public static void Main() { int[] array1 = new int[5]; Console.WriteLine(10 multiplied by default value of second array element is {0} , 10 * array1[2]); } }
C# Data Types
C# Data type
object string int byte
Description
Base data type for all other types. Declares a variable that can store string values Declares a variable that can store integer values. Declares a variable that can store byte integer values.
Example
object o = null; string s = hello; int val = 12; byte val = 12;
float
Declares a variable, which can store real float val = 1.23F; number values that have integer and decimals parts Declares a variable, which can store logical values: true, false Declares a variable, which can store char values. bool val1 = true; bool val2 = false; char val = 'h';
bool char
Default Values
Default values of the common data types:
Type Numeric (int,float,short) Bool Char Enum 0 False \0 0 Default Value
Input / Output In C#
Uses methods of the Console class in the System namespace The most widely used methods are
Console.ReadLine() Console.WriteLine()
The if construct
if (expression) { //One or more statements to be executed if the expression evaluates to true } [else { //One or more statements to be executed if the expression evaluates to false }]
Selection Statement
string str = hello; if (str) System.Console.WriteLine (The value is True); if (str == hello) System.Console.WriteLine (The Value isThe above piece of code will display an error message True); Error CS0029 : Cannot implicitly convert type string' to 'bool'
Iteration Constructs
Perform a certain set of instructions for a certain number of times or while a specific condition is true Types of iteration constructs
The While Loop The Do Loop The For Loop The foreach Loop
The while loop iterates through the specified statements till the condition specified is true Syntax
The break statement - to break out of the loop at anytime The continue statement - to skip the current iteration and begin with the next iteration
The do loop
Syntax -
Fundamental Types Of C#
Value Types - int, char , and structures int, Reference Types - classes, interfaces, arrays and strings
Contains the address of the Reference Types object in the heap = null means that no object has been referenced
Value Types
using System; class DataTypeTest { public static void Main() { int variableVal = 100; funcTest(variableVal); Console.WriteLine(This value of the variable is {0}",variableVal); } static void funcTest (int variableVal) { int tempVar = 10; variableVal = tempVar*20; } }
Reference Types
using System; class DataTypeTest { public int variableVal; } class DataTypeTestRef { static void Main() { DataTypeTest dataTest = new DataTypeTest(); dataTest.variableVal = 100; funcDataTypeTest(dataTest); Console.WriteLine (dataTest.variableVal); }
Boxing is conversion of a value type into a reference type Unboxing is the conversion of a reference type into a value type
throws OverFlowException
ref: similar to passing reference type parameters in C/C++ Keyword ref must be used when calls functions Parameters passed with keyword ref must be initialized values first Using ref when call a function
out: like ref The difference is: dont need to initialize values before passing
Using out when call a function Declares out keyword in front of data type
3 elements
6 elements
Array
Keyword this
public class list { private int size; ... public SetSize (int size) { this.size = size; }
Data Types In C#
C# provides us with an Unified Type System All data types in C#, are derived from just one class, the object class
using System; class ObjectProff { public static void Main() { string objectVal; objectVal = 7.ToString(); Console.WriteLine (The value now is +objectVal); } }
Static Members
Members are not associated with any particular object or the class Only one instance of this member is possible
static int staticMem; static int instanceCount() { //instanceCount Implementation }
Arrays
A group of values of similar data type Belong to the reference type and hence are stored on the heap The declaration of arrays in C# follow the syntax given below -
int[6] array1;
Structures
Custom data Types Can have methods defined within them Cannot implement inheritance Represent Value types
struct structEx { public int structDataMember; public void structMethod1() { //structMethod1 Implementation } }
Enumerators
public class Holiday { public enum WeekDays { Monday, Tuesday, Wednesday, Thursday, Friday } public void GetWeekDays (String EmpName, WeekDays DayOff) { //Process WeekDays } static void Main() { Holiday myHoliday = new Holiday(); myHoliday.GetWeekDays (Richie, Holiday.WeekDays.Wednesday); } }
Enumerators Contd
Enumerators in C# have numbers associated with the values By default, the first element of the enum is assigned a value of 0 and is incremented for each subsequent enum element The default value can be overridden during initialization
Click 'Start
Start Page screen showing recently used projects is displayed on the screen
Click File New Project to start a new project Select 'Visual C# Projects' from the list on the right hand side and 'Console Application' from the list on the left hand side Type the name of the project in the 'Name' section as Example 1. In the Solution Explorer rename ConsoleApplication1 to Chapter1 Rename the project to Example1 and Class1.cs to HelloWorldDemo.cs
Save the file and Select Build Build Solution option to build the solution Select Start without Debugging option from the Debug menu to execute the application
Another method to compile and execute a program is using notepad and DOS prompt Step 1 Type your code in Notepad Step 2 Save the file with a .cs extension Step 3 Switch to DOS prompt and type the following command csc <filename>.cs To run the C# file, type the name of the file without the extension
Thank You
48 48