Advancing Engineering with AI through the Next Generation of Strategic Projec...
C# program structure
1.
2. C# Program Structure
Anoop K. C.
anoop@baabte.com
www.facebook.com/anoopbaabte
twitter.com/anoop_baabte
in.linkedin.com/in/anoopbaabte/
+91 9746854752
3. C# Files
C# programs can consist of one or more files. Each file can contain zero or more
namespaces. A namespace can contain types such as
Classes
Structs
Interfaces
Enumerations &
delegates.
4. Namespaces
• the .NET Framework uses namespaces to organize its many classes, for example
System.Console.WriteLine("Hello World!");
System is a namespace and Console is a class in that namespace.
The using keyword can be used so that the complete name is not required, as in
the following example
using System;
Console.WriteLine("Hello");
Console.WriteLine("World!");
declaring your own namespaces can help you control the scope of class and
method names in larger programming projects. Use the namespace keyword to
declare a namespace, as in the following example
6. Namespaces Overview
Namespaces have the following properties:
•They organize large code projects.
•They are delimited by using the . operator.
•The using directive obviates the requirement to specify the name of the
namespace for every class used while coding.
•The global namespace is the "root" namespace: global::System will always
refer to the .NET Framework namespace System.
7. Classes
A class is a construct that enables you to create your own custom types by
grouping together variables of other types, methods and events. A class is like a
blueprint. It defines the data and behavior of a type.
Classes are declared by using the class keyword, as shown in the following
example:
public class Customer {
//Fields, properties, methods and events go here...
}
The class keyword is preceded by the access level. Because public is used in this
case, anyone can create objects from this class. The name of the class follows
the class keyword. The remainder of the definition is the class body, where the
behavior and data are defined. Fields, properties, methods, and events on a class
are collectively referred to as class members.
8. Classes – Creating Objects
A class defines a type of object, but it is not an object itself. An object is a concrete
entity based on a class, and is sometimes referred to as an instance of a class.
Objects can be created by using the new keyword followed by the name of the
class that the object will be based on, like this:
Customer object1 = new Customer();
9. Classes – Inheritance
Inheritance is accomplished by using a derivation, which means a class is declared
by using a base class from which it inherits data and behavior. A base class is
specified by appending a colon and the name of the base class following the
derived class name, like this:
public class Manager : Employee {
// Employee fields, properties, methods and events are inherited
// New Manager fields, properties, methods and events go here...
}
When a class declares a base class, it inherits all the members of the base class
except the constructors.
In the above example all the methods and properties of the “Employee” class will
be available to the “Manager” class, without declaring them inside the manager
class
10. Classes – Example
In the following example, a public class that contains a single field, a method, and
a special method called a constructor is defined.
11. Structs
Structs are defined by using the struct keyword, for example:
public struct PostalAddress {
// Fields, properties, methods and events go here...
}
Structs share most of the same syntax as classes, but structs are more limited than
classes.
12. Structs - Overview
Within a struct declaration, fields cannot be initialized unless they are
declared as const or static.
A struct cannot declare a default constructor (a constructor without
parameters) or a destructor.
Structs are copied on assignment -When a struct is assigned to a new
variable, all the data is copied, and any modification to the new copy does not
change the data for the original copy. This is important to remember when
working with collections of value types such as Dictionary<string, myStruct>.
Structs are value types and classes are reference types.
Unlike classes, structs can be instantiated without using a new operator.
13. Structs - Overview
Structs can declare constructors that have parameters.
A struct cannot inherit from another struct or class, and it cannot be the
base of a class. All structs inherit directly from System.ValueType, which
inherits from System.Object.
A struct can implement interfaces.
A struct can be used as a nullable type and can be assigned a null value.
14. When to use structs over classes
The majority of types in a framework should be classes. There are, however, some
situations in which the characteristics of a value type make it more appropriate to
use structs.
√ CONSIDER defining a struct instead of a class if instances of the type are
small and commonly short-lived or are commonly embedded in other objects.
X AVOID defining a struct unless the type has all of the following
characteristics:
It logically represents a single value, similar to primitive types
(int, double, etc.).
It has an instance size under 16 bytes.
It is immutable.
It will not have to be boxed frequently.
In all other cases, you should define your types as classes.
15. Structs - Example
•This example demonstrates struct initialization using both default and
parameterized constructors.
17. Structs – Example (continued…)
The following example demonstrates a feature that is unique to structs. It creates
a CoOrds object without using the new operator. If you replace the
word struct with the word class, the program will not compile.
18. Interfaces
Interfaces describe a group of related functionalities that can belong to
any class or struct. You define an interface by using the interface keyword, as
shown in the following example.
interface IEquatable<T> {
bool Equals(T obj);
}
Interfaces consist of methods, properties, events, indexers, or any combination of
those four member types. An interface cannot contain constants, fields, operators,
instance constructors, destructors, or types. It cannot contain static members.
Interfaces members are automatically public, and they cannot include any access
modifiers.
19. Interfaces - continued
Interface methods can only have method declarations and no
implementation
Like classes an interface can also have properties, methods, delegates or
events but can only have declarations of these.
An interface can not have fields.
Classes and structs can inherit interfaces
If a class or struct inherits an interface it must provide implementation for
the members in the interface.
The implemented method should have the same signature as the interface
method
Classes or struct can inherit multiple interfaces simultaneously
20. Interfaces - continued
an instance of an interface can not be created, but an interface reference
variable can point to a derived class object.
interface naming convention : interface names are prefixed with “I”
Explicit interface implementation
Explicit interface implementation is needed when a class is inheriting
multiple interfaces with the same method name
When methods are implemented explicitly , no access modifiers are
allowed and the method name should be prefixed by “interfacename.”
when such methods are called, the class object reference variable has
to be type casted to the corresponding interface type
21. Interfaces - continued
When a class or struct implements an interface, the class or struct provides
an implementation for all of the members defined by the interface.
The interface itself provides no functionality that a class or struct can
inherit in the way that base class functionality can be inherited.
Classes and structs implement interfaces in a manner similar to how classes
inherit a base class or struct, with two exceptions:
A class or struct can implement more than one interface.
When a class or struct implements an interface, it receives only the method
names and signatures, because the interface itself contains no
implementations, as shown in the following example.
22. Interfaces - Example
The IEquatable<T> interface announces to the user of the object that the object can
determine whether it is equal to other objects of the same type, and the user of the
interface does not have to know how this is implemented.
23. Interfaces - Overview
An interface has the following properties:
An interface is like an abstract base class: any non-abstract type that implements
the interface must implement all its members.
An interface cannot be instantiated directly.
Interfaces can contain events, indexers, methods, and properties.
Interfaces contain no implementation of methods.
Classes and structs can implement more than one interface.
An interface itself can inherit from multiple interfaces.
24. Interfaces – when to use
By implementing an interface we define a specific set of functionality to a class
or struct
By inheriting an interface in a class, the class is forced to provide implementation
for all the methods in the interface.
For eg. If we have an interface “Imovable” which defines the methods for
movements of an object, and has 10 methods.
• suppose the classes “person” and “vehicle” implements “Imovable”,
since both can move.
•In this way “person” and “vehicle” classes are forced to provide
implementation for all the methods so that no movement method can be
left unimplemented in any of these classes
•Later if we wish to add a class “animal” which implements “Imovable”,
that will also be forced to provide implementation for all movement
methods
25. Abstract Classes
An abstract class can be declared with the abstract keyword
An object instance of an abstract class can not be created
Abstract classes can only be used as base classes for other classes
A class that inherits an abstract class must provide implementations for all the non-
implemented members in the base abstract class, or if this class does not wish to do so,
the derived class must be declared as an abstract class
An abstract class can not be sealed : we can not prevent other classes from inheriting an
abstract class
The members inside an abstract class may or may not have implementations
26. Abstract Classes Vs Interfaces
An abstract class can have implementation for some of its members whereas an interface
can not have implementation for any of its members
Interface members are public by default and they can not have access modifiers whereas
abstract class members can have access modifiers and they are private by default
Interfaces can not have fields whereas abstract classes can have fields
An interface can inherit only from another interface whereas an abstract class can inherit
from another abstract classes and interfaces
Interfaces define a common set of functionality that forces any class that implements it
to provide implementation for all the methods inside the interface whereas abstract
classes define a common set of traits for the classes that derive from it.
The classes derived from an interface has to provide implementation of all the methods in
the interface whereas the classes derived from an abstract class need not provide
implementation for all the methods in the abstract class.
27. When to use Abstract Classes
Abstract classes are useful when we have to create a multiple related classes those share
common members, but still have implementation of some members in a different way &
we only want the instances of these derived classes to be available for the developer
In such cases abstract classes act as a base class and reduce chances of error and enhance
code maintainability
Since the base class is abstract an instance of a base class can not be created
28. Enumerations
An enumeration is a distinct type that consists of a set of named constants called the
enumerator list.
The enum keyword is used to declare an enumeration. Usually it is best to define an enum
directly within a namespace so that all classes in the namespace can access it with equal
convenience. However, an enum can also be nested within a class or struct.
By default, the first enumerator has the value 0, and the value of each successive
enumerator is increased by 1. For example, in the following
enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
Enumerators can use initializers to override the default values, as shown in the following
example.
enum Days {Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri};
29. Why Enumerations
Enums make the program more readable and maintainable
for eg., if you have a class for customers and you want to track the gender of each
customer.
Without enums, you can create an integer property gender inside customer class and
assume that
if gender = 0, then ”unknown”
if gender = 1, then ”male”
if gender = 2, then ”female”
So if we want to refer the gender of a customer we will have to define like
cusomer.gender = 1;
And we will have to document somewhere else that if the gender is 1, customer is male.
Moreover, since gender is an integer, it can take any integer value and suppose we assign
10 to gender my mistake inside one of our functions, the gender of the customer will not
be defined which can lead to programming inconsistencies.
30. Why Enumerations - contind
In these kind of situations enums can be handy. it defines a property of type gender to the
customer class
Public enum gender {unknown, male, female}
So that the gender of the customer can be defined like
customer.gender = gender.male
Which makes the program more readable and the programmer can not define values other
than defined in the enum’s declaration to the gender property of a customer.
31. Delegates
Delegates are type safe function pointers – It refers to a function and when the
delegate is invoked the function will be called.
The “delegate” keyword is used to define a delegate. The syntax of a
delegate definition is very much similar to that of a method definition with
the only difference that the former has a “delegate” keyword.
To use a delegate an instance of the delegate has to be created in the same
manner as we create an instance of a class.
to make a delegate point to a function/method, the function/method has to
be passed as a parameter to the constructor of the delegate.
to point a delegate to a function, the function and the delegate should have
the same signature (return type, parameters and order in which parameters
are passed)
32. Delegates - contnd
Delegates can be passed as parameters to a function
Delegates help in decoupling the logic from a class to improve the
reusability and flexibility of a class.
33. Multicast Delegates
Multicast delegates are delegates that point to more than one function.
A multicast delegate invokes functions in the same order as the functions
are added to the delegate.
if the functions in a multicast delegate has a return type (non void
functions) the delegate will return the return value of the last function added
to the delegate.
in this way delegates can be used to define callback methods.
35. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
36. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com