0% found this document useful (0 votes)
42 views

Enterprise Application Development

Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Enterprise Application Development

Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

‭Overview of Enterprise Application Development‬

‭ nterprise application development refers to the process of creating software applications‬


E
‭tailored to the needs of large organizations or businesses. These applications are designed‬
‭to integrate multiple facets of a company's operations, ensuring efficiency, scalability, and‬
‭robustness. Below is an overview of the key aspects of enterprise application development:‬

‭1. Characteristics of Enterprise Application‬

‭●‬ ‭Scalability:‬‭Ability to handle an increasing number‬‭of users and transactions.‬

‭●‬ I‭ntegration:‬‭Seamless interaction with various systems‬‭and software within the‬


‭organization.‬

‭●‬ S
‭ ecurity:‬‭Robust security measures to protect sensitive‬‭data and ensure compliance‬
‭with regulations.‬

‭●‬ ‭Reliability:‬‭High availability and fault tolerance‬‭to minimize downtime.‬

‭●‬ ‭Performance:‬‭Fast response times and efficient resource‬‭utilization.‬

‭●‬ ‭Usability:‬‭User-friendly interfaces to ensure productivity.‬

‭2. Types of Enterprise Applications‬

‭●‬ E
‭ nterprise Resource Planning (ERP):‬‭Integrates core‬‭business processes like‬
‭finance, HR, manufacturing, and supply chain management.‬

‭●‬ C
‭ ustomer Relationship Management (CRM):‬‭Manages a‬‭company's interactions‬
‭with current and potential customers.‬

‭●‬ S
‭ upply Chain Management (SCM):‬‭Oversees the flow of‬‭goods, information, and‬
‭finances from supplier to customer.‬

‭●‬ H
‭ uman Resource Management (HRM):‬‭Handles employee‬‭data, payroll,‬
‭recruitment, and performance management.‬

‭●‬ ‭Business Intelligence (BI):‬‭Analyzes data to support‬‭decision-making processes.‬

‭●‬ C
‭ ontent Management Systems (CMS):‬‭Manages digital‬‭content creation and‬
‭storage.‬

‭3. Development Methodologies‬

‭●‬ W
‭ aterfall‬‭: A linear approach where each phase of development‬‭must be completed‬
‭before moving on to the next.‬
‭●‬ A ‭ gile‬‭: An iterative and incremental approach that emphasizes flexibility and‬
‭customer feedback.‬
‭●‬ ‭DevOps‬‭: Combines development and operations to improve‬‭collaboration and‬
‭productivity through automation and continuous delivery.‬

‭4. Technologies and Tools‬

