Personal Web & Sample Site
Skip Navigation Links
HOME |
About Me |
Articles
| Contact Us
C Sharp Part One
Rating: 12 user(s) have rated this article Average rating: 5.0
Posted by: joet, on 9/5/2011, in category "C#"
Views: this article has been read 3484 times
Location: Houston, Tx, United States
Abstract: >Here I will be showing parts of the Microsoft C# language starting with the basics and then moving on to the more complex aspects of writing programs in C#. The C# language was designed to profit from the experiences of other programming languages. The basic concepts behind C# programming are apparent in even the simplest programs.

 

Here I will be showing parts of the Microsoft C# language starting with the basics and then moving on to the more complex aspects of writing programs in C#

Keywords I'll be using:

  • Identifiers
  • Variables
  • Operators
  • Operands
  • Expressions
  • Statements
  • Namespace
  • Methods

What is C#

The C# language was designed to profit from the experiences of other programming languages. The basic concepts behind C# programming are apparent in even the simplest programs. Essentially, a C# program can be thought of as an onion with a bunch of layers (see Figure 1-1).

C# Onion

In C# programming, you have code inside methods, which are inside classes, which are inside namespaces. Dig it!

In the .NET environment(of which C# is a primary language) are layers of code that go from general to specific. The outer, most general, layer is the namespace. Inside a namespace, you find a series of classes, which contain methods, which contain statements.

WOW! Talk about a simplified view!

What is a C# method?

In college "Introduction to Programming" we studied procedural programming with psuedo code using primitive variables, operators, operands, and expressions. This was confusing at first but once we starting to write complete statements in our functions or subroutines, it became more clear. C# has methods that are often compared to a subroutine but used a bit differently. Methods are named sequences of statements that are made up of one or more expressions and are a fundemental but powerful mechanism in C#. A statement is made up of one or more expressions that consist of variables, operators, and operands. The only way to learn programming is to actually write programs, period! Want an example?

string lastName; (Declare variable)(value type)as lastName

 myName = "Jose M." + lastName (Complete statement)(concantenated)

  • lastName is a Variable used in the expression as a operand
  • myName is another Variable initialized to the value in the expression
  • The = sign is the assignment operator
  • The literal string "Jose M." is an operand
  • The plus sign is the Operator (Arithmetic, Conditional, Compound)
  • The "Jose M." + lastName" is the Expression

Worth mentioning is their are more than one operator type and depending on the data type you'll need to know how to use it. For example the + operator can be used as an arithmitic operator or it can be used to concatenate string values.

Another Example:

static void Main(string[] args)
{
   Console.WriteLine("I would like for you to learn more about methods");
   Console.ReadLine();
}
}
In this example the word "Console" is a class and using dot notation the word "WriteLine" is a member of the Console class that happens to  be a method of that class.

Scope

I haven't mentioned scope and won't other than saying you can declare a variable locally within a method or applied at the class scope.

A method can or will always have return type such void, that doesn't return a value or a return statement. A method name used as the identifier, a parameter list, and the method body statements.

Method Overload

This is very important and will be used when we start adding our class implementations and overloading our methods. If two methods are declared in the same scope they are said to be overloaded. The WriteLine method of the Console class is one of many method implementations and you can get the entire list in Visual Studio intellisense. But if you use the WriteLine method and pass in an int data type argument or you use it again within the same scope and then pass in a literal string data type you are performing the same operation except on different data types. It can also be the same data type but a different number of arguments. This is method overloading.

Refactoring Code

This is when you starting writing the same code in different places in your application. This was one of the improvements in MOSS2007 where the Onet.xml file was refactored to one page. Visual Studio has support for this by clicking the Refactor menu and clicking Extract Method. Highlight the code you know is redundant and click Extract Method. It creates the method and then replaces the redundant code with a call to this method.

Ok, I know that I didn't get into detail about the C# syntax a whole lot but I'm going over things that are important for me to remember and keep in mind. I also haven't gone into desicion making with loops like if, cascading if's, the switch, while, , for, or do loops. I will write examples of these later.


How would you rate this article?

User Feedback

Post your comment
Name:
E-mail:
Comment:
Insert Cancel

Copyright © 2013 Jose M. Tamez
Last Updated August 18th 2013