This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
The C# Language• 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
C# Language Basics• Case Sensitivity• Commenting
// A single-line C# comment./* A multiple-lineC# comment. */
// Retrieve the first string. Notice that the object must be converted to a// string, because there's no way for .NET to be certain what it is.
string item = Convert.ToString(dynamicList[0]);
Enumeration
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.
enum UserType{Admin,Guest,Invalid}
Operators & Math functions+, -, *, / ,% are basic operators
To use the math operations, you invoke the methods of the System.Math class.
Type ConversionsConverting information from one data type to anotherConversions are of two types: widening and narrowing. Widening conversions always succeed. For example, you can always convert a 32-bit integer into a 64-bit integer.int mySmallValue;long myLargeValue;
DateTime and TimeSpan TypesDateTime and Timespane data types have built-in methods and properties
Methods & Properties of DateTime :NowTodayYear, Date, Month ,Hour, Minute, Second, and Millisecond Add() and Subtract()AddYears(), AddMonths(), AddDays(), AddHours, AddMinutes()DaysIn Month()
Methods and Properties of TimeSpan:Days, Hours, Minutes, Seconds, MillisecondsTotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMillisecondsAdd() and Subtract()FromDays(), FromHours(), From Minutes(), FromSeconds()
// Adding a TimeSpan to a DateTime creates a new DateTime.DateTime myDate1 = DateTime.Now;TimeSpan interval = TimeSpan.FromHours(3000);DateTime myDate2 = myDate1 + interval;// Subtracting one DateTime object from another produces a TimeSpan.TimeSpan difference;difference = myDate2 - myDate1;
Conditional Logic
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. To build a condition, you can use any combination of literal values or variables along with logical operators
== Equal to!= Not Equal to< Less than> Greater than<= Less than or equal to>= Greater than or equal to&& Logical and|| Logical or
Conditional Logic
The if Statement
if (myNumber > 10){// Do something.}else if (myString == "hello"){// Do something.}else{// Do something
The switch Statement
switch (myNumber){case 1:// Do something.break;case 2:// Do something.break;default:// Do something.break;}
LoopsLoops allow you to repeat a segment of code multiple times. C# has three basic types of loops• You can loop a set number of times with a for loop.• You can loop through all the items in a collection of
data using a foreach loop.• You can loop while a certain condition holds true
with a while or do…while loop.
THE FOR LOOPstring[] stringArray = {"one", "two", "three"};
for (int i = 0; i < stringArray.Length; i++){System.Diagnostics.Debug.Write(stringArray[i] );}THE FOREACH LOOPforeach (string element in stringArray){System.Diagnostics.Debug.Write(element );}
LoopsTHE WHILE LOOPint i = 0;while (i < 10){i += 1;// This code executes ten times.}
You can also place the condition at the end of the loop using the do…while syntax. In this case, thecondition is tested at the end of each pass through the loop:THE DO WHILE LOOPint i = 0;do{i += 1;// This code executes ten times.}while (i < 10);
MethodsA method also known as function is a named grouping of one or more lines of code. Each method will perform a distinct, logical task.
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// This method doesn't return any information.void MyMethodNoReturnedData(){// Code goes here.}// This method returns an integer.int MyMethodReturnedData(){// As an example, return the number 10.return 10;}
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.
In the next chapter, you’ll learn about more important language concepts and the object-oriented nature of .NET.