Saturday 7 September 2019

can we store different type of data in single array in c sharp?



Answer is yes.
If you declare array with normal data types you  can’t .
Example:
int[] arr = new int[2];
arr[0] = 10;
arr[1] = "karthikeyan";
this will denote error at the compile time.
Note the string “karthikeyan” was red underlined.
But we create an array of object you can.
Example:
object[] a = new object[3];

a[0] = 12;
a[1] = "Muthu";
even you can store complex  data types in object type:
class student
    {
        public int id { get; set; }
        public string name { get; set; }
        public override string ToString()
        {
            return this.name;
        }

    }

Now we create object for student and then initialize values.
student s = new student();
            s.id -= 101;
            s.name = "Muthu karthikeyan";
now assign object s to a[2].

            a[2] = s;
to display:
foreach (object o in a)
            {
                Console.WriteLine(o);
            }
Output:
12
Muthu
Muthu karthikeyan.
And also if we create array with arraylist you can easily add different type of data in single array
Example:
ArrayList arr1 = new ArrayList();
            arr1.Add(10);
            arr1.Add("vishnu");
-Thank you
Muthu karthikeyan,Madurai.


Friday 6 September 2019

Partial class in c sharp.



Coding for a single class can be split and stored more than one source file.
Then the class is called as partial class .
This is useful  when you create a asp.net project and coding will of a single class can be split and written different parts of class in .aspx file and .cs file.
The different parts  of a class in separate file must have same name. the keyword class be preceded by keyword partial. But the source file name may be different.
When the program is compiled, different parts of a class available in difffrent files get combined and become a single class.

Class A1.cs

namespace ConsoleApp2
{
      partial   class A
    {
      public   static void A1()
        {
            Console.WriteLine("A1 method");
        }
    }
}
Class A2.cs
namespace ConsoleApp2
{
   partial class A
    {
       public  static void A2()
        {
            Console.WriteLine("A2 method");
        }
    }
}

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            // The code provided will print ‘Hello World’ to the console.
            // Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
            A.A1();
            A.A2();
            Console.ReadKey();

            // Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app!
        }
    }
}

Output:
A1 method
A2 method
Restrictions of partial class:
All the partial class parts should be in same namespace and assembly.
All the parts must have the same accessibility.
If any part of a class is sealed then the whole class becomes sealed.
If any part of a class abstract then the whole class becomes abstract.

One of the main advantage of partial class is when diffferent programmer work on different parts of a same class and it enables them to split the code and write in separate files .
Muthu karthikeyan,Madurai.


To learn Dotnet in Madurai:
contact:91 9629329142

Thursday 5 September 2019

Var keyword in c sharp









 C# is a strongly typed language. That means you should declare variables before using it.
It can be of explicitly typed or implicitly typed. I.e when we know the data type of a variable you can explicitly typed it
Example:
Int a;
a=10;
or
int a=10;
but in case if you not aware of the data type of variable by using implicitly typed  manner.
Var keyword is used for that.
Var a=10;
The data type of the variable is the data type of value we are assigning to the variable.
In the above example 10 is int type. So a is int

Var name=”Muthu karthikeyan”;
In this example “ Muthu karthikeyan” is string so the data type of name is String.
The variable declared using var keyword is strongly typed. You cannot change the type of variable after declaring it.
Example:
var a =10;
a=”karthikeyan”;
The above statement will throw An error that a is int type. You cannot assign string value to integer variable.
Don’t use var keyword for declaring data type of a property and also return type of method.
Var keyword is use full  in case of linq query where we don’t know the data type that the query will return.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] arr = { "muthu", "karthikeyan", "vishnu", "pradeep" };
            var o = from a in arr where a.Length > 5 select new { len = a.Length, value = a };
            foreach(var v in o)
            {
                Console.WriteLine("name={0} length={1}", v.value, v.len);
            }
            Console.ReadKey();

            // Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app!
        }
    }
}



Output:
name=karthikeyan length=11
name=vishnu length=6
name=pradeep length=7
in the above case var keyword is very usefull.
-Muthu karthikeyan.
Madurai.