‭‬ P
● ‭ rogramming Languages‬‭: Java, C#, Python, JavaScript,‬‭etc.‬
‭●‬ ‭Frameworks‬‭: Spring (Java), .NET (C#), Django (Python),‬‭Angular/React‬
‭(JavaScript).‬
‭●‬ ‭Databases‬‭: SQL (MySQL, PostgreSQL), NoSQL (MongoDB,‬‭Cassandra).‬
‭●‬ ‭Cloud Platforms‬‭: AWS, Microsoft Azure, Google Cloud‬‭Platform.‬
‭●‬ ‭Development Tools‬‭: Integrated Development Environments‬‭(IDEs) like Eclipse,‬
‭Visual Studio; version control systems like Git.‬

‭5. Architecture Patterns‬

‭‬ M
● ‭ onolithic Architecture‬‭: A single, unified codebase.‬
‭●‬ ‭Service-Oriented Architecture (SOA)‬‭: Divides functionality‬‭into services that can be‬
‭accessed over a network.‬
‭●‬ ‭Microservices Architecture‬‭: Breaks down applications‬‭into small, loosely coupled‬
‭services.‬
‭●‬ ‭Event-Driven Architecture‬‭: Uses events to trigger‬‭and communicate between‬
‭services.‬

‭6. Best Practices‬

‭‬
● ‭ equirement Analysis‬‭: Thorough understanding of business‬‭needs and goals.‬
R
‭●‬ ‭Modular Design‬‭: Creating reusable and independent‬‭modules.‬
‭●‬ ‭Automated Testing‬‭: Ensuring software quality through‬‭continuous testing.‬
‭●‬ ‭Continuous Integration/Continuous Deployment (CI/CD)‬‭:‬‭Automating the‬
‭software release process.‬
‭ ‬ ‭Documentation‬‭: Maintaining clear and comprehensive‬‭documentation.‬

‭7. Challenges‬

‭●‬ C ‭ omplexity‬‭: Managing the complexity of large systems‬‭with multiple‬


‭interdependencies.‬
‭●‬ ‭Change Management‬‭: Adapting to changing business requirements‬‭and‬
‭technologies.‬
‭●‬ ‭Data Security and Privacy‬‭: Ensuring compliance with‬‭regulations like GDPR,‬
‭HIPAA.‬
‭●‬ ‭Performance Optimization‬‭: Balancing performance with‬‭resource utilization.‬

‭8. Future Trends‬

‭●‬ A
‭ rtificial Intelligence and Machine Learning‬‭: Enhancing‬‭decision-making and‬
‭automation.‬
‭‬ B
● ‭ lockchain‬‭: Providing secure and transparent transaction processing.‬
‭●‬ ‭Internet of Things (IoT)‬‭: Integrating physical devices‬‭with enterprise systems.‬
‭●‬ ‭Edge Computing‬‭: Processing data closer to its source‬‭to reduce latency.‬

‭Conclusion‬

‭ nterprise application development is a critical aspect of modern business operations,‬


E
‭enabling organizations to streamline processes, improve efficiency, and maintain a‬
‭competitive edge. By leveraging the latest technologies, methodologies, and best practices,‬
‭businesses can develop robust, scalable, and secure applications tailored to their specific‬
‭needs.‬

‭Introduction to .NET Framework‬

‭ he‬ ‭.NET‬ ‭Framework‬ ‭is‬ ‭a‬ ‭software‬ ‭development‬ ‭framework‬ ‭developed‬ ‭by‬ ‭Microsoft‬ ‭that‬
T
‭provides‬ ‭a‬ ‭runtime‬ ‭environment‬ ‭and‬ ‭a‬ ‭set‬ ‭of‬ ‭libraries‬ ‭and‬ ‭tools‬ ‭for‬ ‭building‬ ‭and‬ ‭running‬
‭applications‬ ‭on‬ ‭Windows‬ ‭operating‬ ‭systems.‬ ‭The‬ ‭framework‬ ‭includes‬ ‭a‬ ‭variety‬ ‭of‬
‭programming‬ ‭languages,‬ ‭such‬ ‭as‬ ‭C#,‬ ‭F#,‬ ‭and‬ ‭Visual‬ ‭Basic,‬ ‭and‬ ‭supports‬ ‭a‬ ‭range‬ ‭of‬
‭application types, including desktop, web, mobile, and gaming applications.‬

‭1.‬ T‭ he‬ ‭.NET‬ ‭Framework‬ ‭includes‬ ‭two‬ ‭main‬ ‭components:‬ ‭the‬ ‭Common‬ ‭Language‬
‭Runtime‬ ‭(CLR)‬ ‭and‬ ‭the‬ ‭.NET‬ ‭Framework‬‭Class‬‭Library.‬‭The‬‭CLR‬‭is‬‭responsible‬‭for‬
‭managing‬‭the‬‭execution‬‭of‬‭code‬‭written‬‭in‬‭any‬‭of‬‭the‬‭supported‬‭languages,‬‭while‬‭the‬
‭class‬‭library‬‭provides‬‭a‬‭large‬‭set‬‭of‬‭pre-built‬‭functions‬‭and‬‭classes‬‭that‬‭can‬‭be‬‭used‬
‭to create a wide range of applications.‬

‭2.‬ O‭ ne‬ ‭of‬ ‭the‬ ‭key‬ ‭advantages‬ ‭of‬ ‭the‬ ‭.NET‬ ‭Framework‬ ‭is‬ ‭its‬ ‭support‬ ‭for‬ ‭a‬ ‭variety‬ ‭of‬
‭programming‬‭languages.‬‭This‬‭means‬‭that‬‭developers‬‭can‬‭choose‬‭the‬‭language‬‭that‬
‭best‬ ‭fits‬ ‭their‬ ‭needs‬ ‭and‬ ‭expertise,‬ ‭while‬ ‭still‬ ‭being‬ ‭able‬ ‭to‬ ‭use‬ ‭the‬ ‭same‬ ‭set‬ ‭of‬
‭libraries and tools provided by the framework.‬

‭3.‬ A‭ nother‬ ‭advantage‬ ‭of‬ ‭the‬‭.NET‬‭Framework‬‭is‬‭its‬‭support‬‭for‬‭a‬‭variety‬‭of‬‭application‬


‭types.‬ ‭The‬‭framework‬‭includes‬‭libraries‬‭and‬‭tools‬‭for‬‭creating‬‭desktop,‬‭web,‬‭mobile,‬
‭and‬‭gaming‬‭applications,‬‭which‬‭makes‬‭it‬‭a‬‭versatile‬‭choice‬‭for‬‭developers‬‭working‬‭on‬
‭a wide range of projects.‬

‭4.‬ T‭ he‬ ‭.NET‬ ‭Framework‬ ‭also‬ ‭provides‬ ‭a‬ ‭number‬ ‭of‬ ‭features‬ ‭that‬ ‭help‬ ‭improve‬ ‭the‬
‭security,‬ ‭reliability,‬ ‭and‬‭performance‬‭of‬‭applications.‬‭These‬‭include‬‭features‬‭such‬‭as‬
‭code‬ ‭access‬ ‭security,‬ ‭automatic‬ ‭memory‬ ‭management,‬ ‭and‬ ‭just-in-time‬ ‭(JIT)‬
‭compilation, which helps improve the speed of application execution.‬

‭5.‬ T‭ he‬‭.NET‬‭Framework‬‭is‬‭also‬‭designed‬‭to‬‭integrate‬‭with‬‭other‬‭Microsoft‬‭technologies,‬
‭such‬‭as‬‭Microsoft‬‭SQL‬‭Server,‬‭Microsoft‬‭SharePoint,‬‭and‬‭Microsoft‬‭Office,‬‭which‬‭can‬
‭make‬ ‭it‬ ‭easier‬ ‭to‬ ‭build‬ ‭applications‬ ‭that‬ ‭work‬ ‭seamlessly‬ ‭with‬ ‭other‬ ‭Microsoft‬
‭products.‬
‭ verall,‬‭the‬‭.NET‬‭Framework‬‭is‬‭a‬‭powerful‬‭and‬‭versatile‬‭development‬‭platform‬‭that‬‭provides‬
O
‭a‬ ‭wide‬ ‭range‬ ‭of‬ ‭tools‬ ‭and‬ ‭libraries‬ ‭for‬ ‭building‬ ‭and‬ ‭running‬ ‭applications‬ ‭on‬ ‭Windows‬
‭operating systems.‬

.‭NET‬‭is‬‭a‬‭software‬‭framework‬‭that‬‭is‬‭designed‬‭and‬‭developed‬‭by‬‭Microsoft.‬‭The‬‭first‬‭version‬
‭of‬ ‭the‬ ‭.Net‬ ‭framework‬ ‭was‬ ‭1.0‬ ‭which‬ ‭came‬ ‭in‬ ‭the‬ ‭year‬ ‭2002.‬ ‭In‬ ‭easy‬ ‭words,‬‭it‬‭is‬‭a‬‭virtual‬
‭machine‬ ‭for‬ ‭compiling‬ ‭and‬ ‭executing‬ ‭programs‬ ‭written‬ ‭in‬ ‭different‬ ‭languages‬ ‭like‬ ‭C#,‬
‭VB.Net, etc.‬

I‭t‬ ‭is‬ ‭used‬‭to‬‭develop‬‭Form-based‬‭applications,‬‭Web-based‬‭applications,‬‭and‬‭Web‬‭services.‬


‭There‬‭is‬‭a‬‭variety‬‭of‬‭programming‬‭languages‬‭available‬‭on‬‭the‬‭.Net‬‭platform,‬‭VB.Net‬‭and‬‭C#‬
‭being‬‭the‬‭most‬‭common‬‭ones.‬‭It‬‭is‬‭used‬‭to‬‭build‬‭applications‬‭for‬‭Windows,‬‭phones,‬‭web,‬‭etc.‬
‭It provides a lot of functionalities and also supports industry standards.‬

.‭NET‬‭Framework‬‭supports‬‭more‬‭than‬‭60‬‭programming‬‭languages‬‭of‬‭which‬‭11‬‭programming‬
‭languages‬ ‭are‬ ‭designed‬ ‭and‬ ‭developed‬ ‭by‬ ‭Microsoft.‬ ‭The‬ ‭remaining‬ ‭Non-Microsoft‬
‭Languages are supported by .NET Framework but not designed and developed by Microsoft.‬

‭There are three significant phases of the development of .NET technology.‬

‭ .‬ O
1 ‭ LE Technology‬
‭2.‬ ‭COM Technology‬
‭3.‬ ‭.NET Technology‬

‭ LE‬ ‭Technology:‬ ‭OLE‬ ‭(Object‬ ‭Linking‬ ‭and‬ ‭Embedding)‬ ‭is‬ ‭one‬ ‭of‬ ‭the‬ ‭technologies‬ ‭of‬
O
‭Microsoft’s‬ ‭component‬ ‭document.‬ ‭Basically,‬ ‭its‬ ‭main‬ ‭purpose‬ ‭is‬ ‭to‬ ‭link‬ ‭elements‬ ‭from‬
‭different applications with each other.‬

‭ OM‬‭Technology:‬‭The‬‭technology‬‭of‬‭the‬‭Microsoft‬‭Windows‬‭family‬‭of‬‭the‬‭operating‬‭system,‬
C
‭Microsoft‬ ‭COM‬ ‭(Common‬ ‭Object‬ ‭Model)‬ ‭enables‬ ‭various‬ ‭software‬ ‭components‬ ‭to‬
‭communicate.‬‭COM‬‭is‬‭mostly‬‭used‬‭by‬‭developers‬‭for‬‭various‬‭purposes‬‭like‬‭creating‬‭reusable‬
‭ oftware‬ ‭components,‬ ‭linking‬ ‭components‬ ‭together‬ ‭to‬ ‭build‬ ‭applications,‬ ‭and‬ ‭also‬ ‭taking‬
s
‭advantage‬ ‭of‬ ‭Windows‬ ‭services.‬ ‭The‬ ‭objects‬ ‭of‬‭COM‬‭can‬‭be‬‭created‬‭with‬‭a‬‭wide‬‭range‬‭of‬
‭programming languages.‬

.‭NET‬‭Technology:‬‭.NET‬‭technology‬‭of‬‭collection‬‭or‬‭set‬‭of‬‭technologies‬‭to‬‭develop‬‭windows‬
‭and‬‭web‬‭applications.‬‭The‬‭technology‬‭of‬‭.Net‬‭is‬‭developed‬‭by‬‭Microsoft‬‭and‬‭was‬‭launched‬‭in‬
‭Feb.‬ ‭2002,‬ ‭by‬ ‭basic‬ ‭definition,‬ ‭Microsoft’s‬ ‭new‬ ‭Internet‬ ‭Strategy.‬ ‭It‬ ‭was‬ ‭originally‬ ‭called‬
‭NGWS‬ ‭(Next‬ ‭Generation‬ ‭Web‬ ‭Services).‬ ‭It‬ ‭is‬ ‭considered‬ ‭to‬ ‭be‬ ‭one‬ ‭of‬ ‭the‬ ‭most‬ ‭powerful,‬
‭popular, and very useful Internet Technology available today.‬

‭11 Programming Languages which are designed and developed by Microsoft are:‬

‭ .‬ ‭C#.NET‬
1
‭2.‬ ‭VB.NET‬
‭3.‬ ‭C++.NET‬
‭4.‬ ‭J#.NET‬
‭5.‬ ‭F#.NET‬
‭6.‬ ‭JSCRIPT.NET‬
‭7.‬ ‭WINDOWS POWERSHELL‬
‭8.‬ ‭IRON RUBY‬
‭9.‬ ‭IRON PYTHON‬
‭10.‬‭C OMEGA‬
‭11.‬‭ASML(Abstract State Machine Language)‬

‭Main Components of .NET Framework‬

‭ ommon‬ ‭Language‬ ‭Runtime(CLR):‬ ‭CLR‬ ‭is‬ ‭the‬ ‭basic‬ ‭and‬ ‭Virtual‬ ‭Machine‬‭component‬‭of‬
C
‭the‬ ‭.NET‬ ‭Framework.‬ ‭It‬ ‭is‬ ‭the‬ ‭run-time‬ ‭environment‬ ‭in‬ ‭the‬ ‭.NET‬ ‭Framework‬ ‭that‬ ‭runs‬ ‭the‬
‭code‬ ‭and‬ ‭helps‬ ‭in‬ ‭making‬ ‭the‬ ‭development‬ ‭process‬ ‭easier‬ ‭by‬ ‭providing‬ ‭various‬ ‭services‬
‭such‬‭as‬‭remoting,‬‭thread‬‭management,‬‭type‬‭safety,‬‭memory‬‭management,‬‭robustness,‬‭etc.‬
‭Basically,‬ ‭it‬ ‭is‬ ‭responsible‬ ‭for‬ ‭managing‬‭the‬‭execution‬‭of‬‭.NET‬‭programs‬‭regardless‬‭of‬‭any‬
‭.NET‬‭programming‬‭language.‬‭It‬‭also‬‭helps‬‭in‬‭the‬‭management‬‭of‬‭code,‬‭as‬‭code‬‭that‬‭targets‬
‭the‬ ‭runtime‬ ‭is‬ ‭known‬ ‭as‬‭Managed‬‭Code,‬‭and‬‭code‬‭that‬‭doesn’t‬‭target‬‭runtime‬‭is‬‭known‬‭as‬
‭Unmanaged code.‬

‭ ramework‬ ‭Class‬ ‭Library(FCL):‬ ‭It‬ ‭is‬ ‭the‬ ‭collection‬ ‭of‬ ‭reusable,‬ ‭object-oriented‬ ‭class‬
F
‭libraries‬‭and‬‭methods,‬‭etc‬‭that‬‭can‬‭be‬‭integrated‬‭with‬‭CLR.‬‭Also‬‭called‬‭the‬‭Assemblies.‬‭It‬‭is‬
‭just‬ ‭like‬ ‭the‬ ‭header‬ ‭files‬ ‭in‬ ‭C/C++‬ ‭and‬ ‭packages‬ ‭in‬ ‭java.‬ ‭Installing‬ ‭the‬ ‭.NET‬ ‭framework‬
‭basically‬ ‭is‬ ‭the‬ ‭installation‬ ‭of‬ ‭CLR‬ ‭and‬ ‭FCL‬ ‭into‬ ‭the‬ ‭system.‬ ‭Below‬ ‭is‬ ‭the‬ ‭overview‬‭of‬‭the‬
‭.NET Framework.‬
‭ elease‬ ‭History‬ ‭of‬ ‭.NET‬ ‭Framework‬ ‭and‬ ‭its‬‭compatibility‬‭with‬‭the‬‭different‬‭Windows‬
R
‭version‬
‭.Net Framework Architecture‬
‭ he‬‭architecture‬‭of‬‭the‬‭.Net‬‭framework‬‭focuses‬‭on‬‭two‬‭main‬‭things:‬‭The‬‭first‬‭of‬‭these‬‭is‬‭that‬
T
‭the‬ ‭framework‬ ‭code‬ ‭is‬ ‭largely‬ ‭independent‬ ‭of‬ ‭the‬ ‭language‬ ‭in‬ ‭which‬ ‭it‬ ‭is‬ ‭written.‬ ‭For‬
‭example,‬‭a‬‭framework‬‭written‬‭in‬‭C#‬‭can‬‭contain‬‭calls‬‭to‬‭libraries‬‭written‬‭in‬‭C++,‬‭as‬‭well‬‭as‬‭to‬
‭libraries‬‭written‬‭in‬‭other‬‭programming‬‭languages.‬‭This‬‭allows‬‭.Net‬‭programs‬‭to‬‭be‬‭ported‬‭to‬
‭a‬‭wide‬‭variety‬‭of‬‭platforms.‬‭The‬‭second‬‭main‬‭goal‬‭of‬‭the‬‭.Net‬‭framework‬‭is‬‭that‬‭it‬‭should‬‭be‬
‭easy‬ ‭to‬ ‭adopt.‬ ‭This‬ ‭means‬ ‭that‬ ‭the‬ ‭framework‬ ‭should‬ ‭be‬ ‭designed‬ ‭in‬ ‭a‬‭way‬‭that‬‭makes‬‭it‬
‭easy‬ ‭to‬ ‭incorporate‬ ‭new‬ ‭features‬ ‭and‬ ‭new‬ ‭functionality‬ ‭into‬ ‭an‬ ‭existing‬ ‭program.‬ ‭The‬
‭framework‬ ‭should‬ ‭also‬ ‭be‬ ‭designed‬ ‭in‬ ‭a‬ ‭way‬ ‭that‬ ‭makes‬ ‭it‬ ‭easy‬ ‭to‬ ‭port‬ ‭the‬ ‭program‬ ‭to‬‭a‬
‭different‬ ‭platform‬ ‭or‬ ‭make‬ ‭the‬ ‭program‬ ‭run‬ ‭on‬ ‭a‬ ‭different‬ ‭type‬ ‭of‬ ‭computer.‬ ‭The‬ ‭.Net‬
‭framework‬‭has‬‭been‬‭built‬‭in‬‭a‬‭way‬‭that‬‭makes‬‭it‬‭easy‬‭for‬‭programmers‬‭to‬‭build‬‭new‬‭features‬
‭into‬‭their‬‭programs.‬‭It‬‭has‬‭also‬‭been‬‭built‬‭in‬‭a‬‭way‬‭that‬‭makes‬‭it‬‭easy‬‭to‬‭extend‬‭the‬‭program‬
‭with new features.‬

‭How does the .NET framework work?‬

‭●‬ .‭NET‬‭framework-based‬‭applications‬‭that‬‭are‬‭written‬‭in‬‭supportive‬‭languages‬‭like‬‭C#,‬
‭F#, or Visual basic are compiled to Common Intermediate Language (CIL).‬
‭●‬ C ‭ ompiled‬ ‭code‬ ‭is‬ ‭stored‬ ‭in‬ ‭the‬ ‭form‬ ‭of‬ ‭an‬ ‭assembly‬ ‭file‬ ‭that‬ ‭has‬ ‭a‬ ‭.dll‬ ‭or‬ ‭.exe‬ ‭file‬
‭extension.‬
‭●‬ ‭When‬ ‭the‬ ‭.NET‬ ‭application‬ ‭runs,‬ ‭Common‬ ‭Language‬ ‭Runtime‬ ‭(CLR)‬ ‭takes‬ ‭the‬
‭assembly‬ ‭file‬ ‭and‬ ‭converts‬ ‭the‬ ‭CIL‬ ‭into‬ ‭machine‬ ‭code‬ ‭with‬ ‭the‬ ‭help‬ ‭of‬ ‭the‬ ‭Just‬ ‭In‬
‭Time(JIT) compiler.‬
‭●‬ ‭Now,‬‭this‬‭machine‬‭code‬‭can‬‭execute‬‭on‬‭the‬‭specific‬‭architecture‬‭of‬‭the‬‭computer‬‭it‬‭is‬
‭running on.‬

‭Q Managed code" and "Unmanaged code"‬

I‭n .NET, the terms "managed code" and "unmanaged code" refer to how memory‬
‭management and other low-level details are handled by the runtime.‬

‭Managed Code‬
‭ anaged‬ ‭code‬ ‭is‬ ‭executed‬ ‭by‬ ‭the‬ ‭Common‬ ‭Language‬ ‭Runtime‬ ‭(CLR)‬ ‭in‬ ‭the‬ ‭.NET‬
M
‭framework.‬ ‭The‬ ‭CLR‬ ‭manages‬ ‭memory,‬ ‭handles‬ ‭garbage‬ ‭collection,‬ ‭provides‬ ‭type‬ ‭safety,‬
‭and performs other services such as exception handling and security management.‬

‭‬ L
● ‭ anguages:‬‭C#, VB.NET, F#‬
‭●‬ ‭Advantages:‬
‭○‬ ‭Automatic memory management (via Garbage Collection)‬
‭○‬ ‭Enhanced security and type safety‬
‭○‬ ‭Cross-language integration and support for other services like reflection‬
‭○‬ ‭Simplified development process, as the runtime handles lower-level‬
‭operations‬

‭Example of Managed Code:‬

‭ ublic class ManagedExample‬


p
‭{‬
‭public static void Main()‬
‭{‬
‭Console.WriteLine("This is managed code running in the CLR.");‬
‭}‬
‭}‬

‭Unmanaged Code‬

‭ nmanaged‬‭code‬‭is‬‭executed‬‭directly‬‭by‬‭the‬‭operating‬‭system‬‭,‬‭outside‬‭of‬‭the‬‭control‬‭of‬‭the‬
U
‭CLR.‬‭It‬‭typically‬‭refers‬‭to‬‭code‬‭written‬‭in‬‭lower-level‬‭languages‬‭like‬‭C‬‭or‬‭C++,‬‭which‬‭manage‬
‭their own memory and resources.‬

‭ hen‬‭you‬‭use‬‭unmanaged‬‭code‬‭in‬‭.NET,‬‭you‬‭often‬‭rely‬‭on‬‭platform‬‭invocation‬‭(P/Invoke)‬
W
‭to‬ ‭call‬ ‭native‬ ‭functions‬ ‭from‬ ‭Windows‬ ‭DLLs‬ ‭or‬ ‭external‬‭libraries.‬‭The‬‭CLR‬‭cannot‬‭manage‬
‭memory or provide safety guarantees for unmanaged code.‬

‭‬ L
● ‭ anguages:‬‭C, C++, or assembly‬
‭●‬ ‭Disadvantages:‬
‭○‬ ‭Manual memory management (using‬‭ malloc‬ free‬
‭,‬‭ ‭, etc.)‬
‭○‬ ‭More prone to memory leaks and crashes‬
‭○‬ ‭Lack of type safety and security‬

‭Example of Unmanaged Code (in C++):‬

‭#include <iostream>‬

‭int main()‬
‭{‬
‭ td::cout << "This is unmanaged code running directly on the OS.";‬
s
‭return 0;‬
‭}‬

‭Calling Unmanaged Code in .NET (via P/Invoke)‬

‭ ou can call unmanaged functions from a DLL in managed code using P/Invoke. Here’s an‬
Y
‭example:‬

‭ sing System;‬
u
‭using System.Runtime.InteropServices;‬

‭ lass Program‬
c
‭{‬
‭// P/Invoke declaration‬
‭[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]‬
‭public static extern IntPtr GetForegroundWindow();‬

‭ tatic void Main()‬


s
‭{‬
‭// Call the unmanaged function‬
‭IntPtr handle = GetForegroundWindow();‬
‭Console.WriteLine("Handle of the foreground window: " + handle);‬
‭}‬
‭}‬

‭Managing Unmanaged Resources in Managed Code‬

‭ ven though .NET uses managed code, there are times when you work with unmanaged‬
E
‭resources (like file handles, database connections, etc.). In such cases, you must manually‬
‭clean up these resources using the‬‭IDisposable‬‭interface‬‭and the‬‭using‬‭statement.‬

‭ ublic class UnmanagedResourceWrapper : IDisposable‬


p
‭{‬
‭// Assume this represents an unmanaged resource‬
‭private IntPtr unmanagedResource;‬

‭ ublic UnmanagedResourceWrapper()‬
p
‭{‬
‭// Allocate some unmanaged resource‬
‭}‬

‭ ublic void Dispose()‬


p
‭{‬
‭// Free the unmanaged resource‬
‭ eleaseUnmanagedResources();‬
R
‭GC.SuppressFinalize(this); // Suppress finalization‬
‭}‬

‭ rivate void ReleaseUnmanagedResources()‬


p
‭{‬
‭if (unmanagedResource != IntPtr.Zero)‬
‭{‬
‭// Free unmanaged resources here‬
‭unmanagedResource = IntPtr.Zero;‬
‭}‬
‭}‬

‭ UnmanagedResourceWrapper()‬
~
‭{‬
‭ReleaseUnmanagedResources();‬
‭}‬
‭}‬

I‭n .NET, the terms "managed code" and "unmanaged code" refer to how memory‬
‭management and other low-level details are handled by the runtime.‬

‭Managed Code‬

‭ anaged code is executed by the‬‭Common Language Runtime‬‭(CLR)‬‭in the .NET‬


M
‭framework. The CLR manages memory, handles garbage collection, provides type safety,‬
‭and performs other services such as exception handling and security management.‬

‭‬ L
● ‭ anguages:‬‭C#, VB.NET, F#‬
‭●‬ ‭Advantages:‬
‭○‬ ‭Automatic memory management (via Garbage Collection)‬
‭○‬ ‭Enhanced security and type safety‬
‭○‬ ‭Cross-language integration and support for other services like reflection‬
‭○‬ ‭Simplified development process, as the runtime handles lower-level‬
‭operations‬

‭Example of Managed Code:‬


‭ sharp‬
c
‭Copy code‬
public class ManagedExample‬

{‬

public static void Main()‬

{‬

Console.WriteLine("This is managed code running in the‬

CLR.");‬

}‬

}‬

‭Unmanaged Code‬

‭ nmanaged code is executed directly by the‬‭operating‬‭system‬‭, outside of the control of the‬


U
‭CLR. It typically refers to code written in lower-level languages like C or C++, which manage‬
‭their own memory and resources.‬

‭ hen you use unmanaged code in .NET, you often rely on‬‭platform invocation (P/Invoke)‬
W
‭to call native functions from Windows DLLs or external libraries. The CLR cannot manage‬
‭memory or provide safety guarantees for unmanaged code.‬

‭‬ L
● ‭ anguages:‬‭C, C++, or assembly‬
‭●‬ ‭Disadvantages:‬
‭○‬ ‭Manual memory management (using‬‭ malloc‬ free‬
‭,‬‭ ‭, etc.)‬
‭○‬ ‭More prone to memory leaks and crashes‬
‭○‬ ‭Lack of type safety and security‬

‭Example of Unmanaged Code (in C++):‬


‭ pp‬
c
‭Copy code‬
#include <iostream>‬

int main()‬

{‬

std::cout << "This is unmanaged code running directly on the‬

OS.";‬

return 0;‬

}‬

‭Calling Unmanaged Code in .NET (via P/Invoke)‬

‭ ou can call unmanaged functions from a DLL in managed code using P/Invoke. Here’s an‬
Y
‭example:‬

‭ sharp‬
c
‭Copy code‬
using System;‬

using System.Runtime.InteropServices;‬

class Program‬

{‬

// P/Invoke declaration‬

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling =‬

true)]‬

public static extern IntPtr GetForegroundWindow();‬

static void Main()‬



{‬

// Call the unmanaged function‬

IntPtr handle = GetForegroundWindow();‬

Console.WriteLine("Handle of the foreground window: " +‬

handle);‬

}‬

}‬

