Pages

Tuesday, April 9, 2013

How to create class and object in C#


Here I explained what's class and what's object and how to create a class and object,
Class: class is collection member functions and member variables.
Syntax: Class ClassName
   Object:An instance of the class is called object
Syntax: ClassName obj=new ClassName();
Example:
using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace ConsoleApplication1
{
    class sample
    {
        public void hello()
        {
            Console.WriteLine("Hi this is srinivasulu reddy");
        }
    }
    class Program
    {

        static void Main(string[] args)
        {
            sample s = new sample();
            s.hello();
            Console.ReadLine();
        }
    }
}
output: 

Note:Here I am using Console.ReadLine() for displaying output stability.





















Add two numbers using C#

Adding two numbers using C# in run time

Here I explained how to add Two numbers in run time.

Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            Console.WriteLine("Enter the first value");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the second value");
            b = Convert.ToInt32(Console.ReadLine());
            c = a + b;
            Console.WriteLine("Sum is: "+c.ToString());
            Console.ReadLine();
        }
    }
}
Output:

Display simple message using C#


Display A simple hello message in .net using C#

       Here I explain how to display a simple hello message using c# and here I am using two methods for displaying the hello word.
1)Console.Write("Hello"):->In this case cursor blinking in the same line
2)Console.WriteLine("Welcome to .Net"):->In this case cursor blinking in the next line.

Example:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Hello");//cursor blinking in the same line
            Console.WriteLine("Welcome to .Net");//In this case cursor blinking in the next line
        }
    }
}

Output: