The main purpose of "Dot Net Labs" provide the dot net program. You can learn dot net, .Net Core, C#, SQL, Linq step-by-step.

Tuesday 6 June 2023

What is .NET in simple word?


Introduction

.NET is a software development framework created by Microsoft that allows developers to build a wide range of applications easily.

.Net

.Net is a free, cross-platform development capability. This means that you can develop applications that can run on multiple platforms, including Windows, macOS, and Linux.

That provides a platform for building a wide range of applications. Whether you're working on web applications, mobile apps, or desktop software, games, IoT and more.

.NET supports multiple programming languages such as C#, Visual Basic.NET (VB.NET), and F#.

.NET is a free, cross-platform, open source developer platform for building many different types of applications.

Sunday 4 June 2023

How to Declaration and assign of variable in C#


Variable

Variable is a named storage location that holds a value of a particular data type. It is used to store and manipulate data during the execution of a program. Variables have a specific type, which determines the kind of data they can hold, such as integers, floating-point numbers, characters, Boolean values, or custom types.

In C#, variables can be of different types, each representing a specific kind of data. Here are some commonly used types of variables in C#:

Numeric Types:
    • int: Represents whole numbers (e.g., 42).
    • float: Represents single-precision floating-point numbers (e.g., 3.14f).
    • double: Represents double-precision floating-point numbers (e.g., 3.14).
    • decimal: Represents decimal numbers with higher precision (e.g., 3.14m).
    • byte: Represents unsigned integers (0 to 255).
    • short: Represents small integers (-32,768 to 32,767).
    • long: Represents large integers (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).

Character Types:
    • char: Represents a single character (e.g., 'A', '5', '$').

Boolean Type:
    • bool: Represents a Boolean value (either true or false).

String Type:
    • string: Represents a sequence of characters (e.g., "Hello, World!").

Enumerations:
    • enum: Represents a set of named values (e.g., days of the week, months).

Arrays:
    • type[]: Represents a collection of elements of a specific type (e.g., int[], string[]).

Object Type:
    • object: Represents any type of data (a reference to an instance of a class).

Create Variables

You can declare variables using the following syntax:

<type> <variableName>;

Here's an example of declaring variables of different types in C#:

int age; // declaring an integer variable named "age"
float temperature; // declaring a float variable named "temperature"
bool isStudent; // declaring a boolean variable named "isStudent"
string name; // declaring a string variable named "name"


You can also declare and initialize a variable in a single statement:

<type> <variableName> = <value>;

Here's an example:

int count = 0; // declaring and initializing an integer variable named "count" with a value of 0
float pi = 3.14f; // declaring and initializing a float variable named "pi" with a value of 3.14
bool isActive = true; // declaring and initializing a boolean variable named "isActive" with a value of true
string message = "Hello, world!"; // declaring and initializing a string variable named "message" with a value of "Hello, world!"

It's important to note that the variable name should follow certain rules:

    • It must start with a letter or underscore (_).
    • It can contain letters, digits, and underscores.
    • It cannot contain spaces or special characters (except underscores).

It's also recommended to use meaningful names for variables to make your code more readable and maintainable.

A demonstration of how to declare variables of different types with a screenshot:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VariableDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string Name = "Amit Kumar";
            int Age = 50;
            float num1 = 85.45f;
            double num2 = 524.2;
            char ch = 'A';
 
            Console.WriteLine("All information are :");
            Console.WriteLine(Name);
            Console.WriteLine(Age);
            Console.WriteLine(num1);
            Console.WriteLine(num2);
            Console.WriteLine(ch);
 
            Console.ReadLine();
 
        }
    }
}


Output: 


Thursday 1 June 2023

What is Static Class in C# and when to use with real example


In C#, a static class is a class that cannot be instantiated and can only contain static members, such as static methods, static properties, and static fields. It is primarily used as a container for utility functions or extension methods that can be accessed without creating an instance of the class.

The main features of static classes are as follows:

  • They can only contain static members.
  • They can't be instantiated or inherited because static classes are sealed and can't contain instance constructors. However, the developer can create static constructors to initialize the static members.

The following code is an example of a static class. We have already learned that all members of the class are static.

       public static class Product

    {
        // Static fields because Product is a static class
        public static string Name = "Laptop";
        public static float Price= 500;
        public static DateTime ManufacturingDate = new DateTime(2023, 01, 01);
        public static string Location = "Delhi";
        public static int GetDeliveryCharge()
        {
            return 10;
        }
    }

Let's see the example of static classes with their characteristics

We can create an instance of static class if you create instance of class you will get compile time error like Cannot create an instance of the static class 'Product' let's see example in below picture.

What is Static Class in C# and when to use with real example

Static class cannot be inherited means you can child class from static class if you try to inherit static class then you will get compile time error 'MyItems' cannot drive from static class 'Product' let's see an example and try it yourself for best practice.

When To Use Static Classes In C#

We cannot create instance members in the static class otherwise you will get this error 'GetDeliveryCharge' can't declare instance members in a static class

What is Static Class in C# and when to use with real example


Now we will see how can call a static member of the static class to call a static member of a static class in C#, you can simply use the class name followed by the member name, without needing to create an instance of the class. Here's an example:

using System;
using System.Xml.Linq;
 
namespace SoftwareEngineerDotNet
{
    public static class Product
    {
        public static string Location = "Delhi";
        public static int GetDeliveryCharge()
        {
            return 10;
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            // Calling the static method
            int result = Product.GetDeliveryCharge();
            Console.ReadLine();
        }
    }
}


Note that static classes cannot be inherited, and they cannot have instance constructors. They are sealed by default, which means you cannot derive a class from a static class.


Popular Posts