Phemmy Google Search

Tuesday, August 5, 2008

C# Flow Control

There are two methods of controlling program flow discussed in this section of our learning.
These are :
  1. Branching : This is where code are executed conditionally depending on the outcome of an evaluation such as "only execute this code if myVal is less than 10".
  2. Looping :This is repeatedly executing the same statement( for a certain number of times or until a test condition has been reached)
Boolean comparisons require the sue of Boolean comparison operators( also known as relational operators). In the examples below var1 is a bool type variable, while the type of var2 and var3 may vary.

1. == (var1=var2 ==var3;) var1 is true if var2 is equal to var3 or false otherwise.
2. != (var1=var2 != var3;) var1 is true if var2 is not equal to var3 or false otherwise.
3. < ( var1= var2 < var3;) var1 is true if var2 is less than var3 or false otherwise.
4. > ( var1= var2 > var3;) var1 is true if var2 is greater than var3 or false otherwise.
5. <= (var1 = var2 <= var3:) var1 is true if var2 is less or equal to var3 or false otherwise. 6. >= (var1 = var2>=var3;) var1 is true if var2 is greater or equal to var3 or false otherwise.

You might use operators such as these on numeric values in code such as :

bool isLessThan10;

isLessThan10=myVal < 10; This code result will be true if myVal store a value less than 10 or false otherwise. You can also use these compariosn operators on their types, such as strings: bool isKarli; isKarli =myString=="Karli"; In this example isKarli will only be true if myString stores the string "Karli"

There are some other Boolean operators that are intended specifically for working with Boolean values, shown below:
1. ! (var1 =! var2;) var1 is true if var2 is false or false if var2 is true (Logical NOT);
2. & (va1= var2 & var3;) var1 is true if var2 and var3 are both true or false otherwise (Logical AND);
3. | (var1= var2 | var3;) var1 is true if either var2 or var3 or both are true or false otherwise (Logical OR); and
4. ^ (var1 = var2 ^ var3;) var1 is true if either var2 or var3 but not both, are true or false otherwise (Logical XOR or exclusive OR).

The & and | operators also have two similar operators, known as conditional Boolean operators, as shown below:
1. && (var1 = var2 && var3;) var1 is true if var2 and var3 are both true or false otherwise (Logical AND) and

2. || (var1 = var2||var3;) Var1 is true if either var2 or var3 (or both) are true or false otherwise. The use of && and || aid better performance than & and | so always use && and || where possible.


Monday, August 4, 2008

C# Variable

All Data in a computer is effectively the same thing(a series of zeros & one). Variables are of different types. Using a box analogy to explain variables; one can assume variables in computer memory as boxes sitting on a shelf. One can imagine that your boxes come in different shapes & sizes and some things will only fit in certain boxes. According to this analogy different types of data may require different methods of manipulation.

To use variables you have to declare them. This means that you have to assign them a name and a type. If this is done the variable can be used as storage units for the type of data that you declare them to hold. C# syntax for declaration of variable and type is:
(type) (name);
If any variable is not declared your code will not compile. There are certain types of data that just about everyone will want to use at one time or the other such as a variable that stores a number. There are a number of simple, predefined types that one should be aware of:



It should be noted that string has no upper limit on the amount of characters because it can use varying amount of memory; while boolean type bool is one of the most commonly used variable type in C#.

Example of variable declaration

1. Create a new console Application

2. Add the following code to program.cs

Static void Main(string [] args)
int myInteger;
string myString;
myString= "\"my Integer\" is ";
Console. writeline ("{0} {1}.", mySrting, myInteger);

3. Execute the code

Variable Naming
The basic variable naming rules are:
  • The first character of a variable name must be either a letter or underscore(_), or the at(@) symbol.
  • Subsequent characters may be letters, underscore characters or numbers.
e.g. myBigVar

While these are wrong
99Bottlesof Beer

These are wrong because of some certain keywords that have a specialized meaning to the C# compiler such as namespace while others are number and case sensitivity.

Furthermore, one can have multiple variables whose names differ only in case, such as the ones below:

Naming Conventions
Over the years different systems have come and gone while some developers fought tooth and nail to justify their own system of naming.

Hungarian notation is recently known as the most popular system. This entails placing a lower case prefix on all variable names that identifies the type. For instance, if a variable were of type int then you might place an i (or n) in front of it such as in iAge. Adopting this system, it will be easy to see it at a glance what types different variables are.

Currently, there are two naming conventions in use in the .Net framework namespace; they are:

(a) PascalCase e.g.

(b) camelCase e.g.
For simple variable use camelCase but for advanced naming use PascalCase which is the Microsoft recommendation.

Variable Declaration & Assignment
Recall that declaration of variable entails using their type and name e.g.
int Age;
Then assign values to variable using (=) assignment operator

Declaring multiple variables of the same type at the same time can be done uing commas to separate the variables e.g.

int xSize, ySize;
i.e. xsize and ysize are both declared as integer types.

The second method you are likely to see in assigning values to variables at the same time as declaring them can be:

int age=25;
You can also use both methods together.

int xSize=4, ySize=5;


Saturday, August 2, 2008

Basic C# Syntax

C# code is similar to that of C++ and Java. Initially the syntax can look quite confusing and is lot less like written English than some other languages. C# compilers take no notice of additional spacing in code, whether made up spaces, carriage returns or tab characters. Meaning that one have a lot of freedom in the way that on will format code; though conforming to certain rules which can make the codes easier to read.

C# code is made up of a series of statements, each of which is terminated with semicolon(;), so you don't have multiple statements on one line, but for readability's sake it is usual to add carriage return after semicolons.

C# is a block-structured language, meaning that all statements are part of a block of code. These blocks, which are represented with curly brackets({ & }), may contain any number of statements or none at all. it should noted that the curly bracket does not require semicolons.

Example 1
code line1, statement1;
code line2, statement2;
code line3, statement3;

Another important thing often seen in C# code is comments. Comments are descriptive text to your codes in plain Language(English), which is ignored by the compiler.
The first method of signifying comment is using this symbol (*/)

Example 2

/* This is a comment*/
The second method is using //, with this approach you can write whatever you like provided you keep to one line!

Example 3
//This is a different sort of comment.

The next example show case how to comment can be used on the same line with a code;

Statement; // explanation of statement.

It is pertinent to not that C# code is case sensitive. Unlike some other languages as code must be entered using the right case because using wrong case will prevent a project from compiling.


Kind of Application you can Design with C#

C# uses the Framework and so also has no restrictions on possible applications. However, here are a few of the more common application types:

Windows Applications: These are applications, such as Microsoft Office, which have a familiar Windows look and feel about them. This is made simple by using the Windows Forms module of the .NET Framework, which is a library of controls (such as buttons, toolbars, menus, and so on) that you can use to build a Windows user interface (UI).

Web Applications: These are Web pages such as might be viewed through any Web browser. the .NET Framework includes a powerful system of generating Web content dynamically, allowing personalization, security, and much more. This system is called ASP.NET (Active Server Pages .NET), and you can use C# to create ASP.NET applications using Web Forms.

Web Services: These are a new and exciting way of creating versatile distributed applications. Using Web services you can exchange virtually any data over the Internet, using the same simple syntax regardless of the language used to create a Web service or the system that it resides on.

Any of these types may also require some form of database access, which can be achieved using the ADO.NET (Active Data Objects .NET) section of the .NET Framework. Many other resources can be drawn on, such as tools for creating networking components, outputting graphics, performing complex mathematical tasks, and so on.


Learning C# with me

What is C#?

C#, as mentioned earlier, is one of the languages that can be used to create applications that will run in the .NET CLR. It is an evolution of the C and C++ languages and has been created by Microsoft specifically to work with the .NET platform. Because it is a recent development, the C# language has been designed with hindsight, taking into account many of the best features from other languages, while clearing up their problems.

Developing applications using C# is simpler than using C++, because the language syntax is simpler. However, C# is a powerful language, and there is little you might want to do in C++ that you can't do in C#.
At times, C# code is slightly more verbose than C++. This is a consequence of C# being a type-safe language (unlike C++).C# is just one of the languages available for .NET development, but in my opinion it is certainly the best. It has the advantage of being the only language designed from the ground up for the .NET Framework and may be the principal language used in versions of .NET that are ported to other operating systems.

To keep languages such as the .NET version of Visual Basic as similar as possible to their predecessors yet compliant with the CLR, certain features of the .NET code library are not fully supported. By contrast, C# is able to make use of every feature that the .NET Framework code library has to offer. The latest version of .NET includes several improvements to the C# language, partly in response to requests from developers, making it even more powerful.