The C# Language<br /><ul><li>C# Language Basics
Variables and Data Types
Array, ArrayList, Enumerations
Operator & Math Functions
Type Conversions
The DateTime and TimeSpan Types
Conditional Logic
Loops
Methods & Method Overloading
Parameters (Optional & Named)
Delegates</li></li></ul><li>          C# Language Basics<br /><ul><li>Case Sensitivity
Commenting</li></ul>// A single-line C# comment.<br />/* A multiple-line<br />C# comment. */<br /><ul><li>Statement Termin...
Blocks</li></ul>{<br />// Code statements go here.<br />}<br /><ul><li>Declaration, Assignment &Initializers</li></ul>inte...
Variables and Data Types<br />
Variables and Data Types<br />
Escaped Characters<br />• " (double quote)<br />• n (new line)<br />• t (horizontal tab)<br />•  (backward slash)<br />// ...
Arrays<br />Arrays allow you to store a series of values that have the same data type.<br />// Create an array with four s...
ArrayList<br />ArrayListdynamicList = new ArrayList();<br />// Add several strings to the list.<br />The ArrayList is not ...
Enumeration<br />An enumeration is a group of related constants, each of which is given a descriptive name. Each value in ...
Operators & Math functions<br />+, -, *, / ,% are basic operators<br />To use the math operations, you invoke the methods ...
Type Conversions<br />Converting information from one data type to another<br />Conversions are of two types: widening and...
Type Conversions<br />Converting information from one data type to another<br />String myString;<br />intmyInteger=100;<br...
String Functions<br />Length() :                                       myString.Length()<br />ToUpper(), ToLower()		myStri...
Upcoming SlideShare
Loading in …5
×

Chapter 2

555 views

Published on

C# the Language

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
555
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 2

  1. 1. The C# Language<br /><ul><li>C# Language Basics
  2. 2. Variables and Data Types
  3. 3. Array, ArrayList, Enumerations
  4. 4. Operator & Math Functions
  5. 5. Type Conversions
  6. 6. The DateTime and TimeSpan Types
  7. 7. Conditional Logic
  8. 8. Loops
  9. 9. Methods & Method Overloading
  10. 10. Parameters (Optional & Named)
  11. 11. Delegates</li></li></ul><li> C# Language Basics<br /><ul><li>Case Sensitivity
  12. 12. Commenting</li></ul>// A single-line C# comment.<br />/* A multiple-line<br />C# comment. */<br /><ul><li>Statement Termination
  13. 13. Blocks</li></ul>{<br />// Code statements go here.<br />}<br /><ul><li>Declaration, Assignment &Initializers</li></ul>interrorCode;<br />string myName;<br />
  14. 14. Variables and Data Types<br />
  15. 15. Variables and Data Types<br />
  16. 16. Escaped Characters<br />• " (double quote)<br />• n (new line)<br />• t (horizontal tab)<br />• (backward slash)<br />// A C# variable holding the<br />// c:MyAppMyFiles path.<br />path = "c:MyAppMyFiles";<br />Alternatively, you can turn off C# escaping by preceding a string with an @ symbol, as shown here:<br />path = @"c:MyAppMyFiles";<br />
  17. 17. Arrays<br />Arrays allow you to store a series of values that have the same data type.<br />// Create an array with four strings (from index 0 to index 3).<br />// You need to initialize the array with the new keyword in order to use it.<br />string[] stringArray = new string[4];<br />// Create a 2x4 grid array (with a total of eight integers).<br />int[,] intArray = new int[2, 4];<br />// Create an array with four strings, one for each number from 1 to 4.<br />string[] stringArray = {"1", "2", "3", "4"};<br />// Create a 4x2 array (a grid with four rows and two columns).<br />int[,] intArray = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};<br />
  18. 18. ArrayList<br />ArrayListdynamicList = new ArrayList();<br />// Add several strings to the list.<br />The ArrayList is not strongly typed, so you can add any data type<br />dynamicList.Add("one");<br />dynamicList.Add(2);<br />dynamicList.Add(true);<br />// Retrieve the first string. Notice that the object must be converted to a<br />// string, because there's no way for .NET to be certain what it is.<br />string item = Convert.ToString(dynamicList[0]);<br />
  19. 19. Enumeration<br />An enumeration is a group of related constants, each of which is given a descriptive name. Each value in an enumeration corresponds to a preset integer.<br />enumUserType<br />{<br />Admin,<br />Guest,<br />Invalid<br />}<br />
  20. 20. Operators & Math functions<br />+, -, *, / ,% are basic operators<br />To use the math operations, you invoke the methods of the System.Math class. <br />myValue = Math.Round(42.889, 2); // myValue = 42.89<br />myValue = Math.Abs(-10); // myValue = 10.0<br />myValue = Math.Log(24.212); // myValue = 3.18.. (and so on)<br />myValue = Math.PI; // myValue = 3.14.. (and so on)<br />
  21. 21. Type Conversions<br />Converting information from one data type to another<br />Conversions are of two types: widening and narrowing. Widening conversions always succeed. <br />For example, you can always convert a 32-bit integer into a 64-bit integer.<br />intmySmallValue;<br />long myLargeValue;<br />mySmallValue= Int32.MaxValue;<br />myLargeValue = mySmallValue;<br />Or <br />mySmallValue = (int)myLargeValue;<br />intmyInt=1000;<br />short count;<br />count=(short)myInt;<br />
  22. 22. Type Conversions<br />Converting information from one data type to another<br />String myString;<br />intmyInteger=100;<br />myString=myInteger.ToString();<br />String countString=“10”;<br />int count=Convert.ToInt32(countString);<br />or<br />int count=Int32.Parse(countString);<br />
  23. 23. String Functions<br />Length() : myString.Length()<br />ToUpper(), ToLower() myString.ToUpper() <br />Trim() myString.Trim()<br />Substring() myString.SubString(0,2)<br />StartsWith(), EndsWith() myString.StartsWith(“pre”)<br />PadLeft(), PadRight() myString.PadLeft(5,”*”)<br />Insert() myString.Insert(1,”pre)<br />Remove() myString.Remove(0,2)<br />Split() myString.Split(“,”)<br />Join() myString1.Join(myString2)<br />Replace() myString.Replace(“a”,”b”)<br />
  24. 24. DateTime and TimeSpan Types<br />DateTime and Timespane data types have built-in methods and properties<br />Methods & Properties of DateTime :<br />Now<br />Today<br />Year, Date, Month ,Hour, Minute, Second, and Millisecond <br />Add() and Subtract()<br />AddYears(), AddMonths(), AddDays(), AddHours, AddMinutes()<br />DaysIn Month()<br />Methods and Properties of TimeSpan:<br />Days, Hours, Minutes, Seconds, Milliseconds<br />TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds<br />Add() and Subtract()<br />FromDays(), FromHours(), From Minutes(), FromSeconds()<br />
  25. 25. DateTime and TimeSpan Types<br />DateTimemyDate = DateTime.Now;<br />myDate = myDate.AddDays(100);<br />DateTime myDate2 = DateTime.Now.AddHours(3000);<br />TimeSpan difference;<br />difference = myDate2.Subtract(myDate1);<br />double numberOfMinutes;<br />numberOfMinutes = difference.TotalMinutes;<br />// Adding a TimeSpan to a DateTime creates a new DateTime.<br />DateTime myDate1 = DateTime.Now;<br />TimeSpan interval = TimeSpan.FromHours(3000);<br />DateTime myDate2 = myDate1 + interval;<br />// Subtracting one DateTime object from another produces a TimeSpan.<br />TimeSpan difference;<br />difference = myDate2 - myDate1;<br />
  26. 26. Conditional Logic<br />All conditional logic starts with a condition: a simple expression that can be evaluated to true or false. Your code can then make a decision to execute different logic depending on the outcome of the condition. <br />To build a condition, you can use any combination of literal values or variables along with logical operators <br />== Equal to<br />!= Not Equal to <br />< Less than<br />> Greater than<br /><= Less than or equal to<br />>= Greater than or equal to<br />&& Logical and<br />|| Logical or<br />
  27. 27. Conditional Logic<br />The switch Statement<br />switch (myNumber)<br />{<br />case 1:<br />// Do something.<br />break;<br />case 2:<br />// Do something.<br />break;<br />default:<br />// Do something.<br />break;<br />}<br />The if Statement<br />if (myNumber > 10)<br />{<br />// Do something.<br />}<br />else if (myString == "hello")<br />{<br />// Do something.<br />}<br />else<br />{<br />// Do something<br />
  28. 28. Loops<br />Loops allow you to repeat a segment of code multiple times. C# has three basic types of loops<br />• You can loop a set number of times with a for loop.<br />• You can loop through all the items in a collection of data using a foreachloop.<br />• You can loop while a certain condition holds true with a while or do…while loop.<br />THE FOR LOOP<br />string[] stringArray = {"one", "two", "three"};<br />for (int i = 0; i < stringArray.Length; i++)<br />{<br />System.Diagnostics.Debug.Write(stringArray[i] );<br />}<br />THE FOREACH LOOP<br />foreach (string element in stringArray)<br />{<br />System.Diagnostics.Debug.Write(element );<br />}<br />
  29. 29. Loops<br />THE WHILE LOOP<br />inti = 0;<br />while (i < 10)<br />{<br />i += 1;<br />// This code executes ten times.<br />}<br />You can also place the condition at the end of the loop using the do…while syntax. In this case, the<br />condition is tested at the end of each pass through the loop:<br />THE DO WHILE LOOP<br />inti = 0;<br />do<br />{<br />i += 1;<br />// This code executes ten times.<br />}<br />while (i < 10);<br />
  30. 30. Methods<br />A method also known as function is a named grouping of one or more lines of code. Each method will perform a distinct, logical task.<br />By breaking your code down into methods, you not only simplify your life, but you also make it easier to organize your code into classes<br />// This method doesn't return any information.<br />void MyMethodNoReturnedData()<br />{<br />// Code goes here.<br />}<br />// This method returns an integer.<br />intMyMethodReturnedData()<br />{<br />// As an example, return the number 10.<br />return 10;<br />}<br />
  31. 31. Parameter<br />Optional Parameter<br />private string GetUserName(int ID, booluseShortForm = false)<br />{ // Code here.<br />}<br />name = GetUserName(401, true);<br />name = GetUserName(401);<br />Use Named parameter for multiple optional parameters:<br />private decimal GetSalesTotalForRegion(intregionID, decimal minSale = 0,<br />decimal maxSale = Decimal.MaxValue, boolincludeTax = false)<br />{ // Code here.<br />}<br />total = GetSalesTotalForRegion(523, maxSale: 5000);<br />
  32. 32. Delegates<br />Delegates allow you to create a variable that “points” to a method. <br />private string TranslateEnglishToFrench();<br />{<br />}<br />private delegate string translateLanguage(string inputString);<br />translateLanguage translate;<br />translate=TranslateEnglishToFrench;<br />string frenchString;<br />frenchString=translate(“Hello”);<br />You can make delegate point to any other function also if it has same signature<br />
  33. 33. Delegates<br />private string TranslateEnglishToSpanish();<br />{<br />}<br />translate=TranslateEnglishToSpanish;<br />string spanishString;<br />spanishString=translate(“Hello”);<br />private string TranslateEnglishToGerman();<br />{<br />}<br />translate=TranslateEnglishToGerman;<br />string germanString;<br />germanString=translate(“Hello”);<br />
  34. 34. Summary<br />It’s impossible to do justice to an entire language in a single chapter. However, if you’ve programmed before, you’ll find that this chapter provides all the information you need to get started with the C# language. As you work through the full ASP.NET examples in the following chapters, you can refer to this chapter to clear up any language issues.<br />In the next chapter, you’ll learn about more important language concepts and the object-oriented nature of .NET.<br />

×