Friday, May 12, 2023

Private constructor in C#

 What is private constructor in C#?


In C#, a private constructor is a constructor method that can only be accessed from within the class in which it is defined. This means that no other code outside the class can create an instance of the class using the private constructor.


Private constructors are typically used in situations where you want to prevent the creation of instances of a class from outside the class itself. For example, you might use a private constructor in a class that provides only static methods and properties, and should not be instantiated.


Here is an example of a class with a private constructor:

public class clsSingleton

{

    private static Singleton instance;


    private clsSingleton() // private constructor

    {

        // initialization code

    }


    public static Singleton GetInstance()

    {

        if (instance == null)

        {

            instance = new clsSingleton();

        }

        return instance;

    }


  

}



In this example, the constructor of the "clsSingleton" class is private, so it cannot be called from outside the class. Instead, the class provides a public static method called "GetInstance()" that returns a single instance of the class. This method checks if the instance has already been created, and if not, it creates it using the private constructor.


By making the constructor private, the "clsSingleton" class ensures that only one instance of the class can be created and that it can only be created from within the class itself. This is a common pattern used to enforce the singleton design pattern in C#.

Constructor in C#

 What is the constructor in c #?


In C#, a constructor is a special method that gets called automatically when you create an instance (object) of a class. The purpose of a constructor is to initialize the object's data members or properties to default or specific values, and to perform any necessary setup operations.


A constructor is declared with the same name as the class and has no return type, not even void. In addition, a constructor may have zero or more parameters to accept input values from the caller.


For example, the following code defines a simple class named "clsPerson" with a constructor that accepts two parameters to set the object's name and age:

class clsPerson

{

    public string Name { get; set; }

    public int Age { get; set; }


    // Constructor

    public clsPerson(string name, int age)

    {

        Name = name;

        Age = age;

    }

}

To create a new instance of the "clsPerson" class, you would call the constructor like this:

ClsPerson p = new clsPerson("Dheeraj Kumar", 25);


This creates a new "clsPerson" object with the name "Dheeraj Kumar" and age 25, and assigns it to the variable "p". The constructor is automatically called during the object creation process, and sets the object's "Name" and "Age" properties to the values passed in.


Abstraction in oops or C#

 What is an abstraction in oops or c#?


Abstraction in oops or C# is a fundamental concept in Object-Oriented Programming (OOP) that refers to the idea of representing complex real-world objects or systems in a simplified way, by focusing on the essential features and ignoring the details that are not relevant to the current context.


In other words, abstraction allows us to model a system at a high level of abstraction, where we focus on what an object does, rather than how it does it. Abstraction helps to manage the complexity of a system by hiding unnecessary details and providing a simplified interface for interacting with the system.


Abstraction is often implemented through the use of abstract classes and interfaces. An abstract class is a class that cannot be instantiated and contains one or more abstract methods, which have no implementation and are intended to be overridden by subclasses. Interfaces are similar to abstract classes, but they only contain method signatures and do not have any implementation.


By defining abstract classes and interfaces, we can create a blueprint for classes that will implement them, and provide a common set of methods and properties that can be used to interact with those classes, without having to know the details of how they work.


Abstraction is important in OOP because it promotes modularity, flexibility, and maintainability by allowing us to design systems that are easily extensible and adaptable to changing requirements. By using abstraction, we can create software systems that are easier to understand, test, and modify, which leads to more reliable and efficient code.

Encapsulation in c# or Oops

 What is an encapsulation in oops or C#?

Encapsulation in oops or c# is a fundamental concept in Object-Oriented Programming (OOP) that refers to the idea of bundling data and methods that operate on that data within a single unit, or "encapsulation." This allows objects to control access to their internal state, and prevents other objects from directly accessing or modifying that state without going through the object's defined methods.

Encapsulation in c sharp



In practical terms, encapsulation means that an object's internal state is kept private, and can only be modified through a well-defined interface provided by the object. This helps to ensure that the object's state remains consistent and well-defined, even in the face of external modifications or other unexpected events.


Encapsulation is often implemented using access modifiers, such as public, private, and protected, which specify the level of access that other objects have to the data and methods within an object. By controlling access to its internal state through access modifiers, an object can ensure that it remains well-encapsulated and protected from outside interference.

CLR (common language run time) in C#

What is CLR or Common language run time?

CLR stands for Common Language Runtime, which is a core component of the .NET Framework. It is responsible for managing the execution of .NET programs, providing several key services such as memory management, security, and exception handling.


When you write a .NET program, it is typically written in a language such as C#, VB.NET, or F# and compiled into an intermediate language called Common Intermediate Language (CIL) code. When you run the program, the CLR compiles the CIL code into machine code and executes it.


The CLR provides several benefits to developers, such as the ability to write programs in multiple languages that can run on any platform that supports the .NET Framework. It also provides automatic memory management through a process called garbage collection, which helps prevent memory leaks and other common programming errors.


Overall, the CLR plays a critical role in the .NET Framework, enabling developers to write powerful, scalable, and reliable applications that can run on a wide range of devices and operating systems.

Sunday, October 3, 2021

Dot Net Framework |What is .NET framework with example?| dot net interview questions for experienced| net framework interview questions for 10 years experience|What is NET Framework in C# interview questions and answers?

 What is .NET framework with example?| dot net interview questions for experienced| net framework interview questions for 10 years experience

dotnetshala
Dot Net Framework |What is .NET framework with example?| dot net interview questions for experienced| net framework interview questions for 10 years experience | What is NET Framework in C# interview questions and answers?


What is .NET framework? :

First of all dot net is technologies which is developed by Microsoft in the year of 2000. Dot net technology is provide code execution environment where we can develop different types application like 

1. Console based application

2. it could be web based application

3. it could be windows based application

