C#


Print & Print Global Variable


using System;

namespace CSharpPractice
{ 
    class Program
    {
        static string global = "kishore"; 
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            string message = Console.ReadLine();
            Console.WriteLine($"Echo: {message}");
            Console.ReadLine();
            Console.WriteLine(global);

            var number = 12;
            Console.WriteLine(number);
        }
    
    }
}

Variables and Scopes

using System;

namespace CSharpPractice
{ 
    class Program
    {
        static string global = "static can accessible in different methods";

        static double numberThree = 23.23;
        static void Main(string[] args)
        {
            int numberOne;
            numberOne = 23;
            Console.WriteLine(numberOne);

            int numberTwo = 13;
            Console.WriteLine(numberTwo);

            Console.WriteLine(numberThree);

            Console.WriteLine(global);

            //Scope not allow this var

            //if (numberTwo == 1)
            //{
            //    var numberFour = 12f;
            //}
            //Console.WriteLine(numberFour);
        }
    
    }
}

Class

Data Type - int, string, double, etc

Access modifier - public & private
	To allow access to method inside class

Class

1. Class has attributes, constructor and methods.
	attributes - variable declared in Class
	constructor - Class name and constructor name should be same
	methods - 

2. Object is an instance of a class. All the members of the class can be accessed through object.

Example 1:

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Mathematics obj = new Mathematics();
        int Total = obj.Addition(10,10);
        Console.WriteLine(Total);
    }
}

class Mathematics
{
    public int Addition(int a, int b)
    {
        return a + b;
    }
}

Classes & Namespaces | Properties of Class | Objects


************* Program.cs *************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSharpPractice.Classes;

namespace CSharpPractice
{ 
    class Program
    {
        static void Main(string[] args)
        {
            BankAccount bankAccount = new BankAccount();    //Objects
            bankAccount.AddToBalance(100);
            Console.WriteLine(bankAccount.Balance);

            Console.ReadLine();
          
        }
    
    }

    class SimpleMath
    {
        public static double Add(double n1, double n2)
        {
            return n1 + n2;
        }
    }
}

************* BankAccount.cs *************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpPractice.Classes
{
    public class BankAccount
    {
        private double balance;

        public double Balance-
        {
            get
            {
                if (balance < 1000000)
                    return balance;
                return balance;
            }
            set
            {
                if (value > 0)
                    balance = value;
                else
                    balance = 0;
            }
        }

        //ctor
        public BankAccount()
        {
            Balance = 100;
        }

        public double AddToBalance(double balanceToBeAdded)
        {
            balance += balanceToBeAdded;
            return Balance;
        }
    }
}

************* Output *************

200

---

using System;

public class Program
{
    public static void Main(string[] args)
    {
        BankAccount bankAccount = new BankAccount();
        bankAccount.AddToBalance(-102);
        Console.WriteLine($"Final Balance: {bankAccount.Balance}");
    }
}

public class BankAccount
{
    private double balance;

    public double Balance
    {
        get
        {
            if (balance < 1000000)
            {
                Console.WriteLine($"get if: {balance}");
                return balance;
            }
            else
            {
                Console.WriteLine($"get else: {balance}");
                return balance;
            }
        }
        set
        {
            if (value > 0)
            {
                balance = value;
                Console.WriteLine($"set if: {balance}");
            }
            else
            {
                balance = 0;
                Console.WriteLine($"set else: {balance}");
            }
        }
    }

    // Constructor
    public BankAccount()
    {
        Balance = 100;
        Console.WriteLine($"public BankAccount(): {Balance}");
    }

    public double AddToBalance(double balanceToBeAdded)
    {
        Console.WriteLine($"AddToBalance(): {balanceToBeAdded}");
        Balance += balanceToBeAdded; // Use the property to ensure validation
        return Balance;
    }
}
************* Output *************

set if: 100
get if: 100
public BankAccount(): 100
AddToBalance(): -102
get if: 100
set else: 0
get if: 0
get if: 0
Final Balance: 0

=== Code Execution Successful ===