C Sharp (programming language)
C# (pronounced see sharp) is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, procedural, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within its .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270:2006). C# is one of the programming languages designed for the Common Language Infrastructure.C# is intended to be a simple, modern, general-purpose, object-oriented programming language. Its development team is led by Anders Hejlsberg. The most recent version is C# 5.0, which was released on August 15, 2012.
C# Programming Example
In this chapter you will see some programming examples of using parameter in C#.
Qu 1:Write a program to show difference between value type parameter and reference type parameter.
Example:
using System;
namespace Example1
{
class Program
{
public static void value(int num)
{
num++;
}
public static void reference(ref int num)
{
num++;
}
static void Main(string[] args)
{
int num;
Console.Write("Enter a number:\t");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n\n\tValue Type");
Console.WriteLine("----------------");
Console.Write("\nPrevious Value:\t{0}", num);
Program.value(num);
Console.Write("\nCurrent Value:\t{0}", num);
Console.WriteLine("\n\n\n----------------");
Console.WriteLine("\tReference Type");
Console.WriteLine("--------------------");
Console.Write("\nPrevious Value:\t{0}", num);
Program.reference(ref num);
Console.Write("\nCurrent Value:\t{0}", num);
Console.ReadLine();
}
}
}
namespace Example1
{
class Program
{
public static void value(int num)
{
num++;
}
public static void reference(ref int num)
{
num++;
}
static void Main(string[] args)
{
int num;
Console.Write("Enter a number:\t");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n\n\tValue Type");
Console.WriteLine("----------------");
Console.Write("\nPrevious Value:\t{0}", num);
Program.value(num);
Console.Write("\nCurrent Value:\t{0}", num);
Console.WriteLine("\n\n\n----------------");
Console.WriteLine("\tReference Type");
Console.WriteLine("--------------------");
Console.Write("\nPrevious Value:\t{0}", num);
Program.reference(ref num);
Console.Write("\nCurrent Value:\t{0}", num);
Console.ReadLine();
}
}
}
Output
Enter a number: 9
Value Type
---------------------------------
Previous Value: 9
Current Value: 9
---------------------------------
Reference Type
---------------------------------
Previous Value: 9
Current Value: 10
Qu2:Write a program in which accept two argument as parameter from the user and returns four output value as add, subtract, multiplication and division.
Example:
using System;
namespace Example2
{
class Program
{
public static void parameter(int num1, int num2, out int add, out int sub, out int mul, out float div)
{
add = num1 + num2;
sub = num1 - num2;
mul = num1 * num2;
div = (float)num1 / num2;
}
static void Main(string[] args)
{
int num1,num2;
int add, sub, mul;
float div;
Console.Write("Enter 1st number\t");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("\nEnter 2nd number\t");
num2 = Convert.ToInt32(Console.ReadLine());
Program.parameter(num1, num2, out add, out sub, out mul, out div);
Console.WriteLine("\n\n{0} + {1} = {2}", num1, num2, add);
Console.WriteLine("{0} - {1} = {2}", num1, num2, sub);
Console.WriteLine("{0} * {1} = {2}", num1, num2, mul);
Console.WriteLine("{0} / {1} = {2}", num1, num2, div);
Console.ReadLine();
}
}
}
namespace Example2
{
class Program
{
public static void parameter(int num1, int num2, out int add, out int sub, out int mul, out float div)
{
add = num1 + num2;
sub = num1 - num2;
mul = num1 * num2;
div = (float)num1 / num2;
}
static void Main(string[] args)
{
int num1,num2;
int add, sub, mul;
float div;
Console.Write("Enter 1st number\t");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("\nEnter 2nd number\t");
num2 = Convert.ToInt32(Console.ReadLine());
Program.parameter(num1, num2, out add, out sub, out mul, out div);
Console.WriteLine("\n\n{0} + {1} = {2}", num1, num2, add);
Console.WriteLine("{0} - {1} = {2}", num1, num2, sub);
Console.WriteLine("{0} * {1} = {2}", num1, num2, mul);
Console.WriteLine("{0} / {1} = {2}", num1, num2, div);
Console.ReadLine();
}
}
}
Output
Enter 1st number 7
Enter 2nd number 9
7 +9 = 16
7 - 9 = -2
7 * 9 = 63
7 / 9 = 0.7777778
Qu3:Write a program in which pass two number as command line argument and displays the multiplication of both number.
Example:
a.Write the following command on notepad and save as commandline.cs
using System;
namespace Example3
{
class Program
{
static void Main(string[] args)
{
int num1,num2,result;
num1 = Convert.ToInt32(args[0]);
num2 = Convert.ToInt32(args[1]);
result = num1 * num2;
Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
}
}
}
namespace Example3
{
class Program
{
static void Main(string[] args)
{
int num1,num2,result;
num1 = Convert.ToInt32(args[0]);
num2 = Convert.ToInt32(args[1]);
result = num1 * num2;
Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
}
}
}
b.Open visual studio command prompt and set current path of your program.
c. Compile it as csc commandline.cs
d. Now execute program as commandline 5 4
c. Compile it as csc commandline.cs
d. Now execute program as commandline 5 4
Output
Setting environment for using Microsoft Visual Studio 2008 x86 tools.
C:\Program Files\MIcrosoft Visual Studio 9.0\VC>d:
D:\>csc commandline.cs
Microsoft <R> Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft <R> .NET Framework version 3.5
Copyright <C> Microsoft Corporation. All rights reserved.
D:\>commandline 5 4
5 x 4 = 20
D:\>_
In this chapter you studied some programming examples of parameter. In next chapter you will do some programming exercises of parameter.
Initialization of Array in C#
In this chapter you will learn:
Sometimes, you need to declare multiple variable of same data type. It is complex and time consuming process. Just for an example, you have to declare 100 integer type of variable then what would you do? Will you declare variable as follow:
int num1,num2,num3,num4,num5,num6,num7,num8.... num100;
It is not a suitable way to declare multiple variable of same data type. To overcome this situation, array came alive. An array is a collection of same data type. If you have to declare 100 variable of same data type in C#, then you can declare and initialize an array as follow.
int[] num = new
int[100];
Structure of C# array
To understand concept of array, consider the following example. What happens in memory when we declare an integer type array?
int[] num = new
int[6];
Num Array:

It stores value in the index position of array starting with 0.
num[0] = 6;
num[1] = 23;
num[2] = 12;
num[3] = 9;
num[4] = 14;
num[5] = 52;
num[1] = 23;
num[2] = 12;
num[3] = 9;
num[4] = 14;
num[5] = 52;
Now, you can access the value of array by index
position. Such as if you have to access 3rd index of value, then you can
write as follow:
int i = num[2];
Programming Example of Declaration and Initialization of Array:
using System;
namespace Declare_Array
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[6]; //Declaring Array
//Initializing array
num[0] = 6;
num[1] = 23;
num[2] = 12;
num[3] = 9;
num[4] = 14;
num[5] = 52;
//Showing value of Array
Console.WriteLine("1st value:\t{0}", num[0]);
Console.WriteLine("2nd value:\t{0}", num[1]);
Console.WriteLine("3rd value:\t{0}", num[2]);
Console.WriteLine("4th value:\t{0}", num[3]);
Console.WriteLine("5th value:\t{0}", num[4]);
Console.WriteLine("6th value:\t{0}", num[5]);
Console.ReadLine();
}
}
}
namespace Declare_Array
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[6]; //Declaring Array
//Initializing array
num[0] = 6;
num[1] = 23;
num[2] = 12;
num[3] = 9;
num[4] = 14;
num[5] = 52;
//Showing value of Array
Console.WriteLine("1st value:\t{0}", num[0]);
Console.WriteLine("2nd value:\t{0}", num[1]);
Console.WriteLine("3rd value:\t{0}", num[2]);
Console.WriteLine("4th value:\t{0}", num[3]);
Console.WriteLine("5th value:\t{0}", num[4]);
Console.WriteLine("6th value:\t{0}", num[5]);
Console.ReadLine();
}
}
}
Output
1st value: 6
2st value: 23
3st value: 12
4st value: 9
5st value: 14
6st value: 52
In next chapter, you will learn how to store value in array.
In this chapter you learned about what is array in C#. You also learned how to declare array in c sharp proramming. In next chapter, you will learn how to store value in array.
C# Programming Examples of Variables and Data types.
In this chapter you will learn:
How to use data types and variables in a program?
Qu 1: Write a program in which accepts user’s details as name, city, age and pin number. Then store all the values in the appropriate variable and then print all the information in correct format.
using System;
namespace Examples
{
class Program
{
static void Main(string[] args)
{
string name;
string city;
sbyte age;
int pin;
// \n is used for line-break
Console.WriteLine("Enter your name\n");
name = Console.ReadLine();
Console.WriteLine("Enter Your City\n");
city = Console.ReadLine();
Console.WriteLine("Enter your age\n");
age = sbyte.Parse(Console.ReadLine());
Console.WriteLine("Enter your pin\n");
pin = Int32.Parse(Console.ReadLine());
// Printing message to console
//formatting output
Console.WriteLine("==============");
Console.WriteLine("Your Complete Address:");
Console.WriteLine("============\n");
Console.WriteLine("Name = {0}", name);
Console.WriteLine("City = {0}", city);
Console.WriteLine("Age = {0}", age);
Console.WriteLine("Pin = {0}", pin);
Console.WriteLine("===============");
Console.ReadLine();
}
}
}
namespace Examples
{
class Program
{
static void Main(string[] args)
{
string name;
string city;
sbyte age;
int pin;
// \n is used for line-break
Console.WriteLine("Enter your name\n");
name = Console.ReadLine();
Console.WriteLine("Enter Your City\n");
city = Console.ReadLine();
Console.WriteLine("Enter your age\n");
age = sbyte.Parse(Console.ReadLine());
Console.WriteLine("Enter your pin\n");
pin = Int32.Parse(Console.ReadLine());
// Printing message to console
//formatting output
Console.WriteLine("==============");
Console.WriteLine("Your Complete Address:");
Console.WriteLine("============\n");
Console.WriteLine("Name = {0}", name);
Console.WriteLine("City = {0}", city);
Console.WriteLine("Age = {0}", age);
Console.WriteLine("Pin = {0}", pin);
Console.WriteLine("===============");
Console.ReadLine();
}
}
}
Output
Enter your name : Steven Clark
Enter Your City : California
Enter your age : 37
Enter your pin : 95012
=================================================
Your Complete Address:
=================================================
Name = Steven Clark
City = California
Age = 37
Pin = 95012
=================================================
In this chapter you learned about how to use data types in C# programming to create variable. In next chapter there are some programming exercises for you. It is recommended you to do all the exercises honestly.
No comments:
Post a Comment