‭Managing Unmanaged Resources in Managed Code‬

‭ ven though .NET uses managed code, there are times when you work with unmanaged‬
E
‭resources (like file handles, database connections, etc.). In such cases, you must manually‬
‭clean up these resources using the‬‭IDisposable‬‭interface‬‭and the‬‭using‬‭statement.‬

‭Example:‬
‭ ublic class UnmanagedResourceWrapper : IDisposable‬
p
‭{‬
‭// Assume this represents an unmanaged resource‬
‭private IntPtr unmanagedResource;‬

‭ ublic UnmanagedResourceWrapper()‬
p
‭{‬
‭// Allocate some unmanaged resource‬
‭}‬

‭ ublic void Dispose()‬


p
‭{‬
‭// Free the unmanaged resource‬
‭ReleaseUnmanagedResources();‬
‭GC.SuppressFinalize(this); // Suppress finalization‬
‭}‬

‭ rivate void ReleaseUnmanagedResources()‬


p
‭{‬
‭if (unmanagedResource != IntPtr.Zero)‬
‭{‬
‭// Free unmanaged resources here‬
‭unmanagedResource = IntPtr.Zero;‬
‭}‬
‭}‬
‭ UnmanagedResourceWrapper()‬
~
‭{‬
‭ReleaseUnmanagedResources();‬
‭}‬
‭}‬

‭Summary:‬

‭‬ M
● ‭ anaged code‬‭: Code executed by the CLR with automatic‬‭memory management.‬
‭●‬ ‭Unmanaged code‬‭: Code that runs directly on the OS,‬‭typically requiring manual‬
‭memory management.‬
‭●‬ ‭P/Invoke‬‭: Allows managed code to call unmanaged functions‬‭from DLLs.‬
‭●‬ ‭IDisposable/Finalizer‬‭: Used to manage unmanaged resources‬‭within managed‬
‭code.‬

‭Q .NET, "assemblies"‬

I‭n .NET, "assemblies" are the building blocks of .NET applications. An assembly is a‬
‭compiled code library used for deployment, versioning, and security in .NET. Assemblies‬
‭contain‬‭IL (Intermediate Language) code‬‭that the .NET‬‭runtime (CLR) executes.‬
‭Assemblies also contain metadata that provides details about the types, versioning, culture,‬
‭and security information.‬

‭Types of Assemblies‬

‭1.‬ ‭Executable Assemblies (EXE)‬‭:‬


‭○‬ ‭These are self-executable files that run as standalone applications. For‬
‭example, a .NET console or Windows application typically compiles into an‬
‭EXE file.‬
‭2.‬ ‭Library Assemblies (DLL)‬‭:‬
‭○‬ ‭These are Dynamic Link Libraries (DLLs) that provide reusable functionality.‬
‭They are not directly executable but can be referenced by EXE assemblies or‬
‭other DLLs.‬

‭Components of a .NET Assembly‬

‭1.‬ ‭IL Code (Intermediate Language):‬


‭○‬ ‭The compiled code in .NET is stored as Intermediate Language (IL) code,‬
‭which is CPU-independent. When the assembly runs, the CLR uses a‬
‭Just-In-Time (JIT) compiler to convert the IL into machine code.‬
‭2.‬ ‭Metadata:‬
‭○‬ C ‭ ontains information about the types, methods, properties, and resources in‬
‭the assembly. It also includes version information, dependencies, and security‬
‭settings.‬
‭3.‬ ‭Manifest:‬
‭○‬ ‭Every assembly contains a manifest, which is part of its metadata. The‬
‭manifest contains information about the version, culture, and other‬
‭assemblies the assembly depends on. It acts as an index and provides‬
‭important metadata for the CLR to load the assembly properly.‬
‭ .‬ ‭Resources:‬
4
‭○‬ ‭Assemblies may contain resources like images, strings, or other files needed‬
‭by the application.‬

‭Creating Assemblies in .NET‬

‭ ssemblies are typically created during the build process in .NET, whether you're working in‬
A
‭Visual Studio or using the .NET CLI. Here's a basic outline of how to create an assembly:‬

‭1. Creating an Assembly (DLL) in Visual Studio‬

‭‬ C
● ‭ reate a new‬‭Class Library‬‭project in Visual Studio.‬
‭●‬ ‭Add code to the project.‬
‭●‬ ‭Build the project to generate a DLL file, which is the compiled assembly.‬

‭ ublic class MyLibrary‬


p
‭{‬
‭public void PrintMessage()‬
‭{‬
‭Console.WriteLine("Hello from the assembly!");‬
‭}‬
‭}‬

I‭n .NET, "assemblies" are the building blocks of .NET applications. An assembly is a‬
‭compiled code library used for deployment, versioning, and security in .NET. Assemblies‬
‭contain‬‭IL (Intermediate Language) code‬‭that the .NET‬‭runtime (CLR) executes.‬
‭Assemblies also contain metadata that provides details about the types, versioning, culture,‬
‭and security information.‬

‭Types of Assemblies‬

‭1.‬ ‭Executable Assemblies (EXE)‬‭:‬


‭○‬ ‭These are self-executable files that run as standalone applications. For‬
‭example, a .NET console or Windows application typically compiles into an‬
‭EXE file.‬
‭2.‬ ‭Library Assemblies (DLL)‬‭:‬
‭○‬ ‭These are Dynamic Link Libraries (DLLs) that provide reusable functionality.‬
‭They are not directly executable but can be referenced by EXE assemblies or‬
‭other DLLs.‬
‭Components of a .NET Assembly‬

‭1.‬ ‭IL Code (Intermediate Language):‬


‭○‬ ‭The compiled code in .NET is stored as Intermediate Language (IL) code,‬
‭which is CPU-independent. When the assembly runs, the CLR uses a‬
‭Just-In-Time (JIT) compiler to convert the IL into machine code.‬
‭2.‬ ‭Metadata:‬
‭○‬ ‭Contains information about the types, methods, properties, and resources in‬
‭the assembly. It also includes version information, dependencies, and security‬
‭settings.‬
‭3.‬ ‭Manifest:‬
‭○‬ ‭Every assembly contains a manifest, which is part of its metadata. The‬
‭manifest contains information about the version, culture, and other‬
‭assemblies the assembly depends on. It acts as an index and provides‬
‭important metadata for the CLR to load the assembly properly.‬
‭4.‬ ‭Resources:‬
‭○‬ ‭Assemblies may contain resources like images, strings, or other files needed‬
‭by the application.‬

‭Creating Assemblies in .NET‬

‭ ssemblies are typically created during the build process in .NET, whether you're working in‬
A
‭Visual Studio or using the .NET CLI. Here's a basic outline of how to create an assembly:‬

‭1. Creating an Assembly (DLL) in Visual Studio‬

‭‬ C
● ‭ reate a new‬‭Class Library‬‭project in Visual Studio.‬
‭●‬ ‭Add code to the project.‬
‭●‬ ‭Build the project to generate a DLL file, which is the compiled assembly.‬

‭ sharp‬
c
‭Copy code‬
public class MyLibrary‬

{‬

public void PrintMessage()‬

{‬

Console.WriteLine("Hello from the assembly!");‬

}‬

}‬

.dll‬‭file‬‭in the output directory (‬‭


‭After building the project, you will get a‬‭ /bin/Debug/‬‭or‬
/bin/Release/‬
‭ ‭).‬

‭2. Creating an Assembly using the .NET CLI‬

‭You can also use the .NET CLI to create an assembly:‬


‭dotnet new classlib -n MyLibrary‬

‭dotnet build‬

‭This will generate a DLL file for the library.‬

‭Referencing Assemblies‬

‭ o use an assembly in another project, you can reference it. Here’s how you can reference a‬
T
‭DLL in a .NET project:‬

‭1. Using Visual Studio‬

‭‬ R
● ‭ ight-click on the project in Solution Explorer.‬
‭●‬ ‭Select‬‭Add > Reference‬‭.‬
‭●‬ ‭Browse to the DLL you want to reference.‬

‭2. Using .NET CLI‬


‭dotnet add reference path/to/your/assembly.dll‬

‭ nce referenced, you can use the types and methods defined in the assembly:‬
O
‭using MyLibrary;‬

‭ lass Program‬
c
‭{‬
‭static void Main()‬
‭{‬
‭MyLibrary lib = new MyLibrary();‬
‭lib.PrintMessage();‬
‭}‬
‭}‬

‭Assembly Versioning‬

‭ very .NET assembly has a version number in the format‬


E
major.minor.build.revision‬
‭ ‭. You can specify the version‬‭in the‬‭AssemblyInfo.cs‬‭file‬
‭or in the project file:‬

/‭/ AssemblyInfo.cs‬
‭[assembly: AssemblyVersion("1.0.0.0")]‬
‭[assembly: AssemblyFileVersion("1.0.0.0")]‬
‭ roper versioning is important when you deploy and manage multiple versions of‬
P
‭assemblies.‬

‭Global Assembly Cache (GAC)‬

‭●‬ T ‭ he‬‭Global Assembly Cache (GAC)‬‭is a machine-wide‬‭store used to hold‬


‭assemblies that are shared among multiple applications.‬
‭●‬ ‭Assemblies stored in the GAC must be strong-named (signed with a cryptographic‬
‭key), which helps ensure version control and security.‬

‭You can add an assembly to the GAC using the‬‭Gacutil‬‭tool:‬

‭gacutil -i MyLibrary.dll‬

‭Strong-Naming Assemblies‬

‭ trong-naming an assembly involves giving it a unique identity by signing it with a‬


S
‭public-private key pair. This helps ensure the integrity and versioning of the assembly,‬
‭especially when it is placed in the GAC.‬

