C# Program Program 1 //program to understand the concept of get value from user and then print and calculate them using System; class Program { static void Main(string[] args) { int a, b, c; Console.WriteLine("enter any two num"); a = Convert.ToInt32(Console.ReadLine()); b = Convert.ToInt32(Console.ReadLine()); c = a + b; Console.Write("Sum "+c); } } Program 2 //program to understand the concept of method in class using System; class demo { void d() { Console.WriteLine("called"); } public static void Main(string[] args) { demo d1 = new demo(); d1.d(); } } using System;
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
C# Program
Program 1
//program to understand the concept of get value from user and then print and
calculate them
using System;
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("enter any two num");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = a + b;
Console.Write("Sum "+c);
}
}
Program 2 //program to understand the concept of method in class
using System;
class demo
{
void d()
{
Console.WriteLine("called");
}
public static void Main(string[] args)
{
demo d1 = new demo();
d1.d();
}
}
using System;
class demo
{
void d()
{
Console.WriteLine("called");
}
public static void Main(string[] args)
{
demo d1 = new demo();
d1.d();
}
}
Program 3 //program to understand the concept of method in class
using System;
class Program
{
void demo()
{
int a, b, c;
Console.WriteLine("enter any two num");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = a + b;
Console.Write("Sum " + c);
}
static void Main(string[] args)
{
Program k = new Program();
k.demo();
}
}
Program 4 //program to understand the concept of instance variable in class
Points to remember
If you want to use any variable in more than one method then it is necessary to declare it under class
using System;
class Program
{
int c;
void demo()
{
int a, b;
Console.WriteLine("enter any two num");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = a + b;
}
void show()
{
Console.WriteLine("value of c" + c);
}
static void Main(string[] args)
{
Program k = new Program();
k.demo();
k.show();
}
}
Program 5 //program to understand the concept of nested method in class
using System;
class Program
{
int a,b;
void assign(int x, int y)
{
a = x;
b = y;
}
int largest()
{
if (a > b)
return a;
else
return b;
}
void show()
{
Console.WriteLine("largest num is " + largest ());
}
static void Main(string[] args)
{
Program k = new Program();
k.assign(4, 5);
k.show();
}
}
Program 6 //program to understand the concept of static variable and static method
using System;
class Program
{
static int a;
static void Main(string[] args)
{
a = 10;
Console.WriteLine(a);
}
}
Program 7 //accessing variable stored in another class ((inheritance not used)
using System;
class one
{
public int t;
}
class two
{
void second()
{
one r = new one();
r.t = 10;
Console.WriteLine(r.t);
}
static void Main(string[] args)
{
two m = new two();
m.second();
}
}
Program 8 //accessing variable stored in another class (inheritance used)
using System;
class one
{
public int t;
}
class two :one
{
void second()
{
t = 10;
Console.WriteLine(t);
}
static void Main(string[] args)
{
two m = new two();
m.second();
}
}
Program 9 //accessing method stored in another class (inheritance not used)
using System;
class one
{
public void show1()
{
Console.WriteLine("iam in one");
}
public void show2()
{
Console.WriteLine("iam in two");
}
}
class two
{
void three()
{
Console.WriteLine("iam in three");
}
void four()
{
Console.WriteLine("iam in four");
}
static void Main(string[] args)
{
one t = new one();
two t1 = new two();
t.show1();
t.show2();
t1.three();
t1.four();
}
}
Program 10 //accessing method stored in another class (inheritance used)
using System;
class one
{
public void show1()
{
Console.WriteLine("iam in one");
}
public void show2()
{
Console.WriteLine("iam in two");
}
}
class two :one
{
void three()
{
Console.WriteLine("iam in three");
}
void four()
{
Console.WriteLine("iam in four");
}
static void Main(string[] args)
{
two t1 = new two();
t1.show1();
t1.show2();
t1.three();
t1.four();
}
}
Program 11 //accessing private variable stored in base class (inheritance used)
using System;
class one
{
int a;
public void assign(int x)
{
a = x;
}
public int re()
{
return a;
}
}
class two :one
{
int k;
void three()
{
k = re();
Console.WriteLine(k);
}
static void Main(string[] args)
{
two t1 = new two();
t1.assign(30);
t1.three();
}
}
Program 12 // object as argument
using System;
class demo
{
int t;
void assign(int x)
{
t = x;
}
void process(demo r1)
{
Console.WriteLine("value" +t);
Console.WriteLine("value "+ r1.t);
}
public static void Main(string[] args)
{
demo y = new demo();
demo y1 = new demo();
y.assign(2);
y1.assign(4);
y.process(y1);
}
}
Program 13
//static variable
using System;
class demo
{
static int t;
void co()
{
t = t + 1;
}
void show()
{
Console.WriteLine(t);
}
public static void Main(string[] args)
{
demo y = new demo();
demo y1 = new demo();
y.co();
y1.co();
y.show();
y1.show();
}
}
Program 14 //static method
using System;
class demo
{
static int t;
int y;
void co()
{
y=++t;
}
void show()
{
Console.WriteLine("value of y"+ y);
}
static void sh()
{
Console.WriteLine("value of t" + t);
}
public static void Main(string[] args)
{
demo a = new demo();
demo b = new demo();
a.co();
b.co();
sh();
a.show();
b.show();
}
}
Program 15 //static method called from another class
using System;
class demo
{
static int t;
int y;
public void co()
{
y = ++t;
}
public void show()
{
Console.WriteLine("value of y" + y);
}
public static void sh()
{
Console.WriteLine("value of t" + t);
}
}
class work
{
public static void Main(string[] args)
{
demo a = new demo();
demo b = new demo();
a.co();
b.co();
demo.sh();
a.show();
b.show();
}
}
Program 16 //static method called from derived class
using System;
class demo
{
static int t;
int y;
public void co()
{
y = ++t;
}
public void show()
{
Console.WriteLine("value of y" + y);
}
public static void sh()
{
Console.WriteLine("value of t" + t);
}
}
class work:demo
{
public static void Main(string[] args)
{
work a = new work();
work b = new work();
a.co();
b.co();
sh();
a.show();
b.show();
}
}
Program 17
// Namespace Declaration
using System;
using csharp;
// Program start class
class demo
{
// Main begins program execution
public static void Main()
{
// Call namespace member
myExample t = new myExample();
t.myPrint();
myPrint();
}
// Potentially ambiguous method
static void myPrint()
{
Console.WriteLine("Not a member of");
}
}
namespace csharp
{
class myExample
{
public void myPrint()
{
Console.WriteLine("This is a member of myExample.");
}
}
}
Program 18 // Namespace Declaration using alias name concept
using System;
using t = csharp.myExample; // alias
// Program start class
class demo
{
// Main begins program execution
public static void Main()
{
// Call namespace member
t.myPrint();
myPrint();
}
// Potentially ambiguous method
static void myPrint()
{
Console.WriteLine("Not a member of");
}
}
namespace csharp
{
class myExample
{
public static void myPrint()
{
Console.WriteLine("This is a member of myExample.");