SlideShare ist ein Scribd-Unternehmen logo
1 von 36
C# Program Structure
Anoop K. C.
anoop@baabte.com
www.facebook.com/anoopbaabte
twitter.com/anoop_baabte
in.linkedin.com/in/anoopbaabte/
+91 9746854752
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.
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
Namespaces
namespace SampleNamespace {
class SampleClass {
public void SampleMethod() {
System.Console.WriteLine("SampleMethod inside
SampleNamespace");
}
}
}
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.
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.
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();
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
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.
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.
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.
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.
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.
Structs - Example
•This example demonstrates struct initialization using both default and
parameterized constructors.
Structs – Example (continued…)
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.
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.
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
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
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.
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.
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.
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
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
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.
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
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};
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.
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.
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)
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.
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.
Delegates - Example
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
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
C# basics
 C# basics C# basics
C# basics
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Program control statements in c#
Program control statements in c#Program control statements in c#
Program control statements in c#
 
Array in c#
Array in c#Array in c#
Array in c#
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
 
LINQ
LINQLINQ
LINQ
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
C# in depth
C# in depthC# in depth
C# in depth
 
2 Object Oriented Programming
2 Object Oriented Programming2 Object Oriented Programming
2 Object Oriented Programming
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
.NET and C# Introduction
.NET and C# Introduction.NET and C# Introduction
.NET and C# Introduction
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
interface in c#
interface in c#interface in c#
interface in c#
 

Ähnlich wie C# program structure

Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad NicheTech Com. Solutions Pvt. Ltd.
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .NetGreg Sohl
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsMaryo Manjaruni
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and oodthan sare
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentSuresh Mohta
 

Ähnlich wie C# program structure (20)

Oops
OopsOops
Oops
 
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
My c++
My c++My c++
My c++
 
Classes2
Classes2Classes2
Classes2
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
C# concepts
C# conceptsC# concepts
C# concepts
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 

Mehr von baabtra.com - No. 1 supplier of quality freshers

Mehr von baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Kürzlich hochgeladen

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 

Kürzlich hochgeladen (20)

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
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
  • 5. Namespaces namespace SampleNamespace { class SampleClass { public void SampleMethod() { System.Console.WriteLine("SampleMethod inside SampleNamespace"); } } }
  • 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.
  • 16. Structs – Example (continued…)
  • 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