‭To strong-name an assembly:‬

‭1.‬ ‭Generate a key pair:‬

‭sn -k mykey.snk‬

‭Add the key to the assembly:‬

‭●‬ ‭In‬‭AssemblyInfo.cs‬‭or the project file, specify the‬‭key:‬

‭[assembly: AssemblyKeyFile("mykey.snk")]‬

‭1.‬ ‭Build the project. The assembly will now have a strong name.‬

‭Summary‬

‭●‬ A ‭ ssemblies‬‭are the fundamental building blocks of‬‭.NET applications, containing‬


‭compiled IL code and metadata.‬
‭●‬ ‭They come in two forms:‬‭EXE‬‭(for executables) and‬‭DLL‬‭(for libraries).‬
‭●‬ ‭You can create assemblies in Visual Studio or the .NET CLI, and reference them in‬
‭other projects.‬
‭●‬ ‭Assemblies can be versioned and placed in the‬‭Global‬‭Assembly Cache (GAC)‬‭for‬
‭shared use across applications.‬
‭●‬ ‭Strong-naming‬‭ensures the integrity and versioning‬‭of assemblies, especially when‬
‭stored in the GAC.‬
‭Q Fundamental of c#‬

‭ # (pronounced "C-sharp") is a modern, object-oriented programming language developed‬


C
‭by Microsoft as part of its .NET initiative. It is widely used for developing desktop, web, and‬
‭mobile applications, and is known for its simplicity, flexibility, and powerful features. Below‬
‭are the key fundamentals of C#:‬

‭1. Basic Structure of a C# Program‬

‭ basic C# program consists of namespaces, classes, and methods. Here’s the simplest‬
A
‭possible C# program:‬

‭using System; // Namespace‬

‭ lass Program // Class‬


c
‭{‬
‭static void Main() // Method‬
‭{‬
‭Console.WriteLine("Hello, World!"); // Statement‬
‭}‬
‭}‬

‭ amespaces‬‭: Containers that organize classes and other‬‭types, preventing name conflicts.‬
N
‭Classes‬‭: The blueprint for objects in C# (object-oriented‬‭programming).‬
‭Methods‬‭: Functions or procedures defined in classes‬‭to perform actions. Every C# program‬
Main()‬‭method, which is the entry point‬‭of the application.‬
‭must have a‬‭

‭2. Data Types and Variables‬

‭ # is a statically typed language, meaning you must declare the type of variables before‬
C
‭using them.‬

‭Value Types‬

‭These types store data directly and include:‬

‭●‬ ‭Primitive Types‬‭:‬


‭○‬ ‭
int‬‭: Stores integers.‬
‭○‬ ‭
double‬
‭: Stores floating-point numbers.‬
‭○‬ ‭
bool‬
‭: Stores boolean values (‬‭
true‬
‭/‬‭
false‬
‭).‬
‭○‬ ‭
char‬
‭: Stores single characters.‬
‭○‬ d‭ecimal‬ ‭: Stores high-precision decimal numbers, often‬‭used in financial‬
‭applications.‬
‭○‬ ‭float‬ ‭: Stores single-precision floating-point numbers.‬

‭Example:‬
i‭nt age = 30;‬
‭double price = 9.99;‬
‭char initial = 'A';‬
‭bool isTrue = true;‬

‭Reference Types‬

‭These types store references to the actual data. Examples include:‬

‭‬ S
● ‭ tring‬‭: Stores a sequence of characters.‬
‭●‬ ‭Arrays‬‭: Stores multiple values of the same type.‬
‭●‬ ‭Objects‬:‭ Represents the base type from which all other‬‭types derive.‬

‭Example:‬
‭ tring name = "John";‬
s
‭int[] numbers = { 1, 2, 3, 4 };‬
‭object obj = "Hello, World!";‬

‭3. Operators‬

‭C# supports a wide variety of operators, including:‬

+‭,‬‬‭
‭●‬ ‭Arithmetic operators‬‭:‬‭ -‭,‬‬‭
*‭,‬‬‭
/‭,‬‬‭
%‬
==‬
‭●‬ ‭Comparison operators‬‭:‬‭ !=‬
‭,‬‭ <‬
‭,‬‭ >‬
‭,‬‭ <=‬
‭,‬‭ >=‬
‭,‬‭
&&‬
‭●‬ ‭Logical operators‬‭:‬‭ ||‬
‭,‬‭ !‬
‭,‬‭
=‬
‭●‬ ‭Assignment operators‬‭:‬‭ +=‬
‭,‬‭ -=‬
‭,‬‭ *=‬
‭,‬‭ /=‬
‭,‬‭
++‬
‭●‬ ‭Increment/Decrement operators‬‭:‬‭ --‬
‭,‬‭

i‭nt a = 5;‬
‭int b = 10;‬
‭int sum = a + b; // Addition‬
‭bool isEqual = (a == b); // Comparison‬

‭4. Control Flow Statements‬

‭ ontrol flow statements are used to make decisions and execute code conditionally or‬
C
‭repeatedly.‬

‭Conditional Statements‬

‭●‬ ‭
if‬
‭: Executes a block of code if a condition is true.‬
‭●‬ ‭
else‬
‭: Executes if the condition is false.‬
‭●‬ ‭
else if‬
‭: Used for multiple conditions.‬
‭●‬ ‭
switch‬
‭: A more efficient way to handle multiple possible‬‭values for a variable.‬
‭Example:‬
‭int x = 10;‬

i‭f (x > 5)‬


‭{‬
‭Console.WriteLine("x is greater than 5");‬
‭}‬
‭else if (x == 5)‬
‭{‬
‭Console.WriteLine("x is 5");‬
‭}‬
‭else‬
‭{‬
‭Console.WriteLine("x is less than 5");‬
‭}‬

‭ witch (x)‬
s
‭{‬
‭case 1:‬
‭Console.WriteLine("x is 1");‬
‭break;‬
‭case 10:‬
‭Console.WriteLine("x is 10");‬
‭break;‬
‭default:‬
‭Console.WriteLine("x is something else");‬
‭break;‬
‭}‬

‭Loops‬

‭●‬ ‭
for‬
‭: Loops through a block of code a specified number‬‭of times.‬
‭●‬ ‭
while‬
‭: Repeats a block of code while a condition is‬‭true.‬
‭●‬ ‭
do-while‬ while‬
‭: Same as‬‭ ‭, but it checks the condition‬‭after the first iteration.‬
‭●‬ ‭
foreach‬
‭: Loops through items in a collection (arrays,‬‭lists, etc.).‬

‭Example:‬
/‭/ for loop‬
‭for (int i = 0; i < 5; i++)‬
‭{‬
‭Console.WriteLine(i);‬
‭}‬

/‭/ while loop‬


‭int j = 0;‬
‭while (j < 5)‬
‭{‬
‭ onsole.WriteLine(j);‬
C
‭j++;‬
‭}‬

/‭/ foreach loop‬


‭int[] numbers = { 1, 2, 3, 4, 5 };‬
‭foreach (int number in numbers)‬
‭{‬
‭Console.WriteLine(number);‬
‭}‬

‭5. Methods (Functions)‬

‭ ethods in C# are blocks of code that perform a task. Methods can have parameters and‬
M
‭return values.‬

‭Example:‬
‭ lass Program‬
c
‭{‬
‭static void Main()‬
‭{‬
‭int result = Add(5, 3); // Call the method‬
‭Console.WriteLine(result);‬
‭}‬

‭ tatic int Add(int a, int b) // Method definition‬


s
‭{‬
‭return a + b;‬
‭}‬
‭}‬

‭6. Classes and Objects‬

‭ # is an object-oriented language, and classes are its core building blocks. Classes define‬
C
‭the properties and behavior of objects.‬

‭Example:‬
‭ lass Car‬
c
‭{‬
‭public string Make { get; set; }‬
‭public string Model { get; set; }‬

‭ ublic void Start()‬


p
‭{‬
‭Console.WriteLine("The car has started.");‬
‭}‬
‭}‬

‭ lass Program‬
c
‭{‬
‭static void Main()‬
‭{‬
‭Car myCar = new Car();‬
‭myCar.Make = "Toyota";‬
‭myCar.Model = "Camry";‬
‭myCar.Start();‬
‭}‬
‭}‬

‭7. Object-Oriented Principles‬

‭C# is designed around four key OOP principles:‬

‭●‬ E ‭ ncapsulation‬‭: Bundling of data (fields) and methods‬‭that operate on the data into a‬
‭single unit (class).‬
‭●‬ ‭Abstraction‬‭: Hiding complex implementation details‬‭and exposing only the essential‬
‭features.‬
‭●‬ ‭Inheritance‬‭: Allows one class to inherit members (fields,‬‭methods) from another‬
‭class.‬
‭●‬ ‭Polymorphism‬‭: Allows one interface to be used for‬‭a general class of actions (e.g.,‬
‭method overriding, interfaces).‬

‭Example of Inheritance:‬
‭ lass Animal‬
c
‭{‬
‭public void Eat()‬
‭{‬
‭Console.WriteLine("Eating");‬
‭}‬
‭}‬

‭ lass Dog : Animal // Inheritance‬


c
‭{‬
‭public void Bark()‬
‭{‬
‭Console.WriteLine("Barking");‬
‭}‬
‭}‬

‭ lass Program‬
c
‭{‬
‭static void Main()‬
‭{‬
‭ og dog = new Dog();‬
D
‭dog.Eat(); // Inherited method‬
‭dog.Bark(); // Dog-specific method‬
‭}‬
‭}‬

‭8. Exception Handling‬

try‬
‭C# provides structured exception handling using‬‭ catch‬
‭,‬‭ finally‬‭blocks.‬
‭, and‬‭

‭Example:‬
t‭ry‬
‭{‬
‭int result = 10 / 0; // This will cause an exception‬
‭}‬
‭catch (DivideByZeroException ex)‬
‭{‬
‭Console.WriteLine("Error: " + ex.Message);‬
‭}‬
‭finally‬
‭{‬
‭Console.WriteLine("This code runs no matter what.");‬
‭}‬

‭9. Properties‬

‭ roperties in C# provide a flexible mechanism to read, write, or compute the values of‬
P
‭private fields.‬

‭Example:‬
‭ lass Person‬
c
‭{‬
‭private string name;‬

‭ ublic string Name // Property‬


p
‭{‬
‭get { return name; }‬
‭set { name = value; }‬
‭}‬
‭}‬

‭10. Interfaces‬
I‭nterfaces define a contract for classes, specifying what methods or properties they must‬
‭implement. Unlike inheritance, interfaces only define methods without implementation.‬

‭Example:‬
i‭nterface IAnimal‬
‭{‬
‭void MakeSound();‬
‭}‬

‭ lass Dog : IAnimal‬


c
‭{‬
‭public void MakeSound()‬
‭{‬
‭Console.WriteLine("Bark");‬
‭}‬
‭}‬

‭Summary:‬

‭‬ C
● ‭ # is object-oriented‬‭: It revolves around classes‬‭and objects.‬
‭●‬ ‭Data types‬‭: Strongly typed language with both value‬‭types and reference types.‬
‭●‬ ‭Control flow‬‭: Uses conditional statements (‬‭if‬ switch‬
‭,‬‭ ‭)‬‭and loops (‬‭
for‬ while‬
‭,‬‭ ‭,‬
‭oreach‬
f ‭).‬
‭●‬ ‭OOP principles‬‭: Inheritance, encapsulation, polymorphism,‬‭and abstraction.‬
‭ ‬ ‭Methods and properties‬‭: Central to defining behavior‬‭and controlling access to‬

‭data.‬
try‬
‭●‬ ‭Exception handling‬‭: Structured with‬‭ catch‬
‭,‬‭ finally‬
‭, and‬‭ ‭.‬

‭ Boxing and Unboxing‬


Q
int‬
‭In C#,‬‭boxing‬‭and‬‭unboxing‬‭are operations related‬‭to converting value types (such as‬‭ ‭,‬
char‬
‭ ‭, etc.) into reference types (‬‭
object‬
‭) and vice‬‭versa.‬

‭1. Boxing‬

object‬‭(a reference type). When a‬


‭ oxing‬‭is the process of converting a value type into‬‭an‬‭
B
‭value type is boxed, the value is wrapped inside an object, and the runtime creates a‬
‭heap-allocated object to hold the value.‬

‭Example of Boxing:‬
i‭nt num = 123; // num is a value type‬
‭object obj = num; // Boxing: num is stored as an object‬

‭In the example above:‬


‭●‬ T num‬‭is a value type, and when it is assigned to the‬‭
‭ he integer‬‭ obj‬‭object (a‬
‭reference type), the integer is boxed.‬
‭●‬ ‭Boxing involves copying the value from the stack (where value types are stored) to‬
‭the heap (where reference types are stored).‬

‭2. Unboxing‬

‭ nboxing‬‭is the reverse process of boxing, where an‬‭object (which holds a value type) is‬
U
‭explicitly cast back to its original value type. Unboxing requires explicit casting, and if you‬
‭attempt to unbox an object to the wrong value type, a‬‭ InvalidCastException‬‭will be‬
‭thrown.‬

‭Example of Unboxing:‬
‭ bject obj = 123;
o // Boxing‬
‭int num = (int)obj; // Unboxing: obj is cast back to an int‬

‭In the example above:‬

obj‬‭holds the boxed integer value.‬


‭●‬ ‭The‬‭
(int)‬‭cast is used to unbox the value from the‬‭object back into an‬‭
‭●‬ ‭The‬‭ int‬
‭.‬

‭Key Points‬

‭●‬ B ‭ oxing‬‭: Conversion of a value type to a reference‬‭type (object). The value is copied‬
‭from the stack to the heap.‬
‭●‬ ‭Unboxing‬‭: Conversion of a reference type (object)‬‭back to a value type. The object‬
‭is cast back to its original value type, and the value is copied from the heap to the‬
‭stack.‬
‭●‬ ‭Performance considerations‬‭: Boxing and unboxing involve‬‭overhead because they‬
‭involve copying data between the stack and heap and allocating memory on the‬
‭heap. Excessive boxing and unboxing operations can impact performance, so it's‬
‭generally a good practice to avoid them when possible.‬

‭Example of Boxing and Unboxing Together:‬


‭using System;‬

‭ lass Program‬
c
‭{‬
‭static void Main()‬
‭{‬
‭int value = 42; // Value type‬
‭object obj = value; // Boxing: value is stored in obj as an object‬
‭Console.WriteLine(obj); // Output: 42‬

i‭nt unboxedValue = (int)obj; // Unboxing: obj is cast back to int‬


‭Console.WriteLine(unboxedValue); // Output: 42‬
‭}‬
‭}‬

‭Performance Impact‬

‭ hile boxing and unboxing are useful, they can lead to performance issues if used‬
W
‭excessively due to:‬

‭1.‬ H ‭ eap allocation‬‭: Boxing creates objects on the heap,‬‭which incurs memory‬
‭allocation overhead.‬
‭2.‬ ‭Garbage collection‬‭: The boxed objects will eventually‬‭be garbage collected, adding‬
‭further overhead.‬
‭3.‬ ‭Type safety‬‭: Unboxing requires type casting, and incorrect‬‭casting can lead to‬
‭runtime exceptions.‬

‭Example of performance overhead due to boxing:‬


f‭or (int i = 0; i < 1000000; i++)‬
‭{‬
‭object obj = i; // Boxing happens in each iteration‬
‭}‬

‭Alternatives to Boxing/Unboxing‬

‭ o avoid boxing and unboxing, you can use‬‭generics‬‭.‬‭Generics allow you to create classes,‬
T
‭methods, and structures that work with any data type without requiring boxing and unboxing.‬

‭Example using Generics (no boxing/unboxing):‬


‭ ist<int> numbers = new List<int>();‬
L
‭numbers.Add(42); // No boxing occurs‬

List<int>‬‭can handle value types‬


‭In this case, no boxing is required because the generic‬‭
object‬
‭directly without converting them to‬‭ ‭.‬

‭Summary‬

‭●‬ B object‬‭(reference type), and‬


‭ oxing‬‭is the process of converting a value type to‬‭an‬‭
‭it moves the value from the stack to the heap.‬
‭●‬ ‭Unboxing‬‭is the process of converting an‬‭ object‬‭back‬‭into a value type and‬
‭requires explicit casting.‬
‭●‬ ‭Performance issues‬‭can arise from frequent boxing/unboxing,‬‭but you can mitigate‬
‭these by using generics.‬

‭Q Introduction to ADO.Net‬
‭ DO.NET‬‭(ActiveX Data Objects .NET) is a set of classes that expose data access services‬
A
‭for .NET applications. It is a part of the .NET Framework and provides a bridge between a‬
‭database and a .NET application, allowing you to work with data in a structured and‬
‭disconnected way.‬

‭Key Components of ADO.NET‬

‭1.‬ C ‭ onnection‬‭: Represents a connection to a specific‬‭data source (such as SQL‬


‭Server, Oracle, MySQL).‬
‭○‬ ‭SqlConnection‬‭: Used to connect to Microsoft SQL Server.‬
‭○‬ ‭OleDbConnection‬‭: Used for OLE DB databases.‬
‭○‬ ‭OdbcConnection‬‭: Used for ODBC data sources.‬
‭2.‬ ‭Command‬‭: Represents a SQL query or stored procedure‬‭that you want to execute‬
‭against a data source.‬
‭○‬ ‭SqlCommand‬‭: Used to execute SQL commands against SQL‬‭Server.‬
‭3.‬ ‭DataReader‬‭: Provides a way of reading a forward-only‬‭stream of data from a‬
‭database. It offers high performance for reading large datasets.‬
‭○‬ ‭SqlDataReader‬‭: Used to read data retrieved from a‬‭SQL Server database.‬
‭4.‬ ‭DataSet‬‭: Represents an in-memory cache of data that‬‭can hold multiple tables and‬
‭relationships between them. It allows working with disconnected data.‬
‭○‬ ‭DataSet‬‭can be filled using a‬‭DataAdapter‬‭and allows‬‭you to work with data‬
‭without keeping an open connection to the database.‬
‭5.‬ ‭DataAdapter‬‭: Acts as a bridge between a‬‭ DataSet‬‭and‬‭the database. It is used to‬
DataSet‬
‭retrieve data from a database and populate the‬‭ ‭,‬‭as well as to push updates‬
‭from the‬‭DataSet‬‭back to the database.‬
‭○‬ ‭SqlDataAdapter‬‭: Works with SQL Server databases to‬‭fill datasets.‬
‭6.‬ ‭DataTable‬‭: Represents a single table of in-memory‬‭data.‬
‭ .‬ ‭Transaction‬‭: Supports transactions to ensure data‬‭integrity in the database when‬
7
‭executing multiple queries.‬

‭ADO.NET Architecture‬

‭There are two primary data access models in ADO.NET:‬

‭●‬ C ‭ onnected Architecture‬‭: You keep an active connection‬‭to the database while‬
DataReader‬‭for real-time reading of‬
‭working with the data. For example, using‬‭
‭database records.‬
‭●‬ ‭Disconnected Architecture‬‭: Data is retrieved and worked‬‭on independently of the‬
‭database connection.‬‭DataSet‬‭and‬‭
DataAdapter‬‭are key‬‭components for‬
‭disconnected data access.‬

‭Basic ADO.NET Operations‬

‭1. Establishing a Connection‬


SqlConnection‬‭(or‬
‭ o work with a database, you need to first create a connection using‬‭
T
‭another connection class for different databases).‬

‭ tring connectionString = "Data Source=server_name;Initial‬


s
‭Catalog=database_name;Integrated Security=True;";‬
‭using (SqlConnection connection = new SqlConnection(connectionString))‬
‭{‬
‭connection.Open();‬
‭Console.WriteLine("Connection Opened");‬
‭// Perform database operations‬
‭connection.Close();‬
‭}‬

‭2. Executing a Command‬

SqlCommand‬‭is used to execute SQL queries such as‬‭


‭ SELECT‬ INSERT‬
‭,‬‭ UPDATE‬
‭,‬‭ ‭, or‬
DELETE‬
‭ ‭.‬

‭ tring query = "SELECT * FROM Employees";‬


s
‭using (SqlConnection connection = new SqlConnection(connectionString))‬
‭{‬
‭SqlCommand command = new SqlCommand(query, connection);‬
‭connection.Open();‬
‭SqlDataReader reader = command.ExecuteReader();‬

‭ hile (reader.Read())‬
w
‭{‬
‭Console.WriteLine(reader["FirstName"] + " " + reader["LastName"]);‬
‭}‬

r‭ eader.Close();‬
‭connection.Close();‬
‭}‬

‭3. Using DataAdapter and DataSet for Disconnected Data Access‬

DataAdapter‬‭and fill‬‭it into a‬‭


‭ ou can retrieve data using a‬‭
Y DataSet‬
‭. This allows working‬
‭with the data offline.‬

‭ tring query = "SELECT * FROM Employees";‬


s
‭SqlDataAdapter adapter = new SqlDataAdapter(query, connectionString);‬
‭DataSet dataSet = new DataSet();‬
‭adapter.Fill(dataSet, "Employees");‬

f‭oreach (DataRow row in dataSet.Tables["Employees"].Rows)‬


‭{‬
‭Console.WriteLine(row["FirstName"] + " " + row["LastName"]);‬
‭}‬

‭4. Inserting Data into the Database‬

SqlCommand‬‭with an‬‭
‭To insert data into the database, you can use the‬‭ INSERT‬‭statement.‬

‭ tring query = "INSERT INTO Employees (FirstName, LastName, Age) VALUES‬


s
‭(@FirstName, @LastName, @Age)";‬
‭using (SqlConnection connection = new SqlConnection(connectionString))‬
‭{‬
‭SqlCommand command = new SqlCommand(query, connection);‬
‭command.Parameters.AddWithValue("@FirstName", "John");‬
‭command.Parameters.AddWithValue("@LastName", "Doe");‬
‭command.Parameters.AddWithValue("@Age", 30);‬

‭ onnection.Open();‬
c
‭command.ExecuteNonQuery();‬
‭connection.Close();‬
‭}‬

‭5. Updating Data‬

UPDATE‬‭statement‬‭with a‬‭
‭To update existing records, use the‬‭ SqlCommand‬
‭.‬

‭ tring query = "UPDATE Employees SET Age = @Age WHERE FirstName = @FirstName‬
s
‭AND LastName = @LastName";‬
‭using (SqlConnection connection = new SqlConnection(connectionString))‬
‭{‬
‭SqlCommand command = new SqlCommand(query, connection);‬
‭command.Parameters.AddWithValue("@Age", 35);‬
‭command.Parameters.AddWithValue("@FirstName", "John");‬
‭command.Parameters.AddWithValue("@LastName", "Doe");‬

‭ onnection.Open();‬
c
‭command.ExecuteNonQuery();‬
‭connection.Close();‬
‭}‬

‭6. Deleting Data‬

DELETE‬‭statement.‬
‭To delete data from the database, use a‬‭

‭ tring query = "DELETE FROM Employees WHERE FirstName = @FirstName AND‬


s
‭LastName = @LastName";‬
‭using (SqlConnection connection = new SqlConnection(connectionString))‬
‭{‬
‭ qlCommand command = new SqlCommand(query, connection);‬
S
‭command.Parameters.AddWithValue("@FirstName", "John");‬
‭command.Parameters.AddWithValue("@LastName", "Doe");‬

‭ onnection.Open();‬
c
‭command.ExecuteNonQuery();‬
‭connection.Close();‬
‭}‬

‭ADO.NET Architecture: Connected vs Disconnected‬

‭1.‬ ‭Connected Model‬‭:‬


‭○‬ ‭The connection remains open while the data is being worked on.‬
‭○‬ ‭Example: Using‬‭ SqlDataReader‬‭for fast, forward-only‬‭access to data.‬
‭2.‬ ‭Disconnected Model‬‭:‬
‭○‬ ‭The connection is opened briefly to retrieve data, then closed. Data is worked‬
‭on while the connection is closed.‬
‭○‬ ‭Example: Using‬‭ DataSet‬‭and‬‭ SqlDataAdapter‬‭for working‬‭with data offline‬
‭and later pushing updates to the database.‬

‭Advantages of ADO.NET‬

‭●‬ S ‭ calability‬‭: The disconnected model allows ADO.NET‬‭to work with data in a‬
‭scalable way, as it doesn’t need to keep the connection open.‬
‭●‬ ‭Flexibility‬‭: ADO.NET supports various types of data‬‭sources, including relational‬
‭databases, XML, and web services.‬
‭●‬ ‭Performance‬‭: ADO.NET’s‬‭ DataReader‬‭offers fast, efficient‬‭access to data.‬

‭Summary‬

‭●‬ A ‭ DO.NET‬‭is a data access technology for working with‬‭databases in .NET‬


‭applications.‬
SqlDataReader‬
‭●‬ ‭It provides‬‭connected‬‭(using‬‭ DataSet‬
‭) and‬‭disconnected‬‭(using‬‭
‭ nd‬‭
a DataAdapter‬ ‭) models for interacting with databases.‬
‭●‬ ‭Key components include‬‭SqlConnection‬‭,‬‭SqlCommand‬‭,‬‭SqlDataReader‬‭,‬‭DataSet‬‭,‬
‭DataAdapter‬‭, and‬‭DataTable‬‭.‬
‭ ‬ ‭It supports executing SQL queries, transactions, and reading/writing data to‬

‭databases with both synchronous and asynchronous options.‬

‭ DO.NET is widely used in enterprise applications where reliable, scalable, and efficient‬
A
‭data access is required.‬

‭Q SQL injection‬
‭ QL injection‬‭is a type of security vulnerability that allows an attacker to interfere with the‬
S
‭queries an application makes to its database. This occurs when user input is improperly‬
‭handled, enabling malicious SQL code to be executed. SQL injection can result in‬
‭unauthorized access to sensitive data, database modification, or even complete system‬
‭compromise.‬

‭How SQL Injection Works‬

‭ QL injection happens when an application inserts untrusted data (such as form inputs) into‬
S
‭an SQL query without proper validation or escaping. Attackers can inject malicious SQL‬
‭code into the input fields, which is then executed by the database.‬

‭Example of Vulnerable Code:‬

‭ tring query = "SELECT * FROM Users WHERE Username = '" + username + "' AND‬
s
‭Password = '" + password + "'";‬

‭ qlCommand command = new SqlCommand(query, connection);‬


S
‭command.ExecuteNonQuery();‬

‭If the user enters:‬

‭●‬ ‭
username‬‭=‬‭
' OR 1=1 --‬
‭●‬ ‭
password‬‭=‬‭
anything‬

‭The resulting query would be:‬

‭SELECT * FROM Users WHERE Username = '' OR 1=1 --' AND Password = 'anything'‬

‭ he‬‭
T OR 1=1‬‭condition is always true, and the‬‭
--‬‭is‬‭a comment marker that ignores the rest‬
‭of the query. This means the attacker can bypass authentication and log in without knowing‬
‭the actual credentials.‬

‭Types of SQL Injection‬

