1 / 18

VS2010 C# programming – ‘C’ Basics 1

VS2010 C# programming – ‘C’ Basics 1. Topics: Variables and Data types Naming convention Assignments, operators and comments Converting data types Math functions Comparisons Random Date and Time. VS2010 C# programming – ‘C’ Basics 2. Data types – constants and variables

jory
Télécharger la présentation

VS2010 C# programming – ‘C’ Basics 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VS2010 C# programming – ‘C’ Basics 1 Topics: Variables and Data types Naming convention Assignments, operators and comments Converting data types Math functions Comparisons Random Date and Time From VS C# 2010 Programming, John Allwork

  2. VS2010 C# programming – ‘C’ Basics 2 Data types – constants and variables Basic data types: Integer, floating point, char, string, Boolean Integer: byte (0 to 255), int (-2147483648 to +2147483647), long (huge!) Floating point: float, double Characters: char, string Logical: bool Declaration: Datatype name ( = initialValue); From VS C# 2010 Programming, John Allwork

  3. VS2010 C# programming – ‘C’ Basics 3 Examples: int integerNumber; // not initialised double fpnumber = 34.5; // initialised float temperature = 98.6F; // use F suffix char charIn, charOut; bool lightSwitch = true; string strMessage = “Cancel”; const double ACONSTANT =3.14159; // we can declare more than one at a time const int MAX = 10, MIN = 1; // use a 0x for hexadecimal // ushort is 0 to 0xFFFF const ushort HEXNUMBER = 0xCAFE; From VS C# 2010 Programming, John Allwork

  4. VS2010 C# programming – ‘C’ Basics 4 Naming convention C# is case sensitive .NET convention on MS web site. Variables start with lowercase letter. Methods and Classes start with Capital letter Separate words with Uppercase letter, e.g: ‘upperCase’ is a variable, UpperCase is a method (convert to upper case). Useful if controls start with three letters indicating the control: lblTitle, btnStopStart, txtFirstName From VS C# 2010 Programming, John Allwork

  5. VS2010 C# programming – ‘C’ Basics 5 Assignments: ‘=‘ sign used, end with semicolon, e.g: variable = expression; sum = one + two; Comments use /* or //: /* Slash-star is a multi-line comment- end with a star-slash */ // a double slash which ignores the rest of the line Editor can comment out many lines in one go From VS C# 2010 Programming, John Allwork

  6. VS2010 C# programming – ‘C’ Basics 6 Operators: Usual operators: +, -, *, /, % Order of operation: !, and pointers Unary *, /, &,>>, << and % Multiplication +, -, |, ^ Addition ==, !=, >, <, <=, >= Relational If in doubt, use brackets From VS C# 2010 Programming, John Allwork

  7. VS2010 C# programming – ‘C’ Basics 7 Shortcuts Can replace X = X + 3; with X+=3; Useful for long names Note: it’s NOT X = +3; Other shortcuts: div = div / 4; becomes div / = 4; mult = mult * 5; becomes mult * = 5; sub = sub - 2; becomes sub - = 2; rem = rem%4; becomes rem % = 4; Increment i: i++; decrement i: i--; See C# operators in help From VS C# 2010 Programming, John Allwork

  8. VS2010 C# programming – ‘C’ Basics 8 Converting data types: numbers to strings (and back) integer to float (and back) implicit and explicit conversions: Implicit: conversion is OK double dbli=1.234; float fltj=1.234F; // note declaration of the float dbli = fltj; // implicit float to double is OK fltj = dbli; // but double to float errors // - requires explicit cast: fltj = (float) dbli; // (float) - you say it’s OK Explicit integer conversion – you say it’s OK : From VS C# 2010 Programming, John Allwork

  9. VS2010 C# programming – ‘C’ Basics 9 Converting data types: Convert or Parse? Convert.ToInt32(“strNumber”) and Int32.Parse(“strNumber”) Give identical results except when the string is a null. Int32.Parse(null) throws an ArgumentNullException; Convert.ToInt32(null) returns a zero. From VS C# 2010 Programming, John Allwork

  10. VS2010 C# programming – ‘C’ Basics 10 String conversions Characters and strings defined using: Examples: char chrCapitalA = 'A'; string strAString = "this is a string"; string strNumber = "1.234"; float fltNumber = float.Parse(strNumber); int intNumber = Int32.Parse(“12345”); From VS C# 2010 Programming, John Allwork

  11. VS2010 C# programming – ‘C’ Basics 11 Common Math functions: Name Description Sin, Cos, Tan - sine, cosine etc. of an angle (radians) Exp - e raised to the specified power. IEEERemainder - remainder of a division. Log - the logarithm of the number. Log10 - returns log to the base 10. Pow - the number raised to a specified power Round - rounds to nearest integer or decimalplaces. Sqrt - square root of the number. Truncate - the integral part of a number. No need to instantiate (create them) You just use From VS C# 2010 Programming, John Allwork

  12. VS2010 C# programming – ‘C’ Basics 12 Boolean logic operations: AND a = a & b; OR a= a | b; NOT a= !b; XOR a= a ^ b; BITWISE a = ~a; // inverts all bits Shift uses: << and >> e.g: int j = i >> 2; From VS C# 2010 Programming, John Allwork

  13. VS2010 C# programming – ‘C’ Basics 13 Relational (or comparison) – for testing Operator English Example == equal to (2+2) == 4 != not equal to (1+2) != 4 < less than 1 < 2 <= less than or equal to Y <= 3 > greater than 4 > 3 >= greater than or equal to (X+Y) >= Z && AND a && b || OR x || y From VS C# 2010 Programming, John Allwork

  14. VS2010 C# programming – ‘C’ Basics 14 Random An object – have to create one when you want to use it. Random randomNo = new Random( ); // create it int randomNumber; // declare integer to use randomNumber = randomNo.Next(5); // value 0 to 4 or randomNumber = randomNo.Next(5, 7); // value 5 or 6 or just: randomNumber = randomNo.Next( ); // random value Or floating-point float rndFloat = randomNo.Next(); Can now use the number From VS C# 2010 Programming, John Allwork

  15. VS2010 C# programming – ‘C’ Basics 15 Date and Time DateTime object / data type Declare and instantiate it: DateTime theDate = new DateTime(); Get the date and time theTime = DateTime.Now; Declare your own date and time: DateTime xmas2007 = new DateTime(2007, 12, 25); Convert to text: label1.Text = theDate.ToString( ); From VS C# 2010 Programming, John Allwork

  16. VS2010 C# programming – ‘C’ Basics 16 DateTime methods Add a timespan – a timespan is hours, minutes, seconds // Add one hour, two minutes and three seconds theDate = theDate + new TimeSpan(1, 2, 3); // Subtract twenty-five days theDate = theDate - new TimeSpan(25, 0, 0, 0); // or xmas2007.AddDays(7); // new year xmas2007.AddYears(1); // xmas2008 Can also add Milliseconds, Seconds, Months etc. From VS C# 2010 Programming, John Allwork

  17. VS2010 C# programming – ‘C’ Basics 17 Retrieving parts of DateTime int year = xmas2007.Year; // 2007 int month = xmas2007.Month; // gives 12 int day = xmas2007.Day; // gives 25 // also .Hour, .Minute, .Second, .Millisecond And you can compare dates: DateTime startDate = DateTime.Parse("30 Dec 2006"); DateTime endDate = DateTime.Parse("1 Jan 2007"); bool result = startDate > endDate; // result = false From VS C# 2010 Programming, John Allwork

  18. VS2010 C# programming – ‘C’ Basics 18 Summary Variables constants and data types: integer, floating point, char, string, boolean Naming convention Assignments and comments Operators Converting data types int to float etc, numbers to strings Math functions Logic Operations Comparisons Date and Time From VS C# 2010 Programming, John Allwork

More Related