C# Programming: 3.0 Fundamentals II
C# Programming: 3.0 Fundamentals II
C# Programming: 3.0 Fundamentals II
C# Programming
By Purvis Samsoodeen
3.0 Fundamentals II
Class fundamentals
Instantiate an object
Method basics
Method parameters
Return a value from a method
Constructors
new
Garbage collection
Destructors
The this keyword
Page 1 of 10
C# Programming Purvis Samsoodeen
class classname {
// Declare instance variables.
access type var1;
access type var2;
// ...
access type varN;
// Declare methods.
access ret-type method1(parameters) {
// body of method
}
access ret-type method2(parameters) {
// body of method
}
// ...
access ret-type methodN(parameters) {
// body of method
}
}
Format output
Literals
C# literals can be of any of the value types. The way each literal is
represented depends upon its type. Character literals are enclosed
between single quotes. For example ‘a’ and ‘%’ are both character
literals.
Integer literals are specified as numbers without fractional
components. For example, 10 and –100 are integer literals.
Page 2 of 10
C# Programming Purvis Samsoodeen
Initialize variables
The scope rules of a method
The scope defined by a method begins with its opening curly brace
and ends with its closing curly brace. However, if that method has
parameters, they, too, are included within the scope defined by the
method.
As a general rule, local variables declared inside a scope is not
visible to code that is defined outside that scope. Thus, when you
declare a variable within a scope, you are preventing it from being
accessed or modified by code outside the scope. Indeed, the scope
rules provide the foundation for encapsulation.
Scopes can be nested. For example, each time you create a block of
code, you are creating a new, nested scope. When this occurs, the
outer scope encloses the inner scope. This means that local
variables declared in the outer scope will be visible to code within
the inner scope.
However, the reverse is not true. Local variables declared within
the inner scope will not be visible outside it.
using System;
class ScopeDemo {
static void Main() {
int x; // known to all code within Main()
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
Console.WriteLine("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
Console.WriteLine("x is " + x);
}
}
Page 3 of 10
C# Programming Purvis Samsoodeen
Expressions
Page 4 of 10
C# Programming Purvis Samsoodeen
Indexers
Properties
Page 6 of 10
C# Programming Purvis Samsoodeen
Object-oriented programs work the other way around. They are organized
around data, with the key principle being “data controlling access to
code.” In an object-oriented language, you define the data and the routines
that are permitted to act on that data. Thus, a data type defines precisely
what sort of operations can be applied to that data.
3.3.1 Abstraction
3.3.2 Polymorphism
3.3.3 Inheritance
Inheritance is the process by which one object can acquire the properties
of another object. This is important because it supports the concept of
hierarchical classification. If you think about it, most knowledge is made
manageable by hierarchical (that is, top-down) classifications.
For example, a Red Delicious apple is part of the classification apple,
which, in turn, is part of the fruit class, which is under the larger class
food. That is, the food class possesses certain qualities (edible, nutritious,
and so on) that also, logically, apply to its subclass, fruit. In addition to
these qualities, the fruit class has specific characteristics (juicy, sweet, and
so forth) that distinguish it from other food. The apple class defines those
qualities specific to an apple (grows on trees, not tropical, and so on). A
Red Delicious apple would, in turn, inherit all the qualities of all preceding
classes and would define only those qualities that make it unique.
Without the use of hierarchies, each object would have to explicitly define
all of its characteristics. Using inheritance, an object need only define
those qualities that make it unique within its class. It can inherit its general
attributes from its parent. Thus, it is the inheritance mechanism that makes
it possible for one object to be a specific instance of a more general case.
3.3.4 Encapsulation
Page 9 of 10
C# Programming Purvis Samsoodeen
C#’s basic unit of encapsulation is the class. A class defines the form of an
object. It specifies both the data and the code that will operate on that data.
C# uses a class specification to construct objects. Objects are instances of
a class. Thus, a class is essentially a set of plans that specify how to build
an object.
Page 10 of 10