Skip to main content

Understanding C# Language


Objective

In this tutorial, you will understand some basic components in C# Language include:

  • Types in C# Language
  • Variables and Constants
  • Identifier and Keywords
  • Statements and Expression
  • Class

Let's go

In a C# program, we will usually see some basic components like variable, expression, keywords, class. Let's start with all of them to investigate what they are and how to use these components.

Types

In C#, Types describe type of values. Any time you want to use a value, you need target a type. More detail, a type defines the allowed values and operations supported by those values.
Types in C# are divided into value types and reference types. Value types describe values that are completely self-contained and include numeric types, enumerated, and structures. Reference types, however, store a reference to a value rather than the actual value.
C# provides many predefined value types and a few predefined reference types. It also enables you to create your own user-defined types.The difference between value type and reference type is that a value type contains the actual value, whereas a reference type contains a reference to the actual data.


Variables and Constants

Variables are commonly used in program. It define a container for a value.
To define a variable, you must provide a type, an identifier, and, optionally, an initial value:

int a;
int b = 1;

If you are declaring multiple variables of the same type, you can combine the declarations, as follow:

int a, b;

Constants are immutable values which are known at compile time and do not change for the life of the program. Constants are declared with the 'const' modifier. For example:

public const int months = 12;


Operators

Operators are symbols that define how expression works. The following table shows a set of operators used in the C# language.

Category Symbol
Sign operators + -
Arithmetic + - * / %
Logical (boolean and bitwise) & | ^ ! ~ && || true false
String concatenation +
Increment, decrement ++ --
Shift >
Relational == != =
Assignment = += -= *= /= %= &= |= ^= >=
Member access .
Indexing []
Cast ()
Ternary ?:
Delegate concatenation and removal + -
Object creation new
Type information as is sizeof typeof
Overflow exception control checked unchecked
Indirection and address * -> [] &
Lambda =>

We will see how to use some operators in the example later.


Statements and Expressions

A statement is simply a single, complete program instruction that must end with a semicolon (;). You can use a single statement or you can group some statements to create a block of statement. For example of Statement is:

Console.WriteLine("Hello World !");//write a string “Hello World !” to Console

An expression evaluates to a value. An expression may be include statement, operator, variables, or nested expression. For example:

x = a + b


Class

A class define characteristic of a group in the context of program. For example, we want to create two object of triangle, so we define a class named Triangle, and then declare two instances of Triangle class. Here a triangle object is an instance of a class.
A class encapsulates all data, attributes and operations (methods) in it. In most of case, Class only exposes some of public methods, not all methods and data to secure and make it “more opaque to the world”.

Combine Things Together: A Program for understanding

Bellow is the source code that combines all things from start of this tutorial.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace MyFirstProgram
  7. {
  8. /*
  9.   * Class for defining some numeric operation
  10.   *
  11.   */
  12.  
  13. class NumericOperation
  14. {
  15. /*
  16.   * this function compares 2 numbers and print the result to console.
  17.   *
  18.   */
  19.  
  20. public void compare2Number(int a, int b)
  21. {
  22. if (a > b)//use expression a > b to check where a is greater than b
  23. {
  24. Console.WriteLine(a + " is greater than " + b);//write result to console
  25. }
  26. else
  27. {
  28. if (a == b)//expression a = b is used to check equality
  29. {
  30. Console.WriteLine(a + " is smaller than " + b);//write result to console
  31. }
  32. else
  33. {
  34. Console.WriteLine(a + " is smaller than " + b);//write result to console
  35.  
  36. }
  37. }
  38. }
  39. }
  40. }

Explanation

Here we define a class name NumericOperation that model the Operation of two number.

This program will do compare the value of two numbers and print the result to the console. Note that we use expression like “a > b”, “a = b” to check. We also use operator “ + “ to make input for the statement Console.WriteLine (Here is expression “ a + ‘is greater than ‘ + b “).

Summary

In this tutorial, you have learnt some basic (but important) components and definitions of C# language With these basic knowledge, you can write some simple C# programs and test. To write a more complicated program, you need to investigate more, but for now writing a simple program let you understand how C# language works.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.