‭1.‬ C ‭ lassic SQL Injection‬‭: This is when user inputs are‬‭injected into SQL queries‬
‭directly without validation, resulting in unintended execution of commands.‬
‭2.‬ ‭Blind SQL Injection‬‭: In blind SQL injection, attackers‬‭don't get direct feedback from‬
‭the database. They send queries and infer information by analyzing the application's‬
‭behavior (such as response time or error messages).‬
‭3.‬ ‭Error-based SQL Injection‬‭: This occurs when the attacker‬‭manipulates inputs to‬
‭cause the database to throw errors. The error messages can reveal sensitive‬
‭information about the database's structure or contents.‬
‭4.‬ ‭Union-based SQL Injection‬‭: In this form, attackers‬‭use the‬‭ UNION‬‭operator to‬
‭combine the results of multiple queries into a single response, potentially extracting‬
‭data from other tables.‬
‭5.‬ T
‭ ime-based SQL Injection‬‭: Attackers use time delays in queries to infer whether the‬
‭input is being executed, even when no data is returned. For example, injecting a‬
SLEEP(5)‬‭function into the query to cause a delay‬‭and observe the response.‬

‭Consequences of SQL Injection‬

‭●‬ D ‭ ata Theft‬‭: Attackers can extract sensitive information‬‭such as usernames,‬


‭passwords, or credit card details.‬
‭●‬ ‭Data Tampering‬‭: SQL injection can be used to modify‬‭or delete data, potentially‬
‭causing financial or reputational damage.‬
‭●‬ ‭Authentication Bypass‬‭: Attackers can bypass login‬‭systems and gain unauthorized‬
‭access to accounts.‬
‭●‬ ‭Database Compromise‬‭: Attackers can execute administrative‬‭commands on the‬
‭database, potentially gaining full control over the system.‬
‭●‬ ‭Denial of Service (DoS)‬‭: Attackers can issue queries‬‭that overload the database,‬
‭causing it to crash or become unresponsive.‬

‭Preventing SQL Injection‬

‭1.‬ U
‭ se Parameterized Queries (Prepared Statements)‬‭The‬‭most effective way to‬
‭prevent SQL injection is to use parameterized queries or prepared statements. This‬
‭ensures that user input is treated as data, not executable code.‬
‭Example of Parameterized Query in C#:‬

‭ tring query = "SELECT * FROM Users WHERE Username = @Username AND Password =‬
s
‭@Password";‬
‭using (SqlCommand command = new SqlCommand(query, connection))‬
‭{‬
‭command.Parameters.AddWithValue("@Username", username);‬
‭command.Parameters.AddWithValue("@Password", password);‬
‭command.ExecuteNonQuery();‬
‭}‬

@Username‬‭and‬‭
I‭n this example, the user input is bound to the‬‭ @Password‬‭parameters,‬
‭preventing SQL injection.‬
‭Use Stored Procedures‬‭Stored procedures can also help‬‭prevent SQL injection by‬
‭encapsulating SQL queries and ensuring input validation.‬
‭Example of Stored Procedure:‬

‭CREATE PROCEDURE GetUser‬


‭@Username NVARCHAR(50),‬
‭@Password NVARCHAR(50)‬
‭AS‬
‭BEGIN‬
‭SELECT * FROM Users WHERE Username = @Username AND Password =‬
‭@Password;‬
‭END‬
I‭nput Validation‬‭Always validate and sanitize user inputs. Ensure that input data matches‬
‭expected formats (e.g., alphanumeric for usernames).‬
‭Use ORM Frameworks‬‭Object-Relational Mapping (ORM)‬‭frameworks like Entity‬
‭Framework or Hibernate automatically use parameterized queries and can help protect‬
‭against SQL injection.‬
‭Example with Entity Framework:‬

‭var user = dbContext.Users‬


‭.Where(u => u.Username == username && u.Password == password)‬
‭.FirstOrDefault();‬

‭1.‬ L ‭ east Privilege Principle‬‭Ensure that the database‬‭user account used by the‬
‭application has the least privileges required. For example, avoid using admin‬
‭accounts for queries.‬
‭2.‬ ‭Avoid Dynamic SQL‬‭Avoid constructing SQL queries dynamically‬‭using string‬
‭concatenation. Always use parameterized queries instead.‬
‭3.‬ ‭Escape User Input (as a last resort)‬‭If dynamic SQL‬‭is absolutely necessary,‬
‭ensure proper escaping of user input to prevent malicious characters from being‬
‭executed.‬
‭4.‬ ‭Use ORM Frameworks‬‭Use Object-Relational Mapping (ORM)‬‭frameworks like‬
‭Entity Framework, which abstract SQL queries and prevent SQL injection by using‬
‭safe methods for querying.‬

‭Example of Safe Code with Parameterized Query:‬


‭ tring query = "SELECT * FROM Users WHERE Username = @Username AND Password =‬
s
‭@Password";‬

‭ sing (SqlConnection connection = new SqlConnection(connectionString))‬


u
‭{‬
‭using (SqlCommand command = new SqlCommand(query, connection))‬
‭{‬
‭command.Parameters.AddWithValue("@Username", username);‬
‭command.Parameters.AddWithValue("@Password", password);‬

‭ onnection.Open();‬
c
‭SqlDataReader reader = command.ExecuteReader();‬

i‭f (reader.HasRows)‬
‭{‬
‭Console.WriteLine("Login successful!");‬
‭}‬
‭else‬
‭{‬
‭Console.WriteLine("Login failed.");‬
‭}‬
‭reader.Close();‬
‭}‬
‭}‬

‭ummary of Best Practices to Prevent SQL Injection:‬

‭ .‬
1 ‭ se parameterized queries‬‭(prepared statements).‬
U
‭2.‬ ‭Validate and sanitize all user inputs‬‭.‬
‭3.‬ ‭Use stored procedures‬‭for database queries.‬
‭4.‬ ‭Avoid dynamic SQL‬‭where possible.‬
‭5.‬ ‭Limit database privileges‬‭for the application account.‬
‭6.‬ ‭Use ORM frameworks‬‭to abstract SQL queries.‬

‭ y following these best practices, you can effectively reduce the risk of SQL injection in your‬
B
‭application.‬

‭Q Parameterized queries‬

‭ arameterized queries‬‭(also known as prepared statements)‬‭are a technique used to‬


P
‭protect applications from SQL injection attacks and improve the performance of SQL query‬
‭execution. Parameterized queries allow you to define the SQL query structure and provide‬
‭the values (parameters) separately, ensuring that user input is treated as data, not‬
‭executable SQL code.‬

‭Why Use Parameterized Queries?‬

‭1.‬ S ‭ ecurity‬‭: They prevent SQL injection by ensuring that‬‭user input is treated as data‬
‭rather than part of the SQL command.‬
‭2.‬ ‭Performance‬‭: The SQL query execution plan can be reused,‬‭which can improve‬
‭performance for repeated queries.‬
‭3.‬ ‭Readability‬‭: Parameterized queries often make code‬‭cleaner and easier to‬
‭understand.‬

‭How Parameterized Queries Work‬

I‭n a parameterized query, placeholders are used for the parameters, and the actual values‬
‭are supplied separately. The database engine handles the values as data rather than‬
‭executable SQL, which prevents malicious input from being executed as code.‬

‭Example of a Non-Parameterized (Vulnerable) Query:‬

‭ his code concatenates the user input directly into the SQL query, making it vulnerable to‬
T
‭SQL injection.‬

‭ tring query = "SELECT * FROM Users WHERE Username = '" + username + "' AND‬
s
‭Password = '" + password + "'";‬
‭ qlCommand command = new SqlCommand(query, connection);‬
S
‭command.ExecuteNonQuery();‬

‭ xample of a Parameterized Query in C#:‬


E
‭string query = "SELECT * FROM Users WHERE Username = @Username AND Password =‬
‭@Password";‬
‭using (SqlCommand command = new SqlCommand(query, connection))‬
‭{‬
‭// Adding parameters to the query‬
‭command.Parameters.AddWithValue("@Username", username);‬
‭command.Parameters.AddWithValue("@Password", password);‬

‭ onnection.Open();‬
c
‭SqlDataReader reader = command.ExecuteReader();‬

i‭f (reader.HasRows)‬
‭{‬
‭Console.WriteLine("Login successful!");‬
‭}‬
‭else‬
‭{‬
‭Console.WriteLine("Login failed.");‬
‭}‬

‭reader.Close();‬
‭}‬

‭Key Points:‬

‭●‬ T ‭ he SQL query is defined with placeholders (‬‭ @Username‬ @Password‬


‭,‬‭ ‭) for‬
‭parameters.‬
‭●‬ ‭The‬‭ AddWithValue‬‭method binds the values for‬‭ @Username‬‭and‬‭ @Password‬‭to‬
‭the query in a safe way.‬
‭●‬ ‭The database engine will treat these parameters as data, not as part of the SQL‬
‭command, which eliminates the possibility of SQL injection.‬

‭Parameterized Query with Stored Procedure:‬

‭If you're using stored procedures, you can also pass parameters in a secure way.‬

‭ sing (SqlCommand command = new SqlCommand("GetUser", connection))‬


u
‭{‬
‭command.CommandType = CommandType.StoredProcedure;‬
‭command.Parameters.AddWithValue("@Username", username);‬
‭command.Parameters.AddWithValue("@Password", password);‬
‭ onnection.Open();‬
c
‭SqlDataReader reader = command.ExecuteReader();‬
‭// Process the results‬
‭}‬

‭Performance Benefits of Parameterized Queries‬

‭1.‬ E ‭ xecution Plan Reuse‬‭: When you use parameterized queries,‬‭the database can‬
‭reuse the execution plan, improving performance for repeated queries with different‬
‭parameters.‬
‭2.‬ ‭Reduced Parsing‬‭: The database doesn't need to parse‬‭and compile the SQL query‬
‭for every execution since the structure of the query remains the same.‬

‭Handling Different Data Types in Parameterized Queries‬

SqlParameter‬‭class‬
‭You can specify different data types for the parameters by using the‬‭
AddWithValue‬‭(which infers the data type‬‭automatically).‬
‭instead of‬‭

‭ tring query = "INSERT INTO Employees (FirstName, Age) VALUES (@FirstName, @Age)";‬
s
‭using (SqlCommand command = new SqlCommand(query, connection))‬
‭{‬
‭command.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar) {‬
‭Value = "John" });‬
‭command.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int) { Value = 30 });‬

‭ onnection.Open();‬
c
‭command.ExecuteNonQuery();‬
‭}‬

SqlParameter‬‭Over‬‭
‭Benefits of Using‬‭ AddWithValue‬

‭●‬ B SqlParameter‬‭allows you‬‭to specify the exact type and‬


‭ etter Type Control‬‭: Using‬‭
‭size of the parameters, which can improve performance and avoid potential issues‬
‭(e.g., data truncation).‬
null‬‭values properly, unlike‬‭
‭●‬ ‭Nullable Types‬‭: It handles‬‭ AddWithValue‬ ‭, which can‬
null‬
‭infer incorrect types when passing‬‭ ‭.‬

‭Example with Multiple Parameters:‬


‭ tring query = "SELECT * FROM Products WHERE Category = @Category AND Price <‬
s
‭@Price";‬
‭using (SqlCommand command = new SqlCommand(query, connection))‬
‭{‬
‭command.Parameters.AddWithValue("@Category", "Electronics");‬
‭command.Parameters.AddWithValue("@Price", 1000);‬
‭ onnection.Open();‬
c
‭SqlDataReader reader = command.ExecuteReader();‬
‭// Process the results‬
‭}‬

‭In this case:‬

‭●‬ T
‭ he‬‭@Category‬‭and‬‭@Price‬‭parameters are safely passed,‬‭avoiding SQL injection‬
‭attacks and making the query more efficient.‬

‭Summary‬

‭●‬ P ‭ arameterized queries‬‭prevent SQL injection by separating‬‭SQL commands from‬


‭data input.‬
‭●‬ ‭They improve‬‭security‬‭by treating user input as data‬‭rather than part of the SQL‬
‭command.‬
‭●‬ ‭They also offer‬‭performance benefits‬‭due to execution‬‭plan reuse.‬
‭●‬ ‭Use parameterized queries‬‭in any situation where user‬‭input is involved to prevent‬
‭malicious input from altering your database operations.‬

‭ y using parameterized queries, you can ensure that your database interactions are both‬
B
‭secure and efficient.‬

‭Q Usage of Dataset‬

I‭n ADO.NET, a‬‭DataSet‬‭is an in-memory representation‬‭of data that can hold multiple tables‬
‭and their relationships. It allows you to work with data in a disconnected manner, meaning‬
‭you can retrieve data from a database, manipulate it locally, and then, if needed, update the‬
‭database without maintaining a persistent connection.‬

‭Key Characteristics of DataSet‬

‭‬ D
● ‭ isconnected Architecture‬‭: Data is retrieved and worked‬‭with offline.‬
‭●‬ ‭Multiple Tables‬‭: A‬‭ DataSet‬‭can contain multiple‬‭
DataTable‬‭objects.‬
‭●‬ ‭Data Relationships‬‭: You can define relationships between‬‭tables (similar to foreign‬
‭keys).‬
‭●‬ ‭In-Memory Cache‬‭: Data is stored in memory, and changes‬‭can be made before‬
‭syncing with the database.‬
‭●‬ ‭XML Support‬‭:‬‭ DataSet‬‭can be serialized to XML and‬‭deserialized from XML,‬
‭making it suitable for data exchange.‬

‭Basic Operations with DataSet‬

‭1. Creating and Filling a DataSet‬

DataSet‬
‭To create and fill a‬‭ DataAdapter‬‭to‬‭retrieve data from a database.‬
‭, use a‬‭
/‭/ Define the SQL query‬
‭string query = "SELECT * FROM Employees";‬

/‭/ Create a DataAdapter and a DataSet‬


‭SqlDataAdapter adapter = new SqlDataAdapter(query, connectionString);‬
‭DataSet dataSet = new DataSet();‬

/‭/ Fill the DataSet with data from the database‬


‭adapter.Fill(dataSet, "Employees");‬

‭In this example:‬

‭●‬ A SqlDataAdapter‬‭is used to fill the‬‭


‭ ‬‭ DataSet‬‭with‬‭data from the "Employees"‬
‭table in the database.‬
‭●‬ ‭The‬‭ Fill‬‭method populates the‬‭
DataSet‬‭with the result‬‭set, and the data is stored‬
DataTable‬‭named "Employees" inside the‬‭
‭in a‬‭ DataSet‬
‭.‬

‭2. Accessing Data in DataSet‬

DataSet‬‭by referencing its‬‭


‭You can access data in a‬‭ DataTable‬‭and‬‭
DataRow‬‭objects.‬

/‭/ Access the "Employees" DataTable‬


‭DataTable employeesTable = dataSet.Tables["Employees"];‬

/‭/ Iterate over each row in the DataTable‬


‭foreach (DataRow row in employeesTable.Rows)‬
‭{‬
‭Console.WriteLine("Employee ID: " + row["EmployeeID"]);‬
‭Console.WriteLine("Name: " + row["FirstName"] + " " + row["LastName"]);‬
‭}‬

DataSet‬‭and iterate through its rows to‬


‭ ere, we access the "Employees" table from the‬‭
H
‭display the employee information.‬

‭3. Modifying Data in DataSet‬

DataSet‬‭without being‬‭connected to the database. The‬


‭ ou can modify the data in the‬‭
Y
‭changes are made locally and can be synchronized back to the database.‬

/‭/ Modify the data in the DataSet‬


‭DataTable employeesTable = dataSet.Tables["Employees"];‬
‭DataRow rowToUpdate = employeesTable.Rows[0];‬
‭rowToUpdate["FirstName"] = "John";‬

/‭/ Add a new row‬


‭DataRow newRow = employeesTable.NewRow();‬
‭ ewRow["EmployeeID"] = 101;‬
n
‭newRow["FirstName"] = "Jane";‬
‭newRow["LastName"] = "Doe";‬
‭employeesTable.Rows.Add(newRow);‬

/‭/ Delete a row‬


‭DataRow rowToDelete = employeesTable.Rows[1];‬
‭rowToDelete.Delete();‬

DataSet‬
‭●‬ ‭Update‬‭: You can modify existing rows in the‬‭ ‭.‬
NewRow‬‭and‬‭
‭●‬ ‭Insert‬‭: You can add new rows using‬‭ Add‬
‭.‬
Delete‬‭method.‬
‭●‬ ‭Delete‬‭: You can delete rows using the‬‭

‭4. Saving Changes to the Database‬

‭ nce changes are made in the‬‭


O DataSet‬ DataAdapter‬‭to send the‬
‭, you can use‬‭the‬‭
‭updates back to the database.‬

/‭/ Create a DataAdapter and a CommandBuilder to handle update logic‬


‭SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Employees",‬
‭connectionString);‬
‭SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);‬

/‭/ Save the changes in the DataSet back to the database‬


‭adapter.Update(dataSet, "Employees");‬

Update‬‭method of the‬‭
‭The‬‭ SqlDataAdapter‬‭will apply‬‭any changes (inserts, updates,‬
DataSet‬‭to the database.‬
‭deletes) from the‬‭

‭5. Handling Multiple Tables‬

DataSet‬‭can store multiple‬‭


‭ ‬‭
A DataTable‬‭objects, which‬‭allows you to work with related‬
‭tables in memory.‬

‭ tring query1 = "SELECT * FROM Employees";‬


s
‭string query2 = "SELECT * FROM Departments";‬

‭ qlDataAdapter adapter1 = new SqlDataAdapter(query1, connectionString);‬


S
‭SqlDataAdapter adapter2 = new SqlDataAdapter(query2, connectionString);‬

/‭/ Create a DataSet and fill it with multiple tables‬


‭DataSet dataSet = new DataSet();‬
‭adapter1.Fill(dataSet, "Employees");‬
‭adapter2.Fill(dataSet, "Departments");‬

‭// Accessing the multiple tables‬


‭ ataTable employeesTable = dataSet.Tables["Employees"];‬
D
‭DataTable departmentsTable = dataSet.Tables["Departments"];‬

‭Establishing Relationships Between Tables‬

DataTable‬‭objects‬‭in a‬‭
‭ ou can define relationships between‬‭
Y DataSet‬‭similar to foreign‬
‭key constraints in a database.‬

‭ ataColumn parentColumn = dataSet.Tables["Departments"].Columns["DepartmentID"];‬


D
‭DataColumn childColumn = dataSet.Tables["Employees"].Columns["DepartmentID"];‬

/‭/ Define a DataRelation to relate the two tables‬


‭DataRelation relation = new DataRelation("DeptEmployeeRelation", parentColumn,‬
‭childColumn);‬
‭dataSet.Relations.Add(relation);‬

Relations‬‭property to navigate‬‭between related rows in different‬


‭ ow, you can use the‬‭
N
‭tables.‬

‭7. Serializing and Deserializing DataSet to XML‬

DataSet‬‭supports easy conversion to and from XML for‬‭data exchange or storage.‬


‭●‬ ‭Write DataSet to XML‬‭:‬

‭dataSet.WriteXml("employees.xml");‬

‭Read DataSet from XML‬‭:‬

‭ ataSet newDataSet = new DataSet();‬


D
‭newDataSet.ReadXml("employees.xml");‬

‭Advantages of DataSet‬

‭1.‬ D ‭ isconnected Data Access‬‭: A‬‭ DataSet‬‭allows you to‬‭work with data without‬
‭needing a continuous connection to the database.‬
‭2.‬ ‭In-Memory Data‬‭: It can store data in memory, and the‬‭data can be modified, filtered,‬
‭and sorted before committing changes to the database.‬
‭3.‬ ‭Multiple Tables and Relationships‬‭: You can manage‬‭multiple tables in one‬
DataSet‬
‭ ‭, defining relationships between them just‬‭like in a relational database.‬
‭4.‬ X DataSet‬‭supports reading and writing‬‭XML, making it easy to‬
‭ ML Integration‬‭:‬‭
‭exchange data with other systems or store data locally.‬

‭Use Cases of DataSet‬


‭●‬ O DataSet‬‭is ideal for applications where data needs to be‬
‭ ffline Data Access‬‭: A‬‭
‭manipulated offline and updated to the database later.‬
‭●‬ ‭Working with Multiple Tables‬‭: When you need to work‬‭with related data from‬
DataSet‬‭is a good option.‬
‭multiple tables in memory,‬‭
‭●‬ D DataSet‬‭can be easily serialized‬‭to XML, it’s useful for‬
‭ ata Exchange‬‭: Since‬‭
‭exchanging data between systems in a platform-independent format.‬

‭Example: Retrieving and Displaying Data Using DataSet‬


‭ tring connectionString = "Data Source=server_name;Initial‬
s
‭Catalog=database_name;Integrated Security=True";‬
‭string query = "SELECT * FROM Employees";‬

‭ sing (SqlConnection connection = new SqlConnection(connectionString))‬


u
‭{‬
‭SqlDataAdapter adapter = new SqlDataAdapter(query, connection);‬
‭DataSet dataSet = new DataSet();‬

/‭/ Fill the DataSet with data from the Employees table‬
‭adapter.Fill(dataSet, "Employees");‬

/‭/ Access the Employees DataTable‬


‭DataTable employeesTable = dataSet.Tables["Employees"];‬

/‭/ Display each employee's information‬


‭foreach (DataRow row in employeesTable.Rows)‬
‭{‬
‭Console.WriteLine("Employee ID: " + row["EmployeeID"]);‬
‭Console.WriteLine("Name: " + row["FirstName"] + " " + row["LastName"]);‬
‭}‬
‭}‬

‭Summary‬

‭●‬ D ‭ ataSet‬‭is an in-memory representation of data that‬‭can handle multiple tables and‬
‭relationships in a disconnected mode.‬
‭●‬ ‭It allows you to retrieve, manipulate, and update data without keeping an open‬
‭connection to the database.‬
DataSet‬‭using a‬‭
‭●‬ ‭You can fill a‬‭ DataAdapter‬ ‭, and changes‬‭made to the data can‬
Update‬‭method.‬
‭ e sent back to the database using the‬‭
b
‭ ‬ ‭It supports XML integration, making it useful for data exchange and persistence.‬

DataSet‬‭class is particularly useful when working‬‭with applications that require‬


‭ he‬‭
T
‭complex data handling, offline operations, or multiple related tables.‬

‭Q Data adapter and command builder in disconnected model‬


I‭n ADO.NET's‬‭disconnected model‬‭, the‬‭DataAdapter‬‭and‬‭CommandBuilder‬‭play crucial‬
‭roles in retrieving, manipulating, and updating data from a database without maintaining an‬
‭active connection. They are key components when working with a‬‭DataSet‬‭or‬‭DataTable‬‭in‬
‭a disconnected fashion.‬

‭1. DataAdapter‬

DataSet‬‭or‬‭
‭ ‬‭DataAdapter‬‭serves as a bridge between the database‬‭and the‬‭
A DataTable‬
‭.‬
‭It is responsible for:‬

DataSet‬‭or‬‭
‭●‬ ‭Retrieving data‬‭from the database into a‬‭ DataTable‬
‭.‬
‭●‬ S
‭ ending updates‬‭from the‬‭ DataSet‬‭or‬‭
DataTable‬‭back‬‭to the database (for‬
‭inserts, updates, and deletes).‬

‭It consists of four important SQL commands (though not all are mandatory):‬

‭‬
● ‭ electCommand‬‭: Retrieves data from the database.‬
S
‭●‬ ‭InsertCommand‬‭: Inserts new rows into the database.‬
‭●‬ ‭UpdateCommand‬‭: Updates existing rows in the database.‬
‭●‬ ‭DeleteCommand‬‭: Deletes rows from the database.‬

‭Key Characteristics of DataAdapter:‬

‭●‬ D DataSet‬‭or‬‭
‭ isconnected Operation‬‭: Once the data is fetched into‬‭a‬‭ DataTable‬ ‭,‬
‭the connection can be closed, and changes are made locally in memory.‬
‭●‬ ‭Batch Processing‬‭: You can batch multiple changes (inserts,‬‭updates, deletes) and‬
‭then push them to the database in one go.‬
‭●‬ ‭Custom Commands‬‭: You can manually define SQL commands‬‭for updating data or‬
CommandBuilder‬‭to automatically generate‬‭them.‬
‭rely on a‬‭

‭Example of Using DataAdapter‬


/‭/ Connection string to the database‬
‭string connectionString = "Data Source=server_name;Initial‬
‭Catalog=database_name;Integrated Security=True";‬

/‭/ SQL query to retrieve data‬


‭string query = "SELECT * FROM Employees";‬

/‭/ Creating a connection‬


‭using (SqlConnection connection = new SqlConnection(connectionString))‬
‭{‬
‭// Create a DataAdapter with the SELECT query and connection‬
‭SqlDataAdapter adapter = new SqlDataAdapter(query, connection);‬

/‭/ Create a DataSet to hold the retrieved data‬


‭DataSet dataSet = new DataSet();‬
/‭/ Fill the DataSet with data from the Employees table‬
‭adapter.Fill(dataSet, "Employees");‬

/‭/ Now the DataSet contains a DataTable named "Employees" with the retrieved data‬
‭DataTable employeesTable = dataSet.Tables["Employees"];‬

/‭/ Manipulate data in the DataTable (insert, update, delete)‬


‭// For example, inserting a new row:‬
‭DataRow newRow = employeesTable.NewRow();‬
‭newRow["EmployeeID"] = 101;‬
‭newRow["FirstName"] = "John";‬
‭newRow["LastName"] = "Doe";‬
‭employeesTable.Rows.Add(newRow);‬

/‭/ Sending changes back to the database would require UpdateCommand (handled by‬
‭CommandBuilder)‬
‭}‬

‭2. CommandBuilder‬

‭ ‬‭CommandBuilder‬‭automatically generates the SQL commands‬‭for inserting, updating,‬


A
SelectCommand‬‭of the‬‭
‭and deleting data based on the‬‭ DataAdapter‬ ‭. This simplifies the‬
‭process when working with simple queries, as you don’t need to manually define the‬
InsertCommand‬
‭ UpdateCommand‬
‭,‬‭ DeleteCommand‬
‭, and‬‭ ‭.‬

‭Key Points About CommandBuilder:‬

SELECT‬‭query, the‬
‭●‬ ‭Automatic Command Generation‬‭: If you have a basic‬‭
‭ommandBuilder‬‭can generate the‬‭
C INSERT‬ UPDATE‬
‭,‬‭ DELETE‬‭SQL‬
‭, and‬‭
‭statements automatically.‬
CommandBuilder‬‭works with‬‭a‬‭
‭ ‬ ‭Use with DataAdapter‬‭: The‬‭
● DataAdapter‬‭to‬
DataSet‬‭or‬‭
‭handle changes made to the‬‭ DataTable‬
‭.‬
CommandBuilder‬‭requires‬‭that the‬
‭●‬ ‭Table Schema Required‬‭: The‬‭
SelectCommand‬‭returns enough schema information (e.g.,‬‭primary key) to correctly‬

INSERT‬
‭generate the‬‭ UPDATE‬
‭,‬‭ DELETE‬‭commands.‬
‭, and‬‭