4. it could be services which is known as web service

5. Also we can develop WCF service ..etc.

Dot net framework is mainly written using of C# (C Sharp),F# (F Sharp),VB (Visual Basic) language.

Whenever we are going to write the code in dot net framework by using of C#,F# or VB language first comes CLR (Common Language Time) which convert the code into the Intermediate Language and other task of framework done by the CLR. than JIT (Just in time compiler) comes into the picture which is convert intermediate language into machine language code. JIT is part of Common Language run time compiler (CLR). so that we can say Dot net Framework is managed execution environment. finely when code is executed we have got either .exe or .dll file.   

   

CLR: CLR Stands Common Language Time which provide Memory management. If we are talking about memory management in other language it's a too difficult which was done by developer forcefully like memory cleaning when an object allocate into the memory and his work has finished than forcefully we have need to clear memory or can say deallocate memory. so in dot net framework all memory management related task done by the Common Language Run Time  like memory allocation and deallocation work. As I say in many programming language developers are responsible for allocating and releasing memory and for handling object lifetime. And as I know Common Language Run Time is also responsible to convert code into Intermediate language which is also known as IL .

  IL (Intermediate Language)

            |

     1. Assembly : (Assembly contains collection of data that describes how the elements in the assembly relate to each other. Assembly having maintain version Assembly version, File version, unique id which is in the form of GUID)

     2.  Manifest: (Manifest contains all the assembly versions requirement and security identity related information. the Assembly manifest can be stored in the form of Portable executable file or dynamic link. we can say that manifest just like a Text file which contains all the version and security related information which helps to resolve reference to resources and classes. )

    3. Metadata: (Metadata is define in binary format, which is describing program information stored in portable executable file or dynamic link library. metadata basically provide details about assembly data types like name, visibility, base class and interface)

    4. IL and Resource: (Intermediate Language )


Saturday, October 2, 2021

SQL Server Views|sql tutorial for experienced| sql server views performance| sql server views vs tables|How to create a view in SQL Server Management Studio|Types of views in SQL Server

 SQL Server Views

Sql Server View Dotnetshala
SQL Server Views|sql tutorial for experienced| sql server views performance| sql server views vs tables|How to create a view in SQL Server Management Studio|Types of views in SQL Server


Summary: In this tutorial, you will learn about views and how to manage views such as creating a new view, removing a view, and updating data of the underlying tables through a view.

When you use the Select statement to query data from one or more tables, you get a result set

For example, the following statement returns the product name, brand, and list price of all products from the tbl_customer and tbl_MemberType tables:

Query:  select ISNULL(CU.FirstName,'') [First Name],ISNULL(CU.LastName,'') [Last Name],ISNULL(CU.MembershipId,'') [Membership Id],ISNULL(MT.MemberType,'') [Member Type] from tbl_Customer CU WITH(NOLOCK) JOIN tbl_MemberType MT WITH(NOLOCK) ON MT.MemberTypeId=CU.MemberTypeId

Next time, if you want to get the same result set, you can save this query into a text file, open it, and execute it again. SQL Server provides a better way to save this query in the database catalog through a view. A view is a named query stored in the database catalog that allows you to refer to it later.

So the query above can be stored as a view using the Create View statement as follows:

Create View Vw_GetCustomerDetails

As

select ISNULL(CU.FirstName,'') [First Name],ISNULL(CU.LastName,'') [Last Name],ISNULL(CU.MembershipId,'') [Membership Id],ISNULL(MT.MemberType,'') [Member Type] from tbl_Customer CU WITH(NOLOCK) JOIN tbl_MemberType MT WITH(NOLOCK) ON MT.MemberTypeId=CU.MemberTypeId


And now you are going to get Customer Details from View:

Query: Select * from Vw_GetCustomerDetails

When receiving this query, SQL Server executes the following query:

Select * from (

select ISNULL(CU.FirstName,'') [First Name],ISNULL(CU.LastName,'') [Last Name],ISNULL(CU.MembershipId,'') [Membership Id],ISNULL(MT.MemberType,'') [Member Type] from tbl_Customer CU WITH(NOLOCK) JOIN tbl_MemberType MT WITH(NOLOCK) ON MT.MemberTypeId=CU.MemberTypeId

)

By definition, views do not store data except for indexed view.

A view may consist of columns from multiple tables using joins or just a subset of columns of a single table. This makes views useful for abstracting or hiding complex queries.

Advantages of views:

Security

You can restrict users to access directly to a table and allow them to access a subset of data via views.

For example, you can allow users to access customer name, phone, email via a view but restrict them to access the bank account and other sensitive information.

Simplicity

A relational database may have many tables with complex relationships e.g., one-to-one and one-to-many that make it difficult to navigate.

However, you can simplify the complex queries with joins and conditions using a set of views.

Consistency

Sometimes, you need to write a complex formula or logic in every query.

To make it consistent, you can hide the complex queries logic and calculations in views.

Once views are defined, you can reference the logic from the views rather than rewriting it in separate queries.

Managing views in SQL Server

  • Creating New View– show you how to create a new view in a SQL Server database.
  • Remaining a View– learn how to rename a view using the SQL Server Management Studio (SSMS) or Transact-SQL command.
  • Listing views in sql server– discuss the various way to list all views in a SQL Server Database.
  • Getting view information– how to get information about a view.
  • Removing a view– guide you how to use the DROP VIEW statement to remove one or more views from the database.
  • Creating an indexed view – show you how to create an indexed view against tables that have infrequent data modification to optimize the performance of the view.

Blazor drawback| drawback of blazor| Disadvantage of blazor in c#

  Blazor drawback| drawback of blazor| Disadvantage of blazor in c# While Blazor offers many advantages, it also has a few drawbacks to cons...