‭Example of Using CommandBuilder with DataAdapter‬

‭ tring connectionString = "Data Source=server_name;Initial‬


s
‭Catalog=database_name;Integrated Security=True";‬
‭string query = "SELECT * FROM Employees";‬

‭ sing (SqlConnection connection = new SqlConnection(connectionString))‬


u
‭{‬
‭SqlDataAdapter adapter = new SqlDataAdapter(query, connection);‬
/‭/ Create a DataSet to hold the data‬
‭DataSet dataSet = new DataSet();‬
‭adapter.Fill(dataSet, "Employees");‬

/‭/ Access the Employees table in the DataSet‬


‭DataTable employeesTable = dataSet.Tables["Employees"];‬

/‭/ Modify data in the DataTable (Insert, Update, Delete)‬


‭// Insert a new row‬
‭DataRow newRow = employeesTable.NewRow();‬
‭newRow["EmployeeID"] = 101;‬
‭newRow["FirstName"] = "Jane";‬
‭newRow["LastName"] = "Smith";‬
‭employeesTable.Rows.Add(newRow);‬

/‭/ Create a CommandBuilder to automatically generate the SQL commands‬


‭SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);‬

/‭/ Update the database with the changes made in the DataSet‬
‭adapter.Update(dataSet, "Employees");‬
‭}‬

‭In this example:‬

DataSet‬
‭●‬ ‭DataAdapter‬‭retrieves the data into a‬‭ ‭.‬
DataSet‬
‭●‬ ‭Changes (like inserting a new row) are made in memory in the‬‭ ‭.‬
INSERT‬
‭●‬ ‭The‬‭CommandBuilder‬‭automatically generates the necessary‬‭ UPDATE‬
‭,‬‭ ‭,‬
DELETE‬‭SQL commands.‬
‭and‬‭
Update‬‭method of the‬‭
‭●‬ ‭The‬‭ DataAdapter‬‭sends the changes‬‭back to the database.‬

‭Step-by-Step Process‬

SelectCommand‬‭of the‬‭
‭1.‬ ‭Fill the DataSet‬‭: The‬‭ DataAdapter‬‭fetches data from the‬
‭ atabase and populates the‬‭
d DataSet‬‭or‬‭
DataTable‬ ‭.‬
‭2.‬ ‭Manipulate Data‬‭: You can insert, update, or delete‬‭rows in the‬
DataSet‬
‭ ‭/‭
D
‬ ataTable‬‭in memory.‬
‭3.‬ C CommandBuilder‬‭generates‬‭the necessary SQL‬
‭ ommandBuilder Setup‬‭: The‬‭
‭commands (INSERT, UPDATE, DELETE) based on the schema of the data in the‬
DataSet‬
‭ ‭.‬
Update‬‭method of the‬‭
‭4.‬ ‭Update the Database‬‭: The‬‭ DataAdapter‬‭pushes the‬
DataSet‬‭back to the database, using‬‭the commands generated by‬
‭changes from the‬‭
CommandBuilder‬
‭the‬‭ ‭.‬

‭When to Use DataAdapter and CommandBuilder‬


‭●‬ W ‭ hen working in a disconnected environment‬‭: If your application needs to‬
‭retrieve data, work with it offline, and then send updates back to the database later.‬
‭●‬ ‭When you need to work with multiple records‬‭: A‬‭ DataSet‬‭can handle multiple‬
‭records and multiple tables, making it useful for batch processing.‬
‭●‬ ‭When automatic command generation is sufficient‬‭: For‬‭simple scenarios, where‬
‭your‬‭SelectCommand‬‭retrieves all columns from a single‬‭table, a‬‭
CommandBuilder‬
‭can handle the generation of the necessary commands.‬

‭Limitations of CommandBuilder‬

CommandBuilder‬‭only works‬‭when your‬


‭●‬ ‭Simple Queries Only‬‭: The‬‭
SelectCommand‬‭retrieves data from a single table.‬‭If you're joining multiple tables,‬

InsertCommand‬
‭you’ll need to manually specify the‬‭ UpdateCommand‬
‭,‬‭ ‭, and‬
DeleteCommand‬
‭ ‭.‬
‭●‬ P ‭ erformance‬‭: The‬‭CommandBuilder‬‭is convenient but‬‭may not be the most‬
‭efficient approach, especially in complex applications where performance is critical.‬
‭●‬ ‭Schema Dependence‬‭: The‬‭ CommandBuilder‬‭relies on the‬‭presence of a‬
‭well-defined schema, including primary keys.‬

‭Summary‬

‭●‬ ‭DataAdapter‬‭acts as a bridge between the database‬‭and in-memory data (‬‭


DataSet‬
o DataTable‬
‭ r‬‭ ‭), allowing you to work with data in a‬‭disconnected manner.‬
‭ ‬ ‭CommandBuilder‬‭automatically generates the necessary‬‭SQL commands for‬

‭updating the database when changes are made to the data in the‬‭ DataSet‬ ‭.‬
DataAdapter‬
‭●‬ ‭Workflow‬‭: Retrieve data using‬‭ DataSet‬
‭, manipulate‬‭it in a‬‭ ‭, and‬
CommandBuilder‬‭and‬‭
‭update the database with changes using the‬‭ DataAdapter‬
‭.‬
‭●‬ A
‭ dvantages‬‭: The combination of‬‭ DataAdapter‬‭and‬‭ CommandBuilder‬‭allows for‬
‭efficient, disconnected, and batch processing of data, making it ideal for applications‬
‭where continuous database connectivity is not required.‬

‭ hese components are essential for building data-driven applications that need to handle‬
T
‭large datasets or operate in environments with intermittent connectivity.‬

‭Q Introduction to delegate‬

‭ ‬‭delegate‬‭in C# is a type that defines a method signature‬‭and can reference a method that‬
A
‭matches this signature. In simpler terms, delegates are objects that hold references to‬
‭methods. They are often used to implement event handling and callback mechanisms.‬

‭ elegates enable methods to be passed as parameters, making them highly useful for‬
D
‭designing flexible and extensible code.‬
‭Key Features of Delegates:‬

‭1.‬ T ‭ ype-Safe‬‭: Delegates are type-safe, meaning the signature‬‭(return type and‬
‭parameters) of the method the delegate points to must match the delegate's‬
‭definition.‬
‭2.‬ ‭Encapsulation of Methods‬‭: Delegates encapsulate methods,‬‭allowing them to be‬
‭passed around like objects.‬
‭3.‬ ‭Multicast‬‭: A delegate can reference multiple methods,‬‭which is known as a multicast‬
‭delegate.‬
‭4.‬ ‭Used for Callbacks‬‭: Delegates are commonly used for‬‭callbacks and event handling‬
‭in C#.‬

‭Declaring and Using a Delegate‬

‭1. Delegate Declaration‬

‭A delegate is declared with a specific method signature (return type and parameter types).‬

‭public delegate void MyDelegate(string message);‬

MyDelegate‬‭is a delegate that can reference‬‭methods that take a‬‭


‭Here,‬‭ string‬‭parameter‬
void‬
‭and return‬‭ ‭.‬

‭2. Assigning a Method to a Delegate‬

‭You can assign a method to a delegate if the method matches the delegate's signature.‬

‭ ublic class Program‬


p
‭{‬
‭// Method matching the delegate signature‬
‭public static void DisplayMessage(string message)‬
‭{‬
‭Console.WriteLine(message);‬
‭}‬

‭ ublic static void Main()‬


p
‭{‬
‭// Creating a delegate instance and assigning a method to it‬
‭MyDelegate del = DisplayMessage;‬

/‭/ Invoking the delegate (calling the method via the delegate)‬
‭del("Hello, this is a delegate example.");‬
‭}‬
‭}‬

‭In this example:‬


‭●‬ T MyDelegate‬‭delegate is created to hold a reference to the‬‭
‭ he‬‭ DisplayMessage‬
‭method.‬
‭●‬ ‭The‬‭DisplayMessage‬‭method is invoked through the delegate‬‭by passing the‬
‭message "Hello, this is a delegate example."‬

‭3. Delegate Invocation‬

‭ nce a method is assigned to a delegate, you can invoke the delegate just like you would‬
O
‭call the method itself:‬

‭del("This is a delegate invocation.");‬

‭Types of Delegates‬

‭ .‬ S
1 ‭ ingle-cast Delegate‬‭: A delegate that holds a reference‬‭to a single method.‬
‭2.‬ ‭Multi-cast Delegate‬‭: A delegate that holds references‬‭to multiple methods. When‬
‭invoked, all the methods are called in order.‬

‭Example of a Multicast Delegate‬


‭public delegate void MyDelegate(string message);‬

‭ ublic class Program‬


p
‭{‬
‭public static void Method1(string message)‬
‭{‬
‭Console.WriteLine("Method1: " + message);‬
‭}‬

‭ ublic static void Method2(string message)‬


p
‭{‬
‭Console.WriteLine("Method2: " + message);‬
‭}‬

‭ ublic static void Main()‬


p
‭{‬
‭MyDelegate del1 = Method1;‬
‭MyDelegate del2 = Method2;‬

/‭/ Combine two delegates (multicast)‬


‭MyDelegate del = del1 + del2;‬

/‭/ Invoke both methods through multicast delegate‬


‭del("Hello from multicast delegate!");‬
‭}‬
‭}‬
‭Output:‬

‭Method1: Hello from multicast delegate!‬

‭Method2: Hello from multicast delegate!‬

Method1‬‭and‬‭
‭Here, both‬‭ Method2‬‭are invoked when the‬‭delegate‬‭
del‬‭is called.‬

‭Built-in Delegates in C#‬

‭C# provides three built-in generic delegates:‬

‭●‬ A ‭ ction‬‭: Represents a method that takes zero or more‬‭input parameters and returns‬
void‬
‭ ‭.‬
‭●‬ ‭Func‬‭: Represents a method that takes zero or more‬‭input parameters and returns a‬
‭value.‬
bool‬
‭●‬ ‭Predicate‬‭: Represents a method that takes a single‬‭parameter and returns a‬‭
‭value.‬

‭1. Action Delegate‬

Action‬‭delegate is used when a method doesn't‬‭return a value (‬‭


‭The‬‭ void‬
‭).‬

‭ ction<string> action = DisplayMessage;‬


A
‭action("Hello from Action delegate!");‬

‭2. Func Delegate‬

Func‬‭delegate is used when a method returns a‬‭value. The last type parameter of the‬
‭The‬‭
Func‬‭delegate represents the return type.‬

‭ unc<int, int, int> add = (x, y) => x + y;‬


F
‭int result = add(5, 10);‬
‭Console.WriteLine(result); // Output: 15‬

‭3. Predicate Delegate‬

Predicate‬‭delegate represents a method that takes‬‭one argument and returns a‬


‭The‬‭
bool‬
‭ ‭.‬
‭ redicate<int> isEven = x => x % 2 == 0;‬
P
‭bool result = isEven(10);‬
‭Console.WriteLine(result); // Output: True‬

‭Practical Example: Callback Using Delegates‬

‭ elegates can be used to implement callbacks, where one method calls another method‬
D
‭asynchronously and notifies the caller when the operation is completed.‬

‭public delegate void Notify(); // Declare a delegate‬

‭ ublic class ProcessBusinessLogic‬


p
‭{‬
‭public event Notify ProcessCompleted; // Declare an event using the delegate‬

‭ ublic void StartProcess()‬


p
‭{‬
‭Console.WriteLine("Process Started...");‬
‭// Simulate some work‬
‭System.Threading.Thread.Sleep(3000);‬
‭// Raise the event‬
‭OnProcessCompleted();‬
‭}‬

‭ rotected virtual void OnProcessCompleted()‬


p
‭{‬
‭// Check if there are subscribers to the event‬
‭if (ProcessCompleted != null)‬
‭{‬
‭ProcessCompleted.Invoke();‬
‭}‬
‭}‬
‭}‬

‭ ublic class Program‬


p
‭{‬
‭public static void Main()‬
‭{‬
‭ProcessBusinessLogic process = new ProcessBusinessLogic();‬
‭process.ProcessCompleted += ProcessCompletedHandler; // Subscribe to the event‬
‭process.StartProcess();‬
‭}‬

‭ ublic static void ProcessCompletedHandler()‬


p
‭{‬
‭Console.WriteLine("Process Completed!");‬
‭}‬
‭}‬

‭In this example:‬

‭●‬ A ‭ delegate‬‭ Notify‬‭is declared, which represents a‬‭method with no parameters and‬
‭no return type.‬
‭●‬ ‭An event‬‭ ProcessCompleted‬‭is based on the delegate‬‭ Notify‬ ‭.‬
StartProcess‬‭method simulates a process and then‬‭raises the‬
‭●‬ ‭The‬‭
ProcessCompleted‬‭event.‬

‭●‬ T
‭ he‬‭ProcessCompletedHandler‬‭is the method that gets‬‭called when the event is‬
‭raised.‬

‭Summary‬

‭‬ A
● ‭ ‬‭delegate‬‭is a reference type that encapsulates a‬‭method.‬
‭●‬ ‭Delegates are useful for designing flexible applications with callbacks, events, and‬
‭dynamic method invocation.‬
‭●‬ ‭Single-cast delegates‬‭refer to a single method, while‬‭multi-cast delegates‬‭can‬
‭invoke multiple methods.‬
‭●‬ ‭C# provides built-in delegates like‬‭Action‬‭,‬‭Func‬‭,‬‭and‬‭Predicate‬‭to simplify common‬
‭delegate use cases.‬
‭●‬ ‭Delegates are heavily used in event-driven programming and callback patterns in C#.‬

‭Q Multicast Delegates